-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#146 | Implement the process of Assignment for Save Operation (#147)
- Loading branch information
1 parent
3c15af3
commit 02035a6
Showing
22 changed files
with
704 additions
and
88 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/ays/assignment/controller/AssignmentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ays.assignment.controller; | ||
|
||
import com.ays.assignment.model.dto.request.AssignmentSaveRequest; | ||
import com.ays.assignment.service.AssignmentSaveService; | ||
import com.ays.common.model.dto.response.AysResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* REST controller class for managing assignment-related operations via HTTP requests. | ||
* This controller handles the CRUD operations for assignments in the system. | ||
* The mapping path for this controller is "/api/v1/assignment". | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
@Validated | ||
class AssignmentController { | ||
|
||
private final AssignmentSaveService assignmentSaveService; | ||
|
||
|
||
/** | ||
* Saves a new assignment to the system. | ||
* Requires ADMIN authority. | ||
* | ||
* @param saveRequest The request object containing the assignment data to be saved. | ||
* @return A response object containing the saved assignment data. | ||
*/ | ||
@PostMapping("/assignment") | ||
@PreAuthorize("hasAnyAuthority('ADMIN')") | ||
public AysResponse<Void> saveAssignment(@RequestBody @Valid AssignmentSaveRequest saveRequest) { | ||
assignmentSaveService.saveAssignment(saveRequest); | ||
return AysResponse.SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.ays.assignment.model.dto.request; | ||
|
||
import com.ays.common.model.AysPhoneNumber; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* A DTO class representing the request data for saving an assignment. | ||
* <p> | ||
* This class provides getters and setters for the description, first name, last name, latitude, longitude and phone number fields. | ||
* It also includes a builder pattern implementation for constructing instances of this class with optional parameters. | ||
* <p> | ||
* The purpose of this class is to encapsulate the request data related to saving an assignment, allowing for easy | ||
* transfer of the data between different layers of the application. | ||
*/ | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class AssignmentSaveRequest { | ||
|
||
@NotBlank | ||
private String description; | ||
|
||
@NotBlank | ||
private String firstName; | ||
|
||
@NotBlank | ||
private String lastName; | ||
|
||
@Valid | ||
@NotNull | ||
private AysPhoneNumber phoneNumber; | ||
|
||
@NotNull | ||
private Double latitude; | ||
|
||
@NotNull | ||
private Double longitude; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.ays.assignment.model.mapper; | ||
|
||
import com.ays.assignment.model.Assignment; | ||
import com.ays.assignment.model.dto.request.AssignmentSaveRequest; | ||
import com.ays.assignment.model.entity.AssignmentEntity; | ||
import com.ays.assignment.model.enums.AssignmentStatus; | ||
import com.ays.common.model.mapper.BaseMapper; | ||
import com.ays.common.util.AysRandomUtil; | ||
import com.ays.user.model.entity.UserEntity; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* UserToUserResponseMapper is an interface that defines the mapping between an {@link AssignmentSaveRequest} and an {@link Assignment}. | ||
* This interface uses the MapStruct annotation @Mapper to generate an implementation of this interface at compile-time. | ||
* <p>The class provides a static method {@code initialize()} that returns an instance of the generated mapper implementation. | ||
* <p>The interface extends the MapStruct interface {@link BaseMapper}, which defines basic mapping methods. | ||
* The interface adds no additional mapping methods, but simply defines the types to be used in the mapping process. | ||
*/ | ||
@Mapper | ||
public interface AssignmentSaveRequestToAssignmentMapper extends BaseMapper<AssignmentSaveRequest, Assignment> { | ||
|
||
/** | ||
* Maps an {@link AssignmentSaveRequest} object to an {@link AssignmentEntity} object for saving in the database. | ||
* | ||
* @param saveRequest the {@link AssignmentSaveRequest} object to be mapped. | ||
* @param institutionId the {@link String} object. | ||
* @return the mapped {@link UserEntity} object. | ||
*/ | ||
default AssignmentEntity mapForSaving(AssignmentSaveRequest saveRequest, String institutionId) { | ||
|
||
return AssignmentEntity.builder() | ||
.id(AysRandomUtil.generateUUID()) | ||
.institutionId(institutionId) | ||
.description(saveRequest.getDescription()) | ||
.firstName(saveRequest.getFirstName()) | ||
.lastName(saveRequest.getLastName()) | ||
.lineNumber(saveRequest.getPhoneNumber().getLineNumber()) | ||
.countryCode(saveRequest.getPhoneNumber().getCountryCode()) | ||
.point(saveRequest.getLatitude(), saveRequest.getLongitude()) | ||
.status(AssignmentStatus.AVAILABLE) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Initializes the mapper. | ||
* | ||
* @return the initialized mapper object. | ||
*/ | ||
static AssignmentSaveRequestToAssignmentMapper initialize() { | ||
return Mappers.getMapper(AssignmentSaveRequestToAssignmentMapper.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ays/assignment/service/AssignmentSaveService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ays.assignment.service; | ||
|
||
import com.ays.assignment.model.dto.request.AssignmentSaveRequest; | ||
|
||
/** | ||
* Assignment Save Service to perform assignment related save business operations. | ||
*/ | ||
public interface AssignmentSaveService { | ||
|
||
/** | ||
* Saves a saveRequest to the database. | ||
* | ||
* @param saveRequest the AssignmentSaveRequest entity | ||
* @return Assignment | ||
*/ | ||
void saveAssignment(AssignmentSaveRequest saveRequest); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.ays.assignment.service.impl; | ||
|
||
import com.ays.assignment.model.dto.request.AssignmentSaveRequest; | ||
import com.ays.assignment.model.entity.AssignmentEntity; | ||
import com.ays.assignment.model.mapper.AssignmentSaveRequestToAssignmentMapper; | ||
import com.ays.assignment.repository.AssignmentRepository; | ||
import com.ays.assignment.service.AssignmentSaveService; | ||
import com.ays.auth.model.AysIdentity; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* UserSaveServiceImpl is an implementation of the {@link AssignmentSaveService} interface. | ||
* It provides methods for saving assignment data and performing related operations by admin. | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
class AssignmentSaveServiceImpl implements AssignmentSaveService { | ||
|
||
private final AssignmentRepository assignmentRepository; | ||
|
||
private final AysIdentity identity; | ||
|
||
private static final AssignmentSaveRequestToAssignmentMapper assignmentSaveRequestToAssignmentMapper = AssignmentSaveRequestToAssignmentMapper.initialize(); | ||
|
||
/** | ||
* Saves a new assignment based on the provided save request. | ||
* | ||
* @param saveRequest the request object containing assignment data to be saved | ||
* @return the saved assignment | ||
*/ | ||
@Override | ||
public void saveAssignment(AssignmentSaveRequest saveRequest) { | ||
|
||
final AssignmentEntity assignmentEntity = assignmentSaveRequestToAssignmentMapper.mapForSaving(saveRequest, identity.getInstitutionId()); | ||
|
||
assignmentRepository.save(assignmentEntity); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.