From 4ff9a02c3b54f7f24b486c49e9e224d81b460808 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 19:23:10 +0300 Subject: [PATCH 01/53] Revise AssignmentRepository --- ...ssignmentEntityRepository.java => AssignmentRepository.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/com/ays/assignment/repository/{AssignmentEntityRepository.java => AssignmentRepository.java} (73%) diff --git a/src/main/java/com/ays/assignment/repository/AssignmentEntityRepository.java b/src/main/java/com/ays/assignment/repository/AssignmentRepository.java similarity index 73% rename from src/main/java/com/ays/assignment/repository/AssignmentEntityRepository.java rename to src/main/java/com/ays/assignment/repository/AssignmentRepository.java index 9a9b45207..baae3172a 100644 --- a/src/main/java/com/ays/assignment/repository/AssignmentEntityRepository.java +++ b/src/main/java/com/ays/assignment/repository/AssignmentRepository.java @@ -6,6 +6,6 @@ /** * Repository interface for performing CRUD operations on UserAssignmentEntity objects. */ -public interface AssignmentEntityRepository extends JpaRepository { +public interface AssignmentRepository extends JpaRepository { } From de64acf19fe93a460a904afa1352b59643cdb38e Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 19:23:43 +0300 Subject: [PATCH 02/53] Define AssignmentSaveRequest and AssignmentUpdateRequest --- .../dto/request/AssignmentSaveRequest.java | 26 +++++++++++++++++++ .../dto/request/AssignmentUpdateRequest.java | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java create mode 100644 src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java diff --git a/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java new file mode 100644 index 000000000..6022c35f7 --- /dev/null +++ b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java @@ -0,0 +1,26 @@ +package com.ays.assignment.model.dto.request; + +import com.ays.common.model.AysPhoneNumber; +import lombok.Builder; +import lombok.Data; + +/** + * A DTO class representing the request data for saving an assignment. + *

+ * 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. + *

+ * 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. + */ +@Data +@Builder +public class AssignmentSaveRequest { + + private String description; + private String firstName; + private String lastName; + private AysPhoneNumber phoneNumber; + private double latitude; + private double longitude; +} diff --git a/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java b/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java new file mode 100644 index 000000000..7627b1a05 --- /dev/null +++ b/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java @@ -0,0 +1,26 @@ +package com.ays.assignment.model.dto.request; + +import com.ays.common.model.AysPhoneNumber; +import lombok.Builder; +import lombok.Data; + +/** + * Represents a request object for updating an assignment with its variables. + *

+ * 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. + *

+ * The purpose of this class is to encapsulate the request data related to updating a user, allowing for easy + * transfer of the data between different layers of the application. + */ +@Data +@Builder +public class AssignmentUpdateRequest { + + private String description; + private String firstName; + private String lastName; + private AysPhoneNumber phoneNumber; + private double latitude; + private double longitude; +} From 33d9b8a22f469e1334b0a01166eed9d59db65426 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 19:24:13 +0300 Subject: [PATCH 03/53] Define AssignmentSaveService and AssignmentService --- .../service/AssignmentSaveService.java | 18 ++++++++ .../assignment/service/AssignmentService.java | 45 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/main/java/com/ays/assignment/service/AssignmentSaveService.java create mode 100644 src/main/java/com/ays/assignment/service/AssignmentService.java diff --git a/src/main/java/com/ays/assignment/service/AssignmentSaveService.java b/src/main/java/com/ays/assignment/service/AssignmentSaveService.java new file mode 100644 index 000000000..c4aacd476 --- /dev/null +++ b/src/main/java/com/ays/assignment/service/AssignmentSaveService.java @@ -0,0 +1,18 @@ +package com.ays.assignment.service; + +import com.ays.assignment.model.Assignment; +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 + */ + Assignment saveAssignment(AssignmentSaveRequest saveRequest); +} diff --git a/src/main/java/com/ays/assignment/service/AssignmentService.java b/src/main/java/com/ays/assignment/service/AssignmentService.java new file mode 100644 index 000000000..46540f103 --- /dev/null +++ b/src/main/java/com/ays/assignment/service/AssignmentService.java @@ -0,0 +1,45 @@ +package com.ays.assignment.service; + +import com.ays.assignment.model.Assignment; +import com.ays.assignment.model.dto.request.AssignmentListRequest; +import com.ays.assignment.model.dto.request.AssignmentUpdateRequest; +import com.ays.common.model.AysPage; + +/** + * Assignment service to perform assignment related business operations. + */ +public interface AssignmentService { + + /** + * Get all Assignment from database. + * + * @param listRequest covering page and pageSize + * @return Assignment list + */ + AysPage getAllAssignments(AssignmentListRequest listRequest); + + /** + * Get Assignment by Assignment ID + * + * @param id the given Assignment ID + * @return Assignment + */ + Assignment getAssignmentById(String id); + + /** + * Update Assignment by Assignment ID + * + * @param id the given Assignment ID + * @param updateRequest the given AssignmentUpdateRequest object + */ + void updateAssignment(String id, AssignmentUpdateRequest updateRequest); + + + /** + * Delete Assignment by Assignment ID + * + * @param id the given Assignment ID + */ + void deleteAssignment(String id); + +} From 9be64712d9d11d5957e7083a8900dd75e4f7ad49 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 21:41:48 +0300 Subject: [PATCH 04/53] Implement Builder classes for Assignment --- .../assignment/model/AssignmentBuilder.java | 11 +++ .../request/AssignmentSaveRequestBuilder.java | 53 ++++++++++ .../AssignmentSavedResponseBuilder.java | 43 ++++++++ .../model/entity/AssignmentEntityBuilder.java | 97 +++++++++++++++++++ 4 files changed, 204 insertions(+) create mode 100644 src/test/java/com/ays/assignment/model/AssignmentBuilder.java create mode 100644 src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java create mode 100644 src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java create mode 100644 src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java diff --git a/src/test/java/com/ays/assignment/model/AssignmentBuilder.java b/src/test/java/com/ays/assignment/model/AssignmentBuilder.java new file mode 100644 index 000000000..cd88ca6f1 --- /dev/null +++ b/src/test/java/com/ays/assignment/model/AssignmentBuilder.java @@ -0,0 +1,11 @@ +package com.ays.assignment.model; + +import com.ays.common.model.TestDataBuilder; + +public class AssignmentBuilder extends TestDataBuilder { + + public AssignmentBuilder() { + super(Assignment.class); + } + +} diff --git a/src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java b/src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java new file mode 100644 index 000000000..cf6c5b23a --- /dev/null +++ b/src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java @@ -0,0 +1,53 @@ +package com.ays.assignment.model.dto.request; + +import com.ays.common.model.AysPhoneNumber; +import com.ays.common.model.AysPhoneNumberBuilder; +import com.ays.common.model.TestDataBuilder; + +public class AssignmentSaveRequestBuilder extends TestDataBuilder { + + public AssignmentSaveRequestBuilder() { + super(AssignmentSaveRequest.class); + } + + public AssignmentSaveRequestBuilder withValidFields() { + this.withFirstName("First Name"); + this.withLastName("Last Name"); + this.withLatitude(0.0); + this.withLongitude(0.0); + this.withDescription("Description"); + this.withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()); + return this; + } + + public AssignmentSaveRequestBuilder withDescription(String description) { + data.setDescription(description); + return this; + } + + public AssignmentSaveRequestBuilder withPhoneNumber(AysPhoneNumber phoneNumber) { + data.setPhoneNumber(phoneNumber); + return this; + } + + public AssignmentSaveRequestBuilder withFirstName(String firstName) { + data.setFirstName(firstName); + return this; + } + + public AssignmentSaveRequestBuilder withLastName(String lastName) { + data.setLastName(lastName); + return this; + } + + public AssignmentSaveRequestBuilder withLatitude(double latitude) { + data.setLatitude(latitude); + return this; + } + + public AssignmentSaveRequestBuilder withLongitude(double longitude) { + data.setLongitude(longitude); + return this; + } + +} diff --git a/src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java b/src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java new file mode 100644 index 000000000..45459faaf --- /dev/null +++ b/src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java @@ -0,0 +1,43 @@ +package com.ays.assignment.model.dto.response; + +import com.ays.common.model.AysPhoneNumber; +import com.ays.common.model.TestDataBuilder; + +public class AssignmentSavedResponseBuilder extends TestDataBuilder { + + public AssignmentSavedResponseBuilder() { + super(AssignmentSavedResponse.class); + } + + + public AssignmentSavedResponseBuilder withPhoneNumber(AysPhoneNumber phoneNumber) { + data.setPhoneNumber(phoneNumber); + return this; + } + + + public AssignmentSavedResponseBuilder withDescription(String description) { + data.setDescription(description); + return this; + } + + public AssignmentSavedResponseBuilder withFirstName(String firstName) { + data.setFirstName(firstName); + return this; + } + + public AssignmentSavedResponseBuilder withLastName(String lastName) { + data.setLastName(lastName); + return this; + } + + public AssignmentSavedResponseBuilder withLongitude(double longitude) { + data.setLongitude(longitude); + return this; + } + + public AssignmentSavedResponseBuilder withLatitude(double latitude) { + data.setLatitude(latitude); + return this; + } +} diff --git a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java new file mode 100644 index 000000000..e4762d64c --- /dev/null +++ b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java @@ -0,0 +1,97 @@ +package com.ays.assignment.model.entity; + +import com.ays.assignment.model.enums.AssignmentStatus; +import com.ays.common.model.AysPhoneNumber; +import com.ays.common.model.AysPhoneNumberBuilder; +import com.ays.common.model.TestDataBuilder; +import com.ays.common.util.AysRandomUtil; +import com.ays.institution.model.entity.InstitutionEntity; +import com.ays.institution.model.entity.InstitutionEntityBuilder; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.Point; + +import java.util.ArrayList; +import java.util.List; + +public class AssignmentEntityBuilder extends TestDataBuilder { + + public AssignmentEntityBuilder() { + super(AssignmentEntity.class); + } + + public static List generateValidUserEntities(int size) { + List userEntities = new ArrayList<>(); + for (int i = 0; i < size; i++) { + AssignmentEntity assignmentEntity = new AssignmentEntityBuilder().withValidFields().build(); + userEntities.add(assignmentEntity); + } + return userEntities; + } + + public AssignmentEntityBuilder withValidFields() { + InstitutionEntity institutionEntity = new InstitutionEntityBuilder().withValidFields().build(); + + double x = Math.random(); + double y = Math.random(); + Point point = new GeometryFactory().createPoint(new Coordinate(x, y)); + + return this + .withId(AysRandomUtil.generateUUID()) + .withInstitutionId(institutionEntity.getId()) + .withInstitution(institutionEntity) + .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) + .withDescription("Description") + .withFirstName("First Name") + .withLastName("Last Name") + .withStatus(AssignmentStatus.AVAILABLE) + .withPoint(point); + } + + public AssignmentEntityBuilder withId(String id) { + data.setId(id); + return this; + } + + public AssignmentEntityBuilder withInstitutionId(String institutionId) { + data.setInstitutionId(institutionId); + return this; + } + + public AssignmentEntityBuilder withPhoneNumber(AysPhoneNumber phoneNumber) { + data.setCountryCode(phoneNumber.getCountryCode()); + data.setLineNumber(phoneNumber.getLineNumber()); + return this; + } + + public AssignmentEntityBuilder withInstitution(InstitutionEntity institution) { + data.setInstitution(institution); + return this; + } + + public AssignmentEntityBuilder withDescription(String description) { + data.setDescription(description); + return this; + } + + public AssignmentEntityBuilder withFirstName(String firstName) { + data.setFirstName(firstName); + return this; + } + + public AssignmentEntityBuilder withLastName(String lastName) { + data.setLastName(lastName); + return this; + } + + public AssignmentEntityBuilder withStatus(AssignmentStatus status) { + data.setStatus(status); + return this; + } + + public AssignmentEntityBuilder withPoint(Point point) { + data.setPoint(point); + return this; + } + +} From 0c948c18d19222ecbdf8f56e2289f3cfbba1e1bb Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 21:42:26 +0300 Subject: [PATCH 05/53] Define AssignmentSaveRequestToAssignmentMapper and AssignmentToAssignmentSavedResponseMapper --- ...signmentSaveRequestToAssignmentMapper.java | 53 +++++++++++++++++++ ...gnmentToAssignmentSavedResponseMapper.java | 28 ++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java create mode 100644 src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java new file mode 100644 index 000000000..3ff8fead7 --- /dev/null +++ b/src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java @@ -0,0 +1,53 @@ +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. + *

The class provides a static method {@code initialize()} that returns an instance of the generated mapper implementation. + *

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 { + + /** + * 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) + .firstName(saveRequest.getFirstName()) + .lastName(saveRequest.getLastName()) + .lineNumber(saveRequest.getPhoneNumber().getLineNumber()) + .countryCode(saveRequest.getPhoneNumber().getCountryCode()) + .description(saveRequest.getDescription()) + .status(AssignmentStatus.AVAILABLE) + .build(); + } + + /** + * Initializes the mapper. + * + * @return the initialized mapper object. + */ + static AssignmentSaveRequestToAssignmentMapper initialize() { + return Mappers.getMapper(AssignmentSaveRequestToAssignmentMapper.class); + } + +} diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java new file mode 100644 index 000000000..9b47b7036 --- /dev/null +++ b/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java @@ -0,0 +1,28 @@ +package com.ays.assignment.model.mapper; + +import com.ays.assignment.model.Assignment; +import com.ays.assignment.model.dto.response.AssignmentSavedResponse; +import com.ays.common.model.mapper.BaseMapper; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * UserToUserResponseMapper is an interface that defines the mapping between an {@link Assignment} and an {@link AssignmentSavedResponse}. + * This interface uses the MapStruct annotation @Mapper to generate an implementation of this interface at compile-time. + *

The class provides a static method {@code initialize()} that returns an instance of the generated mapper implementation. + *

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 AssignmentToAssignmentSavedResponseMapper extends BaseMapper { + + /** + * Initializes the mapper. + * + * @return the initialized mapper object. + */ + static AssignmentToAssignmentSavedResponseMapper initialize() { + return Mappers.getMapper(AssignmentToAssignmentSavedResponseMapper.class); + } + +} From c0e3c41179152bf0938f316192352d03f0114409 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 21:42:43 +0300 Subject: [PATCH 06/53] Define AssignmentSavedResponse --- .../dto/response/AssignmentSavedResponse.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java diff --git a/src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java b/src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java new file mode 100644 index 000000000..b9d7192a0 --- /dev/null +++ b/src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java @@ -0,0 +1,20 @@ +package com.ays.assignment.model.dto.response; + +import com.ays.common.model.AysPhoneNumber; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Builder +public class AssignmentSavedResponse { + + private String id; + private String description; + private String firstName; + private String lastName; + private AysPhoneNumber phoneNumber; + private double latitude; + private double longitude; +} From 69c54b2af2fa9639b95b228ecaa52b12d1b67e32 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 21:43:28 +0300 Subject: [PATCH 07/53] Define AssignmentSaveServiceImpl and AssignmentServiceImpl --- .../impl/AssignmentSaveServiceImpl.java | 79 +++++++++++++++++++ .../service/impl/AssignmentServiceImpl.java | 44 +++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java create mode 100644 src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java new file mode 100644 index 000000000..b269d0c96 --- /dev/null +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java @@ -0,0 +1,79 @@ +package com.ays.assignment.service.impl; + +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.mapper.AssignmentSaveRequestToAssignmentMapper; +import com.ays.assignment.repository.AssignmentRepository; +import com.ays.assignment.service.AssignmentSaveService; +import com.ays.auth.model.AysIdentity; +import com.ays.common.model.AysPhoneNumber; +import com.ays.user.util.exception.AysUserAlreadyExistsByPhoneNumberException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 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 + * @throws AysUserAlreadyExistsByPhoneNumberException if a assignment with the same phone number already exists + */ + @Override + public Assignment saveAssignment(AssignmentSaveRequest saveRequest) { + + final List assignmentsFromDatabase = assignmentRepository.findAll(); + + this.checkPhoneNumberExist(saveRequest.getPhoneNumber(), assignmentsFromDatabase); + + final AssignmentEntity assignmentEntity = assignmentSaveRequestToAssignmentMapper.mapForSaving(saveRequest, identity.getInstitutionId()); + + AssignmentEntity savedAssignment = assignmentRepository.save(assignmentEntity); + + return Assignment.builder() + .id(savedAssignment.getId()) + .description(saveRequest.getDescription()) + .firstName(saveRequest.getFirstName()) + .lastName(saveRequest.getLastName()) + .latitude(saveRequest.getLatitude()) + .longitude(saveRequest.getLongitude()) + .phoneNumber(saveRequest.getPhoneNumber()) + .build(); + + } + + /** + * Checks if a phone number already exists in the database. + * + * @param phoneNumber the phone number to check + * @param usersFromDatabase the list of assignment entities from the database + * @throws AysUserAlreadyExistsByPhoneNumberException if a user with the same phone number already exists + */ + private void checkPhoneNumberExist(final AysPhoneNumber phoneNumber, + final List usersFromDatabase) { + + final boolean isPhoneNumberExist = usersFromDatabase.stream() + .filter(userEntity -> userEntity.getCountryCode().equals(phoneNumber.getCountryCode())) + .anyMatch(userEntity -> userEntity.getLineNumber().equals(phoneNumber.getLineNumber())); + + if (isPhoneNumberExist) { + throw new AysUserAlreadyExistsByPhoneNumberException(phoneNumber); + } + } +} diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java new file mode 100644 index 000000000..b21aef657 --- /dev/null +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java @@ -0,0 +1,44 @@ +package com.ays.assignment.service.impl; + +import com.ays.assignment.model.Assignment; +import com.ays.assignment.model.dto.request.AssignmentListRequest; +import com.ays.assignment.model.dto.request.AssignmentUpdateRequest; +import com.ays.assignment.repository.AssignmentRepository; +import com.ays.assignment.service.AssignmentService; +import com.ays.auth.model.AysIdentity; +import com.ays.common.model.AysPage; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * Implementation of the AssignmentService interface. + * This class provides methods to perform assignment-related operations such as retrieving assignments, updating assignment information, and deleting assignment. + */ +@Service +@RequiredArgsConstructor +class AssignmentServiceImpl implements AssignmentService { + + private final AssignmentRepository assignmentRepository; + + private final AysIdentity identity; + + @Override + public AysPage getAllAssignments(AssignmentListRequest listRequest) { + return null; + } + + @Override + public Assignment getAssignmentById(String id) { + return null; + } + + @Override + public void updateAssignment(String id, AssignmentUpdateRequest updateRequest) { + + } + + @Override + public void deleteAssignment(String id) { + + } +} From 231d56786fbf60d268233ab545dd1141356faa87 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 21:43:44 +0300 Subject: [PATCH 08/53] Define AssignmentController --- .../controller/AssignmentController.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/ays/assignment/controller/AssignmentController.java diff --git a/src/main/java/com/ays/assignment/controller/AssignmentController.java b/src/main/java/com/ays/assignment/controller/AssignmentController.java new file mode 100644 index 000000000..f09c3a0af --- /dev/null +++ b/src/main/java/com/ays/assignment/controller/AssignmentController.java @@ -0,0 +1,49 @@ +package com.ays.assignment.controller; + +import com.ays.assignment.model.Assignment; +import com.ays.assignment.model.dto.request.AssignmentSaveRequest; +import com.ays.assignment.model.dto.response.AssignmentSavedResponse; +import com.ays.assignment.model.mapper.AssignmentToAssignmentSavedResponseMapper; +import com.ays.assignment.service.AssignmentSaveService; +import com.ays.assignment.service.AssignmentService; +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 AssignmentService assignmentService; + private final AssignmentSaveService assignmentSaveService; + + private final AssignmentToAssignmentSavedResponseMapper assignmentToAssignmentSavedResponseMapper = AssignmentToAssignmentSavedResponseMapper.initialize(); + + /** + * 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 saveAssignment(@RequestBody @Valid AssignmentSaveRequest saveRequest) { + Assignment assignment = assignmentSaveService.saveAssignment(saveRequest); + AssignmentSavedResponse assignmentSavedResponse = assignmentToAssignmentSavedResponseMapper.map(assignment); + return AysResponse.successOf(assignmentSavedResponse); + } +} From af8aa68154f94c8843075772d3a93ea3dcc35fad Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 22:06:58 +0300 Subject: [PATCH 09/53] Implement AssignmentControllerTest for Save Assignment Process --- .../controller/AssignmentControllerTest.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java diff --git a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java new file mode 100644 index 000000000..7242d6404 --- /dev/null +++ b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java @@ -0,0 +1,127 @@ +package com.ays.assignment.controller; + +import com.ays.AbstractRestControllerTest; +import com.ays.assignment.model.Assignment; +import com.ays.assignment.model.AssignmentBuilder; +import com.ays.assignment.model.dto.request.AssignmentSaveRequest; +import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder; +import com.ays.assignment.model.dto.response.AssignmentSavedResponse; +import com.ays.assignment.model.dto.response.AssignmentSavedResponseBuilder; +import com.ays.assignment.model.enums.AssignmentStatus; +import com.ays.assignment.model.mapper.AssignmentToAssignmentSavedResponseMapper; +import com.ays.assignment.service.AssignmentSaveService; +import com.ays.assignment.service.AssignmentService; +import com.ays.common.model.AysPhoneNumberBuilder; +import com.ays.common.model.dto.response.AysResponse; +import com.ays.common.model.dto.response.AysResponseBuilder; +import com.ays.common.util.exception.model.AysError; +import com.ays.util.AysMockMvcRequestBuilders; +import com.ays.util.AysMockResultMatchersBuilders; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + + +class AssignmentControllerTest extends AbstractRestControllerTest { + + @MockBean + private AssignmentService assignmentService; + + @MockBean + private AssignmentSaveService assignmentSaveService; + + private final AssignmentToAssignmentSavedResponseMapper assignmentToAssignmentSavedResponseMapper = AssignmentToAssignmentSavedResponseMapper.initialize(); + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSavedResponse() throws Exception { + // Given + AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() + .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) + .build(); + + // When + Assignment mockAssignment = new AssignmentBuilder() + .withDescription(mockAssignmentSaveRequest.getDescription()) + .withFirstName(mockAssignmentSaveRequest.getFirstName()) + .withLastName(mockAssignmentSaveRequest.getLastName()) + .withPhoneNumber(mockAssignmentSaveRequest.getPhoneNumber()) + .withStatus(AssignmentStatus.AVAILABLE) + .build(); + Mockito.when(assignmentSaveService.saveAssignment(Mockito.any(AssignmentSaveRequest.class))) + .thenReturn(mockAssignment); + + // Then + String endpoint = BASE_PATH.concat("/assignment"); + AssignmentSavedResponse mockUserSavedResponse = new AssignmentSavedResponseBuilder() + .withDescription(mockAssignment.getDescription()) + .withFirstName(mockAssignment.getFirstName()) + .withLastName(mockAssignment.getLastName()) + .withPhoneNumber(mockAssignment.getPhoneNumber()) + .withLongitude(mockAssignment.getLongitude()) + .withLatitude(mockAssignment.getLatitude()) + .build(); + AysResponse mockResponse = AysResponseBuilder.successOf(mockUserSavedResponse); + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, mockAdminUserToken.getAccessToken(), mockAssignmentSaveRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.description") + .value(mockResponse.getResponse().getDescription())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.firstName") + .value(mockResponse.getResponse().getFirstName())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.lastName") + .value(mockResponse.getResponse().getLastName())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.phoneNumber.countryCode") + .value(mockResponse.getResponse().getPhoneNumber().getCountryCode())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.phoneNumber.lineNumber") + .value(mockResponse.getResponse().getPhoneNumber().getLineNumber())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.latitude") + .value(mockResponse.getResponse().getLatitude())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.longitude") + .value(mockResponse.getResponse().getLongitude())) + ; + + Mockito.verify(assignmentSaveService, Mockito.times(1)) + .saveAssignment(Mockito.any(AssignmentSaveRequest.class)); + } + + + @Test + void givenValidAssignmentSaveRequest_whenUserUnauthorizedForSaving_thenReturnAccessDeniedException() throws Exception { + // Given + AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() + .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/assignment"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockAssignmentSaveRequest); + + AysResponse mockResponse = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } +} \ No newline at end of file From 75276e016e98a376de341b930335339e1f94855b Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 22:08:37 +0300 Subject: [PATCH 10/53] Revise AssignmentBuilder --- .../assignment/model/AssignmentBuilder.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/test/java/com/ays/assignment/model/AssignmentBuilder.java b/src/test/java/com/ays/assignment/model/AssignmentBuilder.java index cd88ca6f1..2b6931f69 100644 --- a/src/test/java/com/ays/assignment/model/AssignmentBuilder.java +++ b/src/test/java/com/ays/assignment/model/AssignmentBuilder.java @@ -1,5 +1,7 @@ package com.ays.assignment.model; +import com.ays.assignment.model.enums.AssignmentStatus; +import com.ays.common.model.AysPhoneNumber; import com.ays.common.model.TestDataBuilder; public class AssignmentBuilder extends TestDataBuilder { @@ -8,4 +10,29 @@ public AssignmentBuilder() { super(Assignment.class); } + public AssignmentBuilder withDescription(String description) { + data.setDescription(description); + return this; + } + + public AssignmentBuilder withFirstName(String firstName) { + data.setFirstName(firstName); + return this; + } + + public AssignmentBuilder withLastName(String lastName) { + data.setLastName(lastName); + return this; + } + + public AssignmentBuilder withStatus(AssignmentStatus status) { + data.setStatus(status); + return this; + } + + public AssignmentBuilder withPhoneNumber(AysPhoneNumber phoneNumber) { + data.setPhoneNumber(phoneNumber); + return this; + } + } From ff428595c3d7f952654558a3cf34681ac230eb95 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 22:08:51 +0300 Subject: [PATCH 11/53] Define AssignmentSaveServiceImplTest --- .../impl/AssignmentSaveServiceImplTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java new file mode 100644 index 000000000..a660f7f6b --- /dev/null +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -0,0 +1,96 @@ +package com.ays.assignment.service.impl; + +import com.ays.AbstractUnitTest; +import com.ays.assignment.model.Assignment; +import com.ays.assignment.model.dto.request.AssignmentSaveRequest; +import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder; +import com.ays.assignment.model.entity.AssignmentEntity; +import com.ays.assignment.model.entity.AssignmentEntityBuilder; +import com.ays.assignment.repository.AssignmentRepository; +import com.ays.auth.model.AysIdentity; +import com.ays.common.util.AysRandomUtil; +import com.ays.user.util.exception.AysUserAlreadyExistsByPhoneNumberException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; + +import java.util.List; + + +class AssignmentSaveServiceImplTest extends AbstractUnitTest { + + @InjectMocks + private AssignmentSaveServiceImpl assignmentSaveService; + + @Mock + private AssignmentRepository assignmentRepository; + + @Mock + private AysIdentity identity; + + @Test + void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment() { + + // Given + AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() + .withValidFields() + .build(); + + List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidUserEntities(10); + + AssignmentEntity mockAssignmentEntity = new AssignmentEntityBuilder() + .withValidFields().build(); + + // When + Mockito.when(assignmentRepository.findAll()) + .thenReturn(assignmentsFromDatabase); + + Mockito.when(assignmentRepository.save(Mockito.any(AssignmentEntity.class))) + .thenReturn(mockAssignmentEntity); + + Mockito.when(identity.getInstitutionId()) + .thenReturn(AysRandomUtil.generateUUID()); + + // Then + Assignment assignment = assignmentSaveService.saveAssignment(mockAssignmentSaveRequest); + + Assertions.assertEquals(mockAssignmentSaveRequest.getFirstName(), assignment.getFirstName()); + Assertions.assertEquals(mockAssignmentSaveRequest.getLastName(), assignment.getLastName()); + Assertions.assertEquals(mockAssignmentSaveRequest.getPhoneNumber().getLineNumber(), assignment.getPhoneNumber().getLineNumber()); + Assertions.assertEquals(mockAssignmentSaveRequest.getPhoneNumber().getCountryCode(), assignment.getPhoneNumber().getCountryCode()); + Assertions.assertEquals(mockAssignmentSaveRequest.getDescription(), assignment.getDescription()); + Assertions.assertEquals(mockAssignmentSaveRequest.getDescription(), assignment.getDescription()); + Assertions.assertEquals(mockAssignmentSaveRequest.getLongitude(), assignment.getLongitude()); + Assertions.assertEquals(mockAssignmentSaveRequest.getLatitude(), assignment.getLatitude()); + + Mockito.verify(assignmentRepository, Mockito.times(1)).findAll(); + Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); + } + + @Test + void givenAssignmentSaveRequestWithExistingPhoneNumber_whenAssignmentSaved_thenThrowAysUserAlreadyExistsByPhoneNumberException() { + + // Given + AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() + .withValidFields() + .build(); + + // When + List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidUserEntities(10); + assignmentsFromDatabase.get(0).setCountryCode(mockAssignmentSaveRequest.getPhoneNumber().getCountryCode()); + assignmentsFromDatabase.get(0).setLineNumber(mockAssignmentSaveRequest.getPhoneNumber().getLineNumber()); + Mockito.when(assignmentRepository.findAll()) + .thenReturn(assignmentsFromDatabase); + + // Then + Assertions.assertThrows( + AysUserAlreadyExistsByPhoneNumberException.class, + () -> assignmentSaveService.saveAssignment(mockAssignmentSaveRequest) + ); + + Mockito.verify(assignmentRepository, Mockito.times(1)).findAll(); + } + +} From d683c9b1d3fa3651b75291badf399744396ff981 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 22:44:40 +0300 Subject: [PATCH 12/53] Revise AysAssignmentAlreadyDeletedException --- .../AysAssignmentAlreadyDeletedException.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyDeletedException.java diff --git a/src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyDeletedException.java b/src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyDeletedException.java new file mode 100644 index 000000000..f0b1ee4e7 --- /dev/null +++ b/src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyDeletedException.java @@ -0,0 +1,26 @@ +package com.ays.assignment.util.exception; + +import com.ays.common.util.exception.AysAlreadyException; + +import java.io.Serial; + +/** + * Exception thrown when an assignment is deleted and attempting to perform an action that requires an assignment which is already deleted. + */ +public class AysAssignmentAlreadyDeletedException extends AysAlreadyException { + + /** + * Unique identifier for serialization. + */ + @Serial + private static final long serialVersionUID = 7742901037914306311L; + + /** + * Constructs a new {@code AysAssignmentAlreadyAvailableException} with the specified id. + * + * @param id the id of the assignment which is already available + */ + public AysAssignmentAlreadyDeletedException(String id) { + super("ASSIGNMENT IS ALREADY DELETED! id:" + id); + } +} From 867fb66888b9c9f57a8e7e7136e28e42c2022435 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 22:45:09 +0300 Subject: [PATCH 13/53] Revise AysAssignmentAlreadyAvailableException --- .../util/exception/AysAssignmentAlreadyAvailableException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyAvailableException.java b/src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyAvailableException.java index ab47bd6de..e6c5ad387 100644 --- a/src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyAvailableException.java +++ b/src/main/java/com/ays/assignment/util/exception/AysAssignmentAlreadyAvailableException.java @@ -5,7 +5,7 @@ import java.io.Serial; /** - * Exception thrown when a user assignment is available and attempting to perform an action that requires an assignment which is already available. + * Exception thrown when an assignment is available and attempting to perform an action that requires an assignment which is already available. */ public class AysAssignmentAlreadyAvailableException extends AysAlreadyException { From 646aa42d0fbdc913aa88b0b5b099ffea07e16527 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 22:45:56 +0300 Subject: [PATCH 14/53] Revise AssignmentEntity, AssignmentEntityBuilder, AssignmentRepository, AssignmentServiceImpl, AssignmentStatus, AssignmentUpdateRequest --- .../dto/request/AssignmentUpdateRequest.java | 2 + .../model/entity/AssignmentEntity.java | 7 +++ .../model/enums/AssignmentStatus.java | 3 +- .../repository/AssignmentRepository.java | 14 +++++- .../service/impl/AssignmentServiceImpl.java | 48 ++++++++++++++++++- .../model/entity/AssignmentEntityBuilder.java | 17 +++++-- 6 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java b/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java index 7627b1a05..30fc00fdd 100644 --- a/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java +++ b/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java @@ -1,5 +1,6 @@ package com.ays.assignment.model.dto.request; +import com.ays.assignment.model.enums.AssignmentStatus; import com.ays.common.model.AysPhoneNumber; import lombok.Builder; import lombok.Data; @@ -23,4 +24,5 @@ public class AssignmentUpdateRequest { private AysPhoneNumber phoneNumber; private double latitude; private double longitude; + private AssignmentStatus status; } diff --git a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java index 4be00a77f..27bc6304e 100644 --- a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java +++ b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java @@ -92,8 +92,15 @@ public boolean isDone() { return AssignmentStatus.DONE.equals(this.status); } + public boolean isDeleted() { + return AssignmentStatus.DELETED.equals(this.status); + } + public void updateAssignmentStatus(AssignmentStatus assignmentStatus) { this.status = assignmentStatus; } + public void deleteUser() { + this.status = AssignmentStatus.DELETED; + } } diff --git a/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java b/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java index c525ce031..e43240de5 100644 --- a/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java +++ b/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java @@ -9,6 +9,7 @@ public enum AssignmentStatus { RESERVED, ASSIGNED, IN_PROGRESS, - DONE + DONE, + DELETED } diff --git a/src/main/java/com/ays/assignment/repository/AssignmentRepository.java b/src/main/java/com/ays/assignment/repository/AssignmentRepository.java index baae3172a..79e843333 100644 --- a/src/main/java/com/ays/assignment/repository/AssignmentRepository.java +++ b/src/main/java/com/ays/assignment/repository/AssignmentRepository.java @@ -2,10 +2,22 @@ import com.ays.assignment.model.entity.AssignmentEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +import java.util.Optional; /** * Repository interface for performing CRUD operations on UserAssignmentEntity objects. */ -public interface AssignmentRepository extends JpaRepository { +public interface AssignmentRepository extends JpaRepository, JpaSpecificationExecutor { + + /** + * Retrieves an optional AssignmentEntity based on the provided user ID and institution ID. + * + * @param id The ID of the assignment to retrieve. + * @param institutionId The ID of the institution associated with the assignment. + * @return An optional AssignmentEntity that matches the specified ID and institution ID, or an empty optional if not found. + */ + Optional findByIdAndInstitutionId(String id, String institutionId); } diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java index b21aef657..07a3fc2d4 100644 --- a/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java @@ -3,13 +3,24 @@ import com.ays.assignment.model.Assignment; import com.ays.assignment.model.dto.request.AssignmentListRequest; import com.ays.assignment.model.dto.request.AssignmentUpdateRequest; +import com.ays.assignment.model.entity.AssignmentEntity; +import com.ays.assignment.model.mapper.AssignmentEntityToAssignmentMapper; import com.ays.assignment.repository.AssignmentRepository; import com.ays.assignment.service.AssignmentService; +import com.ays.assignment.util.exception.AysAssignmentAlreadyDeletedException; +import com.ays.assignment.util.exception.AysAssignmentNotExistByIdException; import com.ays.auth.model.AysIdentity; import com.ays.common.model.AysPage; +import com.ays.common.model.AysSpecification; +import com.ays.user.util.exception.AysUserNotExistByIdException; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.Map; + /** * Implementation of the AssignmentService interface. * This class provides methods to perform assignment-related operations such as retrieving assignments, updating assignment information, and deleting assignment. @@ -22,23 +33,56 @@ class AssignmentServiceImpl implements AssignmentService { private final AysIdentity identity; + private static final AssignmentEntityToAssignmentMapper assignmentEntityToAssignmentMapper = AssignmentEntityToAssignmentMapper.initialize(); + @Override public AysPage getAllAssignments(AssignmentListRequest listRequest) { - return null; + + final Map filter = Map.of("institutionId", identity.getInstitutionId()); + final Specification specification = AysSpecification.builder().and(filter); + + Page assignmentEntities = assignmentRepository.findAll(specification, listRequest.toPageable()); + List assignments = assignmentEntityToAssignmentMapper.map(assignmentEntities.getContent()); + return AysPage.of( + assignmentEntities, + assignments + ); + } @Override public Assignment getAssignmentById(String id) { - return null; + + final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId()) + .orElseThrow(() -> new AysAssignmentNotExistByIdException(id)); + + return assignmentEntityToAssignmentMapper.map(assignmentEntity); + } @Override public void updateAssignment(String id, AssignmentUpdateRequest updateRequest) { + final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId()) + .filter(assignment -> !assignment.isDeleted()) + .orElseThrow(() -> new AysUserNotExistByIdException(id)); + + + assignmentEntity.updateAssignmentStatus(updateRequest.getStatus()); + assignmentRepository.save(assignmentEntity); } @Override public void deleteAssignment(String id) { + final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId()) + .orElseThrow(() -> new AysAssignmentNotExistByIdException(id)); + + if (assignmentEntity.isDeleted()) { + throw new AysAssignmentAlreadyDeletedException(id); + } + + assignmentEntity.deleteUser(); + assignmentRepository.save(assignmentEntity); } } diff --git a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java index e4762d64c..d3a57618f 100644 --- a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java +++ b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java @@ -7,9 +7,12 @@ import com.ays.common.util.AysRandomUtil; import com.ays.institution.model.entity.InstitutionEntity; import com.ays.institution.model.entity.InstitutionEntityBuilder; +import org.jetbrains.annotations.NotNull; import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.CoordinateSequence; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; import java.util.ArrayList; import java.util.List; @@ -32,9 +35,7 @@ public static List generateValidUserEntities(int size) { public AssignmentEntityBuilder withValidFields() { InstitutionEntity institutionEntity = new InstitutionEntityBuilder().withValidFields().build(); - double x = Math.random(); - double y = Math.random(); - Point point = new GeometryFactory().createPoint(new Coordinate(x, y)); + Point point = getPoint(); return this .withId(AysRandomUtil.generateUUID()) @@ -48,6 +49,16 @@ public AssignmentEntityBuilder withValidFields() { .withPoint(point); } + @NotNull + private static Point getPoint() { + double x = Math.random(); + double y = Math.random(); + Coordinate[] coordinates = new Coordinate[]{new Coordinate(x, y)}; + CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); + Point point = new Point(coordinateSequence, new GeometryFactory()); + return point; + } + public AssignmentEntityBuilder withId(String id) { data.setId(id); return this; From 163bf79d8b61dcc3b64f82c5d34e08af53b96855 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 23:18:02 +0300 Subject: [PATCH 15/53] Remove AssignmentService and AssignmentServiceImpl and Revise AssignmentController and AssignmentControllerTest --- .../controller/AssignmentController.java | 2 - .../assignment/service/AssignmentService.java | 45 ---------- .../service/impl/AssignmentServiceImpl.java | 88 ------------------- .../controller/AssignmentControllerTest.java | 4 - 4 files changed, 139 deletions(-) delete mode 100644 src/main/java/com/ays/assignment/service/AssignmentService.java delete mode 100644 src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java diff --git a/src/main/java/com/ays/assignment/controller/AssignmentController.java b/src/main/java/com/ays/assignment/controller/AssignmentController.java index f09c3a0af..9cc4da375 100644 --- a/src/main/java/com/ays/assignment/controller/AssignmentController.java +++ b/src/main/java/com/ays/assignment/controller/AssignmentController.java @@ -5,7 +5,6 @@ import com.ays.assignment.model.dto.response.AssignmentSavedResponse; import com.ays.assignment.model.mapper.AssignmentToAssignmentSavedResponseMapper; import com.ays.assignment.service.AssignmentSaveService; -import com.ays.assignment.service.AssignmentService; import com.ays.common.model.dto.response.AysResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @@ -27,7 +26,6 @@ @Validated class AssignmentController { - private final AssignmentService assignmentService; private final AssignmentSaveService assignmentSaveService; private final AssignmentToAssignmentSavedResponseMapper assignmentToAssignmentSavedResponseMapper = AssignmentToAssignmentSavedResponseMapper.initialize(); diff --git a/src/main/java/com/ays/assignment/service/AssignmentService.java b/src/main/java/com/ays/assignment/service/AssignmentService.java deleted file mode 100644 index 46540f103..000000000 --- a/src/main/java/com/ays/assignment/service/AssignmentService.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.ays.assignment.service; - -import com.ays.assignment.model.Assignment; -import com.ays.assignment.model.dto.request.AssignmentListRequest; -import com.ays.assignment.model.dto.request.AssignmentUpdateRequest; -import com.ays.common.model.AysPage; - -/** - * Assignment service to perform assignment related business operations. - */ -public interface AssignmentService { - - /** - * Get all Assignment from database. - * - * @param listRequest covering page and pageSize - * @return Assignment list - */ - AysPage getAllAssignments(AssignmentListRequest listRequest); - - /** - * Get Assignment by Assignment ID - * - * @param id the given Assignment ID - * @return Assignment - */ - Assignment getAssignmentById(String id); - - /** - * Update Assignment by Assignment ID - * - * @param id the given Assignment ID - * @param updateRequest the given AssignmentUpdateRequest object - */ - void updateAssignment(String id, AssignmentUpdateRequest updateRequest); - - - /** - * Delete Assignment by Assignment ID - * - * @param id the given Assignment ID - */ - void deleteAssignment(String id); - -} diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java deleted file mode 100644 index 07a3fc2d4..000000000 --- a/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.ays.assignment.service.impl; - -import com.ays.assignment.model.Assignment; -import com.ays.assignment.model.dto.request.AssignmentListRequest; -import com.ays.assignment.model.dto.request.AssignmentUpdateRequest; -import com.ays.assignment.model.entity.AssignmentEntity; -import com.ays.assignment.model.mapper.AssignmentEntityToAssignmentMapper; -import com.ays.assignment.repository.AssignmentRepository; -import com.ays.assignment.service.AssignmentService; -import com.ays.assignment.util.exception.AysAssignmentAlreadyDeletedException; -import com.ays.assignment.util.exception.AysAssignmentNotExistByIdException; -import com.ays.auth.model.AysIdentity; -import com.ays.common.model.AysPage; -import com.ays.common.model.AysSpecification; -import com.ays.user.util.exception.AysUserNotExistByIdException; -import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.Map; - -/** - * Implementation of the AssignmentService interface. - * This class provides methods to perform assignment-related operations such as retrieving assignments, updating assignment information, and deleting assignment. - */ -@Service -@RequiredArgsConstructor -class AssignmentServiceImpl implements AssignmentService { - - private final AssignmentRepository assignmentRepository; - - private final AysIdentity identity; - - private static final AssignmentEntityToAssignmentMapper assignmentEntityToAssignmentMapper = AssignmentEntityToAssignmentMapper.initialize(); - - @Override - public AysPage getAllAssignments(AssignmentListRequest listRequest) { - - final Map filter = Map.of("institutionId", identity.getInstitutionId()); - final Specification specification = AysSpecification.builder().and(filter); - - Page assignmentEntities = assignmentRepository.findAll(specification, listRequest.toPageable()); - List assignments = assignmentEntityToAssignmentMapper.map(assignmentEntities.getContent()); - return AysPage.of( - assignmentEntities, - assignments - ); - - } - - @Override - public Assignment getAssignmentById(String id) { - - final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId()) - .orElseThrow(() -> new AysAssignmentNotExistByIdException(id)); - - return assignmentEntityToAssignmentMapper.map(assignmentEntity); - - } - - @Override - public void updateAssignment(String id, AssignmentUpdateRequest updateRequest) { - - final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId()) - .filter(assignment -> !assignment.isDeleted()) - .orElseThrow(() -> new AysUserNotExistByIdException(id)); - - - assignmentEntity.updateAssignmentStatus(updateRequest.getStatus()); - assignmentRepository.save(assignmentEntity); - } - - @Override - public void deleteAssignment(String id) { - - final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId()) - .orElseThrow(() -> new AysAssignmentNotExistByIdException(id)); - - if (assignmentEntity.isDeleted()) { - throw new AysAssignmentAlreadyDeletedException(id); - } - - assignmentEntity.deleteUser(); - assignmentRepository.save(assignmentEntity); - } -} diff --git a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java index 7242d6404..4f602c571 100644 --- a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java +++ b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java @@ -10,7 +10,6 @@ import com.ays.assignment.model.enums.AssignmentStatus; import com.ays.assignment.model.mapper.AssignmentToAssignmentSavedResponseMapper; import com.ays.assignment.service.AssignmentSaveService; -import com.ays.assignment.service.AssignmentService; import com.ays.common.model.AysPhoneNumberBuilder; import com.ays.common.model.dto.response.AysResponse; import com.ays.common.model.dto.response.AysResponseBuilder; @@ -27,9 +26,6 @@ class AssignmentControllerTest extends AbstractRestControllerTest { - @MockBean - private AssignmentService assignmentService; - @MockBean private AssignmentSaveService assignmentSaveService; From 7a4dd01ef20eefea816693f346dd760f8f0ae2bc Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 23:44:35 +0300 Subject: [PATCH 16/53] Revise AssignmentEntityBuilder and AssignmentSaveServiceImplTest --- .../ays/assignment/model/entity/AssignmentEntityBuilder.java | 2 +- .../service/impl/AssignmentSaveServiceImplTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java index d3a57618f..adc0ea0ae 100644 --- a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java +++ b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java @@ -23,7 +23,7 @@ public AssignmentEntityBuilder() { super(AssignmentEntity.class); } - public static List generateValidUserEntities(int size) { + public static List generateValidAssignmentEntities(int size) { List userEntities = new ArrayList<>(); for (int i = 0; i < size; i++) { AssignmentEntity assignmentEntity = new AssignmentEntityBuilder().withValidFields().build(); diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java index a660f7f6b..64d8a3083 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -38,7 +38,7 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment() .withValidFields() .build(); - List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidUserEntities(10); + List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidAssignmentEntities(10); AssignmentEntity mockAssignmentEntity = new AssignmentEntityBuilder() .withValidFields().build(); @@ -78,7 +78,7 @@ void givenAssignmentSaveRequestWithExistingPhoneNumber_whenAssignmentSaved_thenT .build(); // When - List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidUserEntities(10); + List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidAssignmentEntities(10); assignmentsFromDatabase.get(0).setCountryCode(mockAssignmentSaveRequest.getPhoneNumber().getCountryCode()); assignmentsFromDatabase.get(0).setLineNumber(mockAssignmentSaveRequest.getPhoneNumber().getLineNumber()); Mockito.when(assignmentRepository.findAll()) From 05de41b5e94e18e1eccb14b9f94cb9c1f7ec9785 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Tue, 15 Aug 2023 23:53:13 +0300 Subject: [PATCH 17/53] Revise AssignmentEntityBuilder --- .../model/entity/AssignmentEntityBuilder.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java index adc0ea0ae..996daa846 100644 --- a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java +++ b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java @@ -7,12 +7,9 @@ import com.ays.common.util.AysRandomUtil; import com.ays.institution.model.entity.InstitutionEntity; import com.ays.institution.model.entity.InstitutionEntityBuilder; -import org.jetbrains.annotations.NotNull; import org.locationtech.jts.geom.Coordinate; -import org.locationtech.jts.geom.CoordinateSequence; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; -import org.locationtech.jts.geom.impl.CoordinateArraySequence; import java.util.ArrayList; import java.util.List; @@ -35,7 +32,7 @@ public static List generateValidAssignmentEntities(int size) { public AssignmentEntityBuilder withValidFields() { InstitutionEntity institutionEntity = new InstitutionEntityBuilder().withValidFields().build(); - Point point = getPoint(); + Point point = createPoint(); return this .withId(AysRandomUtil.generateUUID()) @@ -49,14 +46,12 @@ public AssignmentEntityBuilder withValidFields() { .withPoint(point); } - @NotNull - private static Point getPoint() { + private Point createPoint() { double x = Math.random(); double y = Math.random(); - Coordinate[] coordinates = new Coordinate[]{new Coordinate(x, y)}; - CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); - Point point = new Point(coordinateSequence, new GeometryFactory()); - return point; + Coordinate coordinate = new Coordinate(x, y); + GeometryFactory geometryFactory = new GeometryFactory(); + return geometryFactory.createPoint(coordinate); } public AssignmentEntityBuilder withId(String id) { From 6fb497d7a3eb84fd1e7848d7d7204e955e91c2dc Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Wed, 16 Aug 2023 00:42:56 +0300 Subject: [PATCH 18/53] Revise AssignmentEntityBuilder and AssignmentEntity --- .../assignment/model/entity/AssignmentEntity.java | 7 +++++-- .../model/entity/AssignmentEntityBuilder.java | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java index 27bc6304e..d795183e5 100644 --- a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java +++ b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java @@ -11,8 +11,10 @@ import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.CoordinateSequence; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; /** * Assignment entity, which holds the information regarding assignment. @@ -67,9 +69,10 @@ public class AssignmentEntity extends BaseEntity { private UserEntity user; public void setPoint(double latitude, double longitude) { - Coordinate coordinate = new Coordinate(latitude, longitude); + Coordinate[] coordinates = new Coordinate[]{new Coordinate(latitude, longitude)}; + CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); GeometryFactory geometryFactory = new GeometryFactory(); - this.point = geometryFactory.createPoint(coordinate); + this.point = geometryFactory.createPoint(coordinateSequence); } public boolean isAvailable() { diff --git a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java index 996daa846..c67541ef5 100644 --- a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java +++ b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java @@ -8,8 +8,10 @@ import com.ays.institution.model.entity.InstitutionEntity; import com.ays.institution.model.entity.InstitutionEntityBuilder; import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.CoordinateSequence; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; import java.util.ArrayList; import java.util.List; @@ -32,7 +34,7 @@ public static List generateValidAssignmentEntities(int size) { public AssignmentEntityBuilder withValidFields() { InstitutionEntity institutionEntity = new InstitutionEntityBuilder().withValidFields().build(); - Point point = createPoint(); + Point point = generatePoint(); return this .withId(AysRandomUtil.generateUUID()) @@ -46,12 +48,14 @@ public AssignmentEntityBuilder withValidFields() { .withPoint(point); } - private Point createPoint() { + private Point generatePoint() { double x = Math.random(); double y = Math.random(); - Coordinate coordinate = new Coordinate(x, y); + Coordinate[] coordinates = new Coordinate[]{new Coordinate(x, y)}; + CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); GeometryFactory geometryFactory = new GeometryFactory(); - return geometryFactory.createPoint(coordinate); + Point point = geometryFactory.createPoint(coordinateSequence); + return point; } public AssignmentEntityBuilder withId(String id) { From 780e3df6a8740e3045296a0aa9bc88dac671428d Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Wed, 16 Aug 2023 17:14:57 +0300 Subject: [PATCH 19/53] Revise AssignmentEntityBuilder, AssignmentSaveServiceImplTest, TestBuilder --- .../model/entity/AssignmentEntityBuilder.java | 25 +------------------ .../impl/AssignmentSaveServiceImplTest.java | 1 + .../com/ays/common/model/TestDataBuilder.java | 21 ++++++++++++++++ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java index c67541ef5..a510bff56 100644 --- a/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java +++ b/src/test/java/com/ays/assignment/model/entity/AssignmentEntityBuilder.java @@ -7,11 +7,6 @@ import com.ays.common.util.AysRandomUtil; import com.ays.institution.model.entity.InstitutionEntity; import com.ays.institution.model.entity.InstitutionEntityBuilder; -import org.locationtech.jts.geom.Coordinate; -import org.locationtech.jts.geom.CoordinateSequence; -import org.locationtech.jts.geom.GeometryFactory; -import org.locationtech.jts.geom.Point; -import org.locationtech.jts.geom.impl.CoordinateArraySequence; import java.util.ArrayList; import java.util.List; @@ -34,8 +29,6 @@ public static List generateValidAssignmentEntities(int size) { public AssignmentEntityBuilder withValidFields() { InstitutionEntity institutionEntity = new InstitutionEntityBuilder().withValidFields().build(); - Point point = generatePoint(); - return this .withId(AysRandomUtil.generateUUID()) .withInstitutionId(institutionEntity.getId()) @@ -44,18 +37,7 @@ public AssignmentEntityBuilder withValidFields() { .withDescription("Description") .withFirstName("First Name") .withLastName("Last Name") - .withStatus(AssignmentStatus.AVAILABLE) - .withPoint(point); - } - - private Point generatePoint() { - double x = Math.random(); - double y = Math.random(); - Coordinate[] coordinates = new Coordinate[]{new Coordinate(x, y)}; - CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); - GeometryFactory geometryFactory = new GeometryFactory(); - Point point = geometryFactory.createPoint(coordinateSequence); - return point; + .withStatus(AssignmentStatus.AVAILABLE); } public AssignmentEntityBuilder withId(String id) { @@ -99,9 +81,4 @@ public AssignmentEntityBuilder withStatus(AssignmentStatus status) { return this; } - public AssignmentEntityBuilder withPoint(Point point) { - data.setPoint(point); - return this; - } - } diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java index 64d8a3083..56b9ae9fc 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -38,6 +38,7 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment() .withValidFields() .build(); + List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidAssignmentEntities(10); AssignmentEntity mockAssignmentEntity = new AssignmentEntityBuilder() diff --git a/src/test/java/com/ays/common/model/TestDataBuilder.java b/src/test/java/com/ays/common/model/TestDataBuilder.java index e005366e3..df82181e6 100644 --- a/src/test/java/com/ays/common/model/TestDataBuilder.java +++ b/src/test/java/com/ays/common/model/TestDataBuilder.java @@ -6,9 +6,15 @@ import org.jeasy.random.EasyRandom; import org.jeasy.random.EasyRandomParameters; import org.jeasy.random.FieldPredicates; +import org.jeasy.random.randomizers.AbstractRandomizer; import org.jeasy.random.randomizers.range.IntegerRangeRandomizer; import org.jeasy.random.randomizers.range.LongRangeRandomizer; import org.jeasy.random.randomizers.text.StringRandomizer; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.CoordinateSequence; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils; import static org.jeasy.random.FieldPredicates.named; @@ -28,6 +34,8 @@ public abstract class TestDataBuilder { private static final LongRangeRandomizer LONG_RANGE_RANDOMIZER = new LongRangeRandomizer(1L, Long.MAX_VALUE); private static final CharacterRandomizer CHARACTER_RANDOMIZER = new CharacterRandomizer(); + private static final PointRandomizer POINT_RANDOMIZER = new PointRandomizer(); + public TestDataBuilder(Class clazz) { this(clazz, false); @@ -48,6 +56,7 @@ private EasyRandomParameters getExclusionParameters(boolean excludeRelations) { parameters.randomize(Integer.class, POSITIVE_INTEGER_RANDOMIZER); parameters.randomize(String.class, CHARACTER_RANDOMIZER); parameters.randomize(Long.class, LONG_RANGE_RANDOMIZER); + parameters.randomize(Point.class, POINT_RANDOMIZER); if (!excludeRelations) return parameters; @@ -82,3 +91,15 @@ public String getRandomValue() { } } + +class PointRandomizer extends AbstractRandomizer { + + @Override + public Point getRandomValue() { + Coordinate[] coordinates = new Coordinate[]{new Coordinate(40.991739, 29.024168)}; + CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); + GeometryFactory geometryFactory = new GeometryFactory(); + return geometryFactory.createPoint(coordinateSequence); + } +} + From e72aa88d7b416bd6b8859a4ebfc39c05927ce572 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Wed, 16 Aug 2023 17:23:43 +0300 Subject: [PATCH 20/53] Revise AssignmentControllerTest --- .../controller/AssignmentControllerTest.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java index 4f602c571..f655c0fce 100644 --- a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java +++ b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java @@ -6,7 +6,6 @@ import com.ays.assignment.model.dto.request.AssignmentSaveRequest; import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder; import com.ays.assignment.model.dto.response.AssignmentSavedResponse; -import com.ays.assignment.model.dto.response.AssignmentSavedResponseBuilder; import com.ays.assignment.model.enums.AssignmentStatus; import com.ays.assignment.model.mapper.AssignmentToAssignmentSavedResponseMapper; import com.ays.assignment.service.AssignmentSaveService; @@ -48,19 +47,16 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSav .withPhoneNumber(mockAssignmentSaveRequest.getPhoneNumber()) .withStatus(AssignmentStatus.AVAILABLE) .build(); + + Mockito.when(assignmentSaveService.saveAssignment(Mockito.any(AssignmentSaveRequest.class))) .thenReturn(mockAssignment); // Then String endpoint = BASE_PATH.concat("/assignment"); - AssignmentSavedResponse mockUserSavedResponse = new AssignmentSavedResponseBuilder() - .withDescription(mockAssignment.getDescription()) - .withFirstName(mockAssignment.getFirstName()) - .withLastName(mockAssignment.getLastName()) - .withPhoneNumber(mockAssignment.getPhoneNumber()) - .withLongitude(mockAssignment.getLongitude()) - .withLatitude(mockAssignment.getLatitude()) - .build(); + + AssignmentSavedResponse mockUserSavedResponse = assignmentToAssignmentSavedResponseMapper.map(mockAssignment); + AysResponse mockResponse = AysResponseBuilder.successOf(mockUserSavedResponse); mockMvc.perform(AysMockMvcRequestBuilders .post(endpoint, mockAdminUserToken.getAccessToken(), mockAssignmentSaveRequest)) From 29fe104cdb346715f191da775de1e66e2729b596 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:07:53 +0300 Subject: [PATCH 21/53] Update AssignmentSaveRequest --- .../model/dto/request/AssignmentSaveRequest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java index 6022c35f7..a10af05f8 100644 --- a/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java +++ b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java @@ -1,6 +1,9 @@ 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.Data; @@ -17,10 +20,22 @@ @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; } From fe7045cf5e3d9d13ab8304223a2bcec1651aced3 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:13:27 +0300 Subject: [PATCH 22/53] Update AssignmentSaveRequest and AssignmentController --- .../assignment/controller/AssignmentController.java | 11 +++-------- .../model/dto/request/AssignmentSaveRequest.java | 6 ++++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/ays/assignment/controller/AssignmentController.java b/src/main/java/com/ays/assignment/controller/AssignmentController.java index 9cc4da375..d09b115cc 100644 --- a/src/main/java/com/ays/assignment/controller/AssignmentController.java +++ b/src/main/java/com/ays/assignment/controller/AssignmentController.java @@ -1,9 +1,6 @@ package com.ays.assignment.controller; -import com.ays.assignment.model.Assignment; import com.ays.assignment.model.dto.request.AssignmentSaveRequest; -import com.ays.assignment.model.dto.response.AssignmentSavedResponse; -import com.ays.assignment.model.mapper.AssignmentToAssignmentSavedResponseMapper; import com.ays.assignment.service.AssignmentSaveService; import com.ays.common.model.dto.response.AysResponse; import jakarta.validation.Valid; @@ -28,7 +25,6 @@ class AssignmentController { private final AssignmentSaveService assignmentSaveService; - private final AssignmentToAssignmentSavedResponseMapper assignmentToAssignmentSavedResponseMapper = AssignmentToAssignmentSavedResponseMapper.initialize(); /** * Saves a new assignment to the system. @@ -39,9 +35,8 @@ class AssignmentController { */ @PostMapping("/assignment") @PreAuthorize("hasAnyAuthority('ADMIN')") - public AysResponse saveAssignment(@RequestBody @Valid AssignmentSaveRequest saveRequest) { - Assignment assignment = assignmentSaveService.saveAssignment(saveRequest); - AssignmentSavedResponse assignmentSavedResponse = assignmentToAssignmentSavedResponseMapper.map(assignment); - return AysResponse.successOf(assignmentSavedResponse); + public AysResponse saveAssignment(@RequestBody @Valid AssignmentSaveRequest saveRequest) { + assignmentSaveService.saveAssignment(saveRequest); + return AysResponse.SUCCESS; } } diff --git a/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java index a10af05f8..e957290ab 100644 --- a/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java +++ b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java @@ -5,7 +5,8 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Builder; -import lombok.Data; +import lombok.Getter; +import lombok.Setter; /** * A DTO class representing the request data for saving an assignment. @@ -16,7 +17,8 @@ * 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. */ -@Data +@Getter +@Setter @Builder public class AssignmentSaveRequest { From f88062884382b2c7c47e5cd9089a878619adc99a Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:17:42 +0300 Subject: [PATCH 23/53] Remove AssignmentSavedResponse and AssignmentUpdateRequest --- .../dto/request/AssignmentUpdateRequest.java | 28 ------------------- .../dto/response/AssignmentSavedResponse.java | 20 ------------- 2 files changed, 48 deletions(-) delete mode 100644 src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java delete mode 100644 src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java diff --git a/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java b/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java deleted file mode 100644 index 30fc00fdd..000000000 --- a/src/main/java/com/ays/assignment/model/dto/request/AssignmentUpdateRequest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.ays.assignment.model.dto.request; - -import com.ays.assignment.model.enums.AssignmentStatus; -import com.ays.common.model.AysPhoneNumber; -import lombok.Builder; -import lombok.Data; - -/** - * Represents a request object for updating an assignment with its variables. - *

- * 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. - *

- * The purpose of this class is to encapsulate the request data related to updating a user, allowing for easy - * transfer of the data between different layers of the application. - */ -@Data -@Builder -public class AssignmentUpdateRequest { - - private String description; - private String firstName; - private String lastName; - private AysPhoneNumber phoneNumber; - private double latitude; - private double longitude; - private AssignmentStatus status; -} diff --git a/src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java b/src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java deleted file mode 100644 index b9d7192a0..000000000 --- a/src/main/java/com/ays/assignment/model/dto/response/AssignmentSavedResponse.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.ays.assignment.model.dto.response; - -import com.ays.common.model.AysPhoneNumber; -import lombok.Builder; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -@Builder -public class AssignmentSavedResponse { - - private String id; - private String description; - private String firstName; - private String lastName; - private AysPhoneNumber phoneNumber; - private double latitude; - private double longitude; -} From bb8daeaef4bc594c2016b7a185d4571d5aee04a7 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:19:10 +0300 Subject: [PATCH 24/53] Remove AssignmentStatus --- .../java/com/ays/assignment/model/enums/AssignmentStatus.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java b/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java index e43240de5..c525ce031 100644 --- a/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java +++ b/src/main/java/com/ays/assignment/model/enums/AssignmentStatus.java @@ -9,7 +9,6 @@ public enum AssignmentStatus { RESERVED, ASSIGNED, IN_PROGRESS, - DONE, - DELETED + DONE } From af3c4fa05cd742c81096ab282fb005ca9c8fdb5e Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:20:12 +0300 Subject: [PATCH 25/53] Remove AssignmentToAssignmentSavedResponseMapper --- ...gnmentToAssignmentSavedResponseMapper.java | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java deleted file mode 100644 index 9b47b7036..000000000 --- a/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentSavedResponseMapper.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.ays.assignment.model.mapper; - -import com.ays.assignment.model.Assignment; -import com.ays.assignment.model.dto.response.AssignmentSavedResponse; -import com.ays.common.model.mapper.BaseMapper; -import org.mapstruct.Mapper; -import org.mapstruct.factory.Mappers; - -/** - * UserToUserResponseMapper is an interface that defines the mapping between an {@link Assignment} and an {@link AssignmentSavedResponse}. - * This interface uses the MapStruct annotation @Mapper to generate an implementation of this interface at compile-time. - *

The class provides a static method {@code initialize()} that returns an instance of the generated mapper implementation. - *

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 AssignmentToAssignmentSavedResponseMapper extends BaseMapper { - - /** - * Initializes the mapper. - * - * @return the initialized mapper object. - */ - static AssignmentToAssignmentSavedResponseMapper initialize() { - return Mappers.getMapper(AssignmentToAssignmentSavedResponseMapper.class); - } - -} From 381a9beb966020bbcbf5028164687d3e5b492329 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:33:28 +0300 Subject: [PATCH 26/53] Remove AssignmentSavedResponseBuilder --- .../AssignmentSavedResponseBuilder.java | 43 ------------------- 1 file changed, 43 deletions(-) delete mode 100644 src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java diff --git a/src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java b/src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java deleted file mode 100644 index 45459faaf..000000000 --- a/src/test/java/com/ays/assignment/model/dto/response/AssignmentSavedResponseBuilder.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.ays.assignment.model.dto.response; - -import com.ays.common.model.AysPhoneNumber; -import com.ays.common.model.TestDataBuilder; - -public class AssignmentSavedResponseBuilder extends TestDataBuilder { - - public AssignmentSavedResponseBuilder() { - super(AssignmentSavedResponse.class); - } - - - public AssignmentSavedResponseBuilder withPhoneNumber(AysPhoneNumber phoneNumber) { - data.setPhoneNumber(phoneNumber); - return this; - } - - - public AssignmentSavedResponseBuilder withDescription(String description) { - data.setDescription(description); - return this; - } - - public AssignmentSavedResponseBuilder withFirstName(String firstName) { - data.setFirstName(firstName); - return this; - } - - public AssignmentSavedResponseBuilder withLastName(String lastName) { - data.setLastName(lastName); - return this; - } - - public AssignmentSavedResponseBuilder withLongitude(double longitude) { - data.setLongitude(longitude); - return this; - } - - public AssignmentSavedResponseBuilder withLatitude(double latitude) { - data.setLatitude(latitude); - return this; - } -} From 47392dd82810b074629dac0d8670bf8aa4eeacf6 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:33:47 +0300 Subject: [PATCH 27/53] Revise AssignmentEntity --- .../com/ays/assignment/model/entity/AssignmentEntity.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java index d795183e5..faf5104a6 100644 --- a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java +++ b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java @@ -95,15 +95,9 @@ public boolean isDone() { return AssignmentStatus.DONE.equals(this.status); } - public boolean isDeleted() { - return AssignmentStatus.DELETED.equals(this.status); - } public void updateAssignmentStatus(AssignmentStatus assignmentStatus) { this.status = assignmentStatus; } - public void deleteUser() { - this.status = AssignmentStatus.DELETED; - } } From bc24a59fe0710892fa4f6aa7169da36c34e49035 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:34:17 +0300 Subject: [PATCH 28/53] Revise AssignmentSaveService and AssignmentSaveServiceImpl --- .../service/AssignmentSaveService.java | 3 +- .../impl/AssignmentSaveServiceImpl.java | 36 ++----------------- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/ays/assignment/service/AssignmentSaveService.java b/src/main/java/com/ays/assignment/service/AssignmentSaveService.java index c4aacd476..9a87ff541 100644 --- a/src/main/java/com/ays/assignment/service/AssignmentSaveService.java +++ b/src/main/java/com/ays/assignment/service/AssignmentSaveService.java @@ -1,6 +1,5 @@ package com.ays.assignment.service; -import com.ays.assignment.model.Assignment; import com.ays.assignment.model.dto.request.AssignmentSaveRequest; /** @@ -14,5 +13,5 @@ public interface AssignmentSaveService { * @param saveRequest the AssignmentSaveRequest entity * @return Assignment */ - Assignment saveAssignment(AssignmentSaveRequest saveRequest); + void saveAssignment(AssignmentSaveRequest saveRequest); } diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java index b269d0c96..0e2cdd9d4 100644 --- a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java @@ -1,13 +1,11 @@ package com.ays.assignment.service.impl; -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.mapper.AssignmentSaveRequestToAssignmentMapper; import com.ays.assignment.repository.AssignmentRepository; import com.ays.assignment.service.AssignmentSaveService; import com.ays.auth.model.AysIdentity; -import com.ays.common.model.AysPhoneNumber; import com.ays.user.util.exception.AysUserAlreadyExistsByPhoneNumberException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -36,44 +34,14 @@ class AssignmentSaveServiceImpl implements AssignmentSaveService { * @throws AysUserAlreadyExistsByPhoneNumberException if a assignment with the same phone number already exists */ @Override - public Assignment saveAssignment(AssignmentSaveRequest saveRequest) { + public void saveAssignment(AssignmentSaveRequest saveRequest) { final List assignmentsFromDatabase = assignmentRepository.findAll(); - this.checkPhoneNumberExist(saveRequest.getPhoneNumber(), assignmentsFromDatabase); - final AssignmentEntity assignmentEntity = assignmentSaveRequestToAssignmentMapper.mapForSaving(saveRequest, identity.getInstitutionId()); - AssignmentEntity savedAssignment = assignmentRepository.save(assignmentEntity); - - return Assignment.builder() - .id(savedAssignment.getId()) - .description(saveRequest.getDescription()) - .firstName(saveRequest.getFirstName()) - .lastName(saveRequest.getLastName()) - .latitude(saveRequest.getLatitude()) - .longitude(saveRequest.getLongitude()) - .phoneNumber(saveRequest.getPhoneNumber()) - .build(); + assignmentRepository.save(assignmentEntity); } - /** - * Checks if a phone number already exists in the database. - * - * @param phoneNumber the phone number to check - * @param usersFromDatabase the list of assignment entities from the database - * @throws AysUserAlreadyExistsByPhoneNumberException if a user with the same phone number already exists - */ - private void checkPhoneNumberExist(final AysPhoneNumber phoneNumber, - final List usersFromDatabase) { - - final boolean isPhoneNumberExist = usersFromDatabase.stream() - .filter(userEntity -> userEntity.getCountryCode().equals(phoneNumber.getCountryCode())) - .anyMatch(userEntity -> userEntity.getLineNumber().equals(phoneNumber.getLineNumber())); - - if (isPhoneNumberExist) { - throw new AysUserAlreadyExistsByPhoneNumberException(phoneNumber); - } - } } From efbfd3fcffa44bdda1e350ce662ffd01c713d83e Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:35:22 +0300 Subject: [PATCH 29/53] Revise AssignmentControllerTest and AssignmentSaveServiceImplTest --- .../controller/AssignmentControllerTest.java | 44 +++---------------- .../impl/AssignmentSaveServiceImplTest.java | 38 +--------------- 2 files changed, 7 insertions(+), 75 deletions(-) diff --git a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java index f655c0fce..2a82570b5 100644 --- a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java +++ b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java @@ -1,13 +1,8 @@ package com.ays.assignment.controller; import com.ays.AbstractRestControllerTest; -import com.ays.assignment.model.Assignment; -import com.ays.assignment.model.AssignmentBuilder; import com.ays.assignment.model.dto.request.AssignmentSaveRequest; import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder; -import com.ays.assignment.model.dto.response.AssignmentSavedResponse; -import com.ays.assignment.model.enums.AssignmentStatus; -import com.ays.assignment.model.mapper.AssignmentToAssignmentSavedResponseMapper; import com.ays.assignment.service.AssignmentSaveService; import com.ays.common.model.AysPhoneNumberBuilder; import com.ays.common.model.dto.response.AysResponse; @@ -20,7 +15,6 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; -import org.springframework.test.web.servlet.result.MockMvcResultMatchers; class AssignmentControllerTest extends AbstractRestControllerTest { @@ -28,8 +22,6 @@ class AssignmentControllerTest extends AbstractRestControllerTest { @MockBean private AssignmentSaveService assignmentSaveService; - private final AssignmentToAssignmentSavedResponseMapper assignmentToAssignmentSavedResponseMapper = AssignmentToAssignmentSavedResponseMapper.initialize(); - private static final String BASE_PATH = "/api/v1"; @Test @@ -40,24 +32,13 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSav .build(); // When - Assignment mockAssignment = new AssignmentBuilder() - .withDescription(mockAssignmentSaveRequest.getDescription()) - .withFirstName(mockAssignmentSaveRequest.getFirstName()) - .withLastName(mockAssignmentSaveRequest.getLastName()) - .withPhoneNumber(mockAssignmentSaveRequest.getPhoneNumber()) - .withStatus(AssignmentStatus.AVAILABLE) - .build(); - - - Mockito.when(assignmentSaveService.saveAssignment(Mockito.any(AssignmentSaveRequest.class))) - .thenReturn(mockAssignment); + Mockito.doNothing().when(assignmentSaveService).saveAssignment(Mockito.any(AssignmentSaveRequest.class)); // Then String endpoint = BASE_PATH.concat("/assignment"); - AssignmentSavedResponse mockUserSavedResponse = assignmentToAssignmentSavedResponseMapper.map(mockAssignment); + AysResponse mockAysResponse = AysResponse.SUCCESS; - AysResponse mockResponse = AysResponseBuilder.successOf(mockUserSavedResponse); mockMvc.perform(AysMockMvcRequestBuilders .post(endpoint, mockAdminUserToken.getAccessToken(), mockAssignmentSaveRequest)) .andDo(MockMvcResultHandlers.print()) @@ -65,25 +46,11 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSav .andExpect(AysMockResultMatchersBuilders.time() .isNotEmpty()) .andExpect(AysMockResultMatchersBuilders.httpStatus() - .value(mockResponse.getHttpStatus().name())) + .value(mockAysResponse.getHttpStatus().getReasonPhrase())) .andExpect(AysMockResultMatchersBuilders.isSuccess() - .value(mockResponse.getIsSuccess())) + .value(mockAysResponse.getIsSuccess())) .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.description") - .value(mockResponse.getResponse().getDescription())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.firstName") - .value(mockResponse.getResponse().getFirstName())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.lastName") - .value(mockResponse.getResponse().getLastName())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.phoneNumber.countryCode") - .value(mockResponse.getResponse().getPhoneNumber().getCountryCode())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.phoneNumber.lineNumber") - .value(mockResponse.getResponse().getPhoneNumber().getLineNumber())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.latitude") - .value(mockResponse.getResponse().getLatitude())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.longitude") - .value(mockResponse.getResponse().getLongitude())) + .doesNotExist()); ; Mockito.verify(assignmentSaveService, Mockito.times(1)) @@ -116,4 +83,5 @@ void givenValidAssignmentSaveRequest_whenUserUnauthorizedForSaving_thenReturnAcc .andExpect(AysMockResultMatchersBuilders.response() .doesNotExist()); } + } \ No newline at end of file diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java index 56b9ae9fc..fd1efcce8 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -1,7 +1,6 @@ package com.ays.assignment.service.impl; import com.ays.AbstractUnitTest; -import com.ays.assignment.model.Assignment; import com.ays.assignment.model.dto.request.AssignmentSaveRequest; import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder; import com.ays.assignment.model.entity.AssignmentEntity; @@ -9,8 +8,6 @@ import com.ays.assignment.repository.AssignmentRepository; import com.ays.auth.model.AysIdentity; import com.ays.common.util.AysRandomUtil; -import com.ays.user.util.exception.AysUserAlreadyExistsByPhoneNumberException; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -55,43 +52,10 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment() .thenReturn(AysRandomUtil.generateUUID()); // Then - Assignment assignment = assignmentSaveService.saveAssignment(mockAssignmentSaveRequest); - - Assertions.assertEquals(mockAssignmentSaveRequest.getFirstName(), assignment.getFirstName()); - Assertions.assertEquals(mockAssignmentSaveRequest.getLastName(), assignment.getLastName()); - Assertions.assertEquals(mockAssignmentSaveRequest.getPhoneNumber().getLineNumber(), assignment.getPhoneNumber().getLineNumber()); - Assertions.assertEquals(mockAssignmentSaveRequest.getPhoneNumber().getCountryCode(), assignment.getPhoneNumber().getCountryCode()); - Assertions.assertEquals(mockAssignmentSaveRequest.getDescription(), assignment.getDescription()); - Assertions.assertEquals(mockAssignmentSaveRequest.getDescription(), assignment.getDescription()); - Assertions.assertEquals(mockAssignmentSaveRequest.getLongitude(), assignment.getLongitude()); - Assertions.assertEquals(mockAssignmentSaveRequest.getLatitude(), assignment.getLatitude()); + assignmentSaveService.saveAssignment(mockAssignmentSaveRequest); Mockito.verify(assignmentRepository, Mockito.times(1)).findAll(); Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); } - @Test - void givenAssignmentSaveRequestWithExistingPhoneNumber_whenAssignmentSaved_thenThrowAysUserAlreadyExistsByPhoneNumberException() { - - // Given - AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() - .withValidFields() - .build(); - - // When - List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidAssignmentEntities(10); - assignmentsFromDatabase.get(0).setCountryCode(mockAssignmentSaveRequest.getPhoneNumber().getCountryCode()); - assignmentsFromDatabase.get(0).setLineNumber(mockAssignmentSaveRequest.getPhoneNumber().getLineNumber()); - Mockito.when(assignmentRepository.findAll()) - .thenReturn(assignmentsFromDatabase); - - // Then - Assertions.assertThrows( - AysUserAlreadyExistsByPhoneNumberException.class, - () -> assignmentSaveService.saveAssignment(mockAssignmentSaveRequest) - ); - - Mockito.verify(assignmentRepository, Mockito.times(1)).findAll(); - } - } From 03c2d134d247bbcfc93c42e6f55a0808ce43ef35 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:36:19 +0300 Subject: [PATCH 30/53] Revise AssignmentSaveServiceImpl --- .../assignment/service/impl/AssignmentSaveServiceImpl.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java index 0e2cdd9d4..f771a3386 100644 --- a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java @@ -10,8 +10,6 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import java.util.List; - /** * UserSaveServiceImpl is an implementation of the {@link AssignmentSaveService} interface. * It provides methods for saving assignment data and performing related operations by admin. @@ -36,8 +34,6 @@ class AssignmentSaveServiceImpl implements AssignmentSaveService { @Override public void saveAssignment(AssignmentSaveRequest saveRequest) { - final List assignmentsFromDatabase = assignmentRepository.findAll(); - final AssignmentEntity assignmentEntity = assignmentSaveRequestToAssignmentMapper.mapForSaving(saveRequest, identity.getInstitutionId()); assignmentRepository.save(assignmentEntity); From 2c3d93ed90ba6bb267e3bfa51e3151a302f35b93 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:41:22 +0300 Subject: [PATCH 31/53] Revise AssignmentSaveServiceImplTest --- .../service/impl/AssignmentSaveServiceImplTest.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java index fd1efcce8..ad72fb303 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -13,8 +13,6 @@ import org.mockito.Mock; import org.mockito.Mockito; -import java.util.List; - class AssignmentSaveServiceImplTest extends AbstractUnitTest { @@ -28,7 +26,7 @@ class AssignmentSaveServiceImplTest extends AbstractUnitTest { private AysIdentity identity; @Test - void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment() { + void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnSucess() { // Given AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() @@ -36,14 +34,10 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment() .build(); - List assignmentsFromDatabase = AssignmentEntityBuilder.generateValidAssignmentEntities(10); - AssignmentEntity mockAssignmentEntity = new AssignmentEntityBuilder() .withValidFields().build(); // When - Mockito.when(assignmentRepository.findAll()) - .thenReturn(assignmentsFromDatabase); Mockito.when(assignmentRepository.save(Mockito.any(AssignmentEntity.class))) .thenReturn(mockAssignmentEntity); From 61f25014b1098e1f0298a1e7b1d58495d077ea40 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:41:33 +0300 Subject: [PATCH 32/53] Revise AssignmentSaveServiceImplTest --- .../assignment/service/impl/AssignmentSaveServiceImplTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java index ad72fb303..578fe3be7 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -26,7 +26,7 @@ class AssignmentSaveServiceImplTest extends AbstractUnitTest { private AysIdentity identity; @Test - void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnSucess() { + void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnSuccess() { // Given AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() From 0a4fb044d5472176ead872f7af63e77115055dc6 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 14:47:02 +0300 Subject: [PATCH 33/53] Revise AssignmentSaveServiceImplTest --- .../assignment/service/impl/AssignmentSaveServiceImplTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java index 578fe3be7..88415d66d 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -48,7 +48,6 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnSuccess() { // Then assignmentSaveService.saveAssignment(mockAssignmentSaveRequest); - Mockito.verify(assignmentRepository, Mockito.times(1)).findAll(); Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); } From ac91a2d8666b66961dafc263a1ad60bd9b407ca9 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 15:02:13 +0300 Subject: [PATCH 34/53] Revise AssignmentEntity --- .../java/com/ays/assignment/model/entity/AssignmentEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java index faf5104a6..f3fd40cb9 100644 --- a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java +++ b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java @@ -57,7 +57,7 @@ public class AssignmentEntity extends BaseEntity { private Point point; @Enumerated(EnumType.STRING) - @Column(name = "ASSIGNMENT_STATUS") + @Column(name = "STATUS") private AssignmentStatus status; @OneToOne From 25b79e37af6c2eeb03ce1b8b191b2a153d4df5e2 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 15:30:52 +0300 Subject: [PATCH 35/53] Revise AssignmentEntity and AssignmentSaveServiceImpl --- .../java/com/ays/assignment/model/entity/AssignmentEntity.java | 1 + .../ays/assignment/service/impl/AssignmentSaveServiceImpl.java | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java index f3fd40cb9..2b9c38115 100644 --- a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java +++ b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java @@ -73,6 +73,7 @@ public void setPoint(double latitude, double longitude) { CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); GeometryFactory geometryFactory = new GeometryFactory(); this.point = geometryFactory.createPoint(coordinateSequence); + this.point.setSRID(4326); // Set the SRID of the created Point } public boolean isAvailable() { diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java index f771a3386..b4d9c8182 100644 --- a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java @@ -36,6 +36,8 @@ public void saveAssignment(AssignmentSaveRequest saveRequest) { final AssignmentEntity assignmentEntity = assignmentSaveRequestToAssignmentMapper.mapForSaving(saveRequest, identity.getInstitutionId()); + assignmentEntity.setPoint(saveRequest.getLatitude(), saveRequest.getLongitude()); + assignmentRepository.save(assignmentEntity); } From 1bc421e6c30ca1f92ced87e92df352b742f112d2 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 15:31:23 +0300 Subject: [PATCH 36/53] Revise some files for Liquibase --- .../db/changelog/changes/2-ays-dml.xml | 17 +++++++++++++++++ .../db/changelog/changes/4-ays-ddl.xml | 2 +- .../db/changelog/changes/5-ays-dml.xml | 12 +++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/resources/db/changelog/changes/2-ays-dml.xml b/src/main/resources/db/changelog/changes/2-ays-dml.xml index 20df7134d..dd2cd26a6 100644 --- a/src/main/resources/db/changelog/changes/2-ays-dml.xml +++ b/src/main/resources/db/changelog/changes/2-ays-dml.xml @@ -63,6 +63,23 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/db/changelog/changes/4-ays-ddl.xml b/src/main/resources/db/changelog/changes/4-ays-ddl.xml index c4b966b30..0d40a6c87 100644 --- a/src/main/resources/db/changelog/changes/4-ays-ddl.xml +++ b/src/main/resources/db/changelog/changes/4-ays-ddl.xml @@ -56,7 +56,7 @@ diff --git a/src/main/resources/db/changelog/changes/5-ays-dml.xml b/src/main/resources/db/changelog/changes/5-ays-dml.xml index 23909b4f0..d316ccd81 100644 --- a/src/main/resources/db/changelog/changes/5-ays-dml.xml +++ b/src/main/resources/db/changelog/changes/5-ays-dml.xml @@ -25,21 +25,27 @@ INSERT INTO AYS_ASSIGNMENT (ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, LINE_NUMBER, STATUS, + INSTITUTION_ID, POINT, CREATED_USER, CREATED_AT) VALUES ('3b30cac9-0645-48cb-bc98-2e928df521ab', '100L İçme Suyu İhtiyacı', 'Ahmet', 'Mehmet', - 90, 1234567890, 'AVAILABLE', ST_GeomFromText('POINT(40.981210 29.026083)', 4326), + 90, 1234567890, 'AVAILABLE', '77ece256-bf0e-4bbe-801d-173083f8bdcf', + ST_GeomFromText('POINT(40.981210 29.026083)', 4326), 'AYS', CURRENT_TIMESTAMP); INSERT INTO AYS_ASSIGNMENT (ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, LINE_NUMBER, STATUS, + INSTITUTION_ID, POINT, CREATED_USER, CREATED_AT) VALUES ('a9ec051e-3c4f-4cb5-ab08-e9dcee5e1f03', 'Kıyafet İhtiyacı', 'Cem', 'Orkun', - 90, 1234567890, 'AVAILABLE', ST_GeomFromText('POINT(40.981210 29.000000)', 4326), + 90, 1234567890, 'AVAILABLE', '7024b566-6df9-445c-8526-ff43d3d0ba9e', + ST_GeomFromText('POINT(40.981210 29.000000)', 4326), 'AYS', CURRENT_TIMESTAMP); INSERT INTO AYS_ASSIGNMENT (ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, LINE_NUMBER, STATUS, + INSTITUTION_ID, POINT, CREATED_USER, CREATED_AT) VALUES ('7e1ebd41-a644-4596-a0f3-b4a71237b3c5', 'Yemek İhtiyacı', 'Ceren', 'Deniz', - 90, 1234567890, 'AVAILABLE', ST_GeomFromText('POINT(40.941210 29.123123)', 4326), + 90, 1234567890, 'AVAILABLE', '705b4fd8-0ec5-41cd-b75a-0ed7aa9d853b', + ST_GeomFromText('POINT(40.941210 29.123123)', 4326), 'AYS', CURRENT_TIMESTAMP); From 6be7f7c2cadc85371daa1d86fdb71906054dc037 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 17:45:54 +0300 Subject: [PATCH 37/53] Revise AssignmentSaveServiceImpl and AssignmentSaveRequestToAssignmentMapper --- .../mapper/AssignmentSaveRequestToAssignmentMapper.java | 6 +++++- .../assignment/service/impl/AssignmentSaveServiceImpl.java | 2 -- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java index 3ff8fead7..e4deabf75 100644 --- a/src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java +++ b/src/main/java/com/ays/assignment/model/mapper/AssignmentSaveRequestToAssignmentMapper.java @@ -29,7 +29,7 @@ public interface AssignmentSaveRequestToAssignmentMapper extends BaseMapper Date: Sat, 19 Aug 2023 22:11:49 +0300 Subject: [PATCH 38/53] Revise AssignmentSaveRequest --- .../assignment/model/dto/request/AssignmentSaveRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java index e957290ab..c98332684 100644 --- a/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java +++ b/src/main/java/com/ays/assignment/model/dto/request/AssignmentSaveRequest.java @@ -36,8 +36,8 @@ public class AssignmentSaveRequest { private AysPhoneNumber phoneNumber; @NotNull - private double latitude; + private Double latitude; @NotNull - private double longitude; + private Double longitude; } From 777e2ffbb474d230f31cb5bcd13a7e20f7c65497 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 22:14:01 +0300 Subject: [PATCH 39/53] Revise xml files for liquibase --- .../resources/db/changelog/changes/2-ays-dml.xml | 15 --------------- .../resources/db/changelog/changes/4-ays-ddl.xml | 16 ++++++++-------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/main/resources/db/changelog/changes/2-ays-dml.xml b/src/main/resources/db/changelog/changes/2-ays-dml.xml index dd2cd26a6..c16289064 100644 --- a/src/main/resources/db/changelog/changes/2-ays-dml.xml +++ b/src/main/resources/db/changelog/changes/2-ays-dml.xml @@ -64,21 +64,6 @@ - - - - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog/changes/4-ays-ddl.xml b/src/main/resources/db/changelog/changes/4-ays-ddl.xml index 0d40a6c87..56da71ab4 100644 --- a/src/main/resources/db/changelog/changes/4-ays-ddl.xml +++ b/src/main/resources/db/changelog/changes/4-ays-ddl.xml @@ -47,17 +47,17 @@ - - From 6c9075af20f044955b4c3beff74bbb7c9b01594a Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 22:20:21 +0300 Subject: [PATCH 40/53] Revise xml file for liquibase --- .../db/changelog/changes/5-ays-dml.xml | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/resources/db/changelog/changes/5-ays-dml.xml b/src/main/resources/db/changelog/changes/5-ays-dml.xml index d316ccd81..eb74c46ec 100644 --- a/src/main/resources/db/changelog/changes/5-ays-dml.xml +++ b/src/main/resources/db/changelog/changes/5-ays-dml.xml @@ -24,29 +24,23 @@ - INSERT INTO AYS_ASSIGNMENT (ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, LINE_NUMBER, STATUS, - INSTITUTION_ID, + INSERT INTO AYS_ASSIGNMENT (ID, INSTITUTION_ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, + LINE_NUMBER, STATUS, POINT, CREATED_USER, CREATED_AT) - VALUES ('3b30cac9-0645-48cb-bc98-2e928df521ab', '100L İçme Suyu İhtiyacı', 'Ahmet', 'Mehmet', - 90, 1234567890, 'AVAILABLE', '77ece256-bf0e-4bbe-801d-173083f8bdcf', + VALUES ('3b30cac9-0645-48cb-bc98-2e928df521ab', '77ece256-bf0e-4bbe-801d-173083f8bdcf', + '100L İçme Suyu İhtiyacı', + 'Ahmet', 'Mehmet', 90, 1234567890, 'AVAILABLE', ST_GeomFromText('POINT(40.981210 29.026083)', 4326), 'AYS', CURRENT_TIMESTAMP); - INSERT INTO AYS_ASSIGNMENT (ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, LINE_NUMBER, STATUS, - INSTITUTION_ID, + INSERT INTO AYS_ASSIGNMENT (ID, INSTITUTION_ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, + LINE_NUMBER, STATUS, POINT, CREATED_USER, CREATED_AT) - VALUES ('a9ec051e-3c4f-4cb5-ab08-e9dcee5e1f03', 'Kıyafet İhtiyacı', 'Cem', 'Orkun', - 90, 1234567890, 'AVAILABLE', '7024b566-6df9-445c-8526-ff43d3d0ba9e', + VALUES ('a9ec051e-3c4f-4cb5-ab08-e9dcee5e1f03', '91df7ae9-d5b9-44ae-b54f-d5d55359c4a4', 'Kıyafet İhtiyacı', + 'Cem', 'Orkun', 90, 1234567890, 'AVAILABLE', ST_GeomFromText('POINT(40.981210 29.000000)', 4326), 'AYS', CURRENT_TIMESTAMP); - INSERT INTO AYS_ASSIGNMENT (ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, LINE_NUMBER, STATUS, - INSTITUTION_ID, - POINT, CREATED_USER, CREATED_AT) - VALUES ('7e1ebd41-a644-4596-a0f3-b4a71237b3c5', 'Yemek İhtiyacı', 'Ceren', 'Deniz', - 90, 1234567890, 'AVAILABLE', '705b4fd8-0ec5-41cd-b75a-0ed7aa9d853b', - ST_GeomFromText('POINT(40.941210 29.123123)', 4326), - 'AYS', CURRENT_TIMESTAMP); From 02f71016d5a74b86ef8dd9e8b5b915404a899c17 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 22:21:00 +0300 Subject: [PATCH 41/53] Revise AssignmentControllerTest --- .../com/ays/assignment/controller/AssignmentControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java index 2a82570b5..df3f7719b 100644 --- a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java +++ b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java @@ -84,4 +84,4 @@ void givenValidAssignmentSaveRequest_whenUserUnauthorizedForSaving_thenReturnAcc .doesNotExist()); } -} \ No newline at end of file +} From a46a080077495d0ad6e487c726bc1bb70b5415fb Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 22:22:14 +0300 Subject: [PATCH 42/53] Revise AssignmentSaveRequestBuilder --- .../model/dto/request/AssignmentSaveRequestBuilder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java b/src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java index cf6c5b23a..2f24a443c 100644 --- a/src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java +++ b/src/test/java/com/ays/assignment/model/dto/request/AssignmentSaveRequestBuilder.java @@ -40,12 +40,12 @@ public AssignmentSaveRequestBuilder withLastName(String lastName) { return this; } - public AssignmentSaveRequestBuilder withLatitude(double latitude) { + public AssignmentSaveRequestBuilder withLatitude(Double latitude) { data.setLatitude(latitude); return this; } - public AssignmentSaveRequestBuilder withLongitude(double longitude) { + public AssignmentSaveRequestBuilder withLongitude(Double longitude) { data.setLongitude(longitude); return this; } From 0b0ec65bc07ef47d8650414ee6a81d8fb6927421 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 22:24:52 +0300 Subject: [PATCH 43/53] Revise AssignmentSaveServiceImpl --- .../ays/assignment/service/impl/AssignmentSaveServiceImpl.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java index f771a3386..0fe3655bd 100644 --- a/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentSaveServiceImpl.java @@ -6,7 +6,6 @@ import com.ays.assignment.repository.AssignmentRepository; import com.ays.assignment.service.AssignmentSaveService; import com.ays.auth.model.AysIdentity; -import com.ays.user.util.exception.AysUserAlreadyExistsByPhoneNumberException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -29,7 +28,6 @@ class AssignmentSaveServiceImpl implements AssignmentSaveService { * * @param saveRequest the request object containing assignment data to be saved * @return the saved assignment - * @throws AysUserAlreadyExistsByPhoneNumberException if a assignment with the same phone number already exists */ @Override public void saveAssignment(AssignmentSaveRequest saveRequest) { From 9bbcbe6ef545f49856d4d00ab04d2da0fe4f2d3a Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 22:36:40 +0300 Subject: [PATCH 44/53] Revise AssignmentSaveServiceImplTest --- .../assignment/service/impl/AssignmentSaveServiceImplTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java index 88415d66d..991baaf93 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentSaveServiceImplTest.java @@ -48,7 +48,9 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnSuccess() { // Then assignmentSaveService.saveAssignment(mockAssignmentSaveRequest); + Mockito.verify(assignmentRepository, Mockito.times(1)).save(Mockito.any(AssignmentEntity.class)); Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); + } } From e9983a3fa8ec296782d5b9e04b9ddbfa264f70aa Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sat, 19 Aug 2023 22:37:17 +0300 Subject: [PATCH 45/53] Revise AssignmentRepository --- .../assignment/repository/AssignmentRepository.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/com/ays/assignment/repository/AssignmentRepository.java b/src/main/java/com/ays/assignment/repository/AssignmentRepository.java index 79e843333..7f885974c 100644 --- a/src/main/java/com/ays/assignment/repository/AssignmentRepository.java +++ b/src/main/java/com/ays/assignment/repository/AssignmentRepository.java @@ -4,20 +4,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; -import java.util.Optional; - /** * Repository interface for performing CRUD operations on UserAssignmentEntity objects. */ public interface AssignmentRepository extends JpaRepository, JpaSpecificationExecutor { - /** - * Retrieves an optional AssignmentEntity based on the provided user ID and institution ID. - * - * @param id The ID of the assignment to retrieve. - * @param institutionId The ID of the institution associated with the assignment. - * @return An optional AssignmentEntity that matches the specified ID and institution ID, or an empty optional if not found. - */ - Optional findByIdAndInstitutionId(String id, String institutionId); } From 7d604dc30dd4b2a3394f19cf61b69e19d2d6fabb Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sun, 20 Aug 2023 16:34:30 +0300 Subject: [PATCH 46/53] Revise files for Liquibase --- src/main/resources/db/changelog/changes/2-ays-dml.xml | 2 -- src/main/resources/db/changelog/changes/4-ays-ddl.xml | 4 ++-- src/main/resources/db/changelog/changes/5-ays-dml.xml | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/resources/db/changelog/changes/2-ays-dml.xml b/src/main/resources/db/changelog/changes/2-ays-dml.xml index 6c4218d07..98111d50e 100644 --- a/src/main/resources/db/changelog/changes/2-ays-dml.xml +++ b/src/main/resources/db/changelog/changes/2-ays-dml.xml @@ -63,8 +63,6 @@ - - diff --git a/src/main/resources/db/changelog/changes/4-ays-ddl.xml b/src/main/resources/db/changelog/changes/4-ays-ddl.xml index 56da71ab4..977e1ee77 100644 --- a/src/main/resources/db/changelog/changes/4-ays-ddl.xml +++ b/src/main/resources/db/changelog/changes/4-ays-ddl.xml @@ -69,10 +69,10 @@ - + - + diff --git a/src/main/resources/db/changelog/changes/5-ays-dml.xml b/src/main/resources/db/changelog/changes/5-ays-dml.xml index eb74c46ec..61d8b1c80 100644 --- a/src/main/resources/db/changelog/changes/5-ays-dml.xml +++ b/src/main/resources/db/changelog/changes/5-ays-dml.xml @@ -29,7 +29,7 @@ POINT, CREATED_USER, CREATED_AT) VALUES ('3b30cac9-0645-48cb-bc98-2e928df521ab', '77ece256-bf0e-4bbe-801d-173083f8bdcf', '100L İçme Suyu İhtiyacı', - 'Ahmet', 'Mehmet', 90, 1234567890, 'AVAILABLE', + 'Ahmet', 'Mehmet', '90', '1234567890', 'AVAILABLE', ST_GeomFromText('POINT(40.981210 29.026083)', 4326), 'AYS', CURRENT_TIMESTAMP); @@ -37,7 +37,7 @@ LINE_NUMBER, STATUS, POINT, CREATED_USER, CREATED_AT) VALUES ('a9ec051e-3c4f-4cb5-ab08-e9dcee5e1f03', '91df7ae9-d5b9-44ae-b54f-d5d55359c4a4', 'Kıyafet İhtiyacı', - 'Cem', 'Orkun', 90, 1234567890, 'AVAILABLE', + 'Cem', 'Orkun', '90', '1234567890', 'AVAILABLE', ST_GeomFromText('POINT(40.981210 29.000000)', 4326), 'AYS', CURRENT_TIMESTAMP); From 7af3f06e1d60132f38fa4b31285bef73800afa7c Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sun, 20 Aug 2023 16:34:51 +0300 Subject: [PATCH 47/53] Revise AssignmentControllerTest --- .../ays/assignment/controller/AssignmentControllerTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java index df3f7719b..d021d460c 100644 --- a/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java +++ b/src/test/java/com/ays/assignment/controller/AssignmentControllerTest.java @@ -51,10 +51,10 @@ void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSav .value(mockAysResponse.getIsSuccess())) .andExpect(AysMockResultMatchersBuilders.response() .doesNotExist()); - ; Mockito.verify(assignmentSaveService, Mockito.times(1)) .saveAssignment(Mockito.any(AssignmentSaveRequest.class)); + } @@ -82,6 +82,7 @@ void givenValidAssignmentSaveRequest_whenUserUnauthorizedForSaving_thenReturnAcc .value(mockResponse.getIsSuccess())) .andExpect(AysMockResultMatchersBuilders.response() .doesNotExist()); + } } From 336a48445fb6db71eccc2ec5beff8f9a5b8c4718 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sun, 20 Aug 2023 16:35:04 +0300 Subject: [PATCH 48/53] Define AssignmentSystemTest --- .../controller/AssignmentSystemTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/test/java/com/ays/assignment/controller/AssignmentSystemTest.java diff --git a/src/test/java/com/ays/assignment/controller/AssignmentSystemTest.java b/src/test/java/com/ays/assignment/controller/AssignmentSystemTest.java new file mode 100644 index 000000000..6802985f6 --- /dev/null +++ b/src/test/java/com/ays/assignment/controller/AssignmentSystemTest.java @@ -0,0 +1,76 @@ +package com.ays.assignment.controller; + +import com.ays.AbstractSystemTest; +import com.ays.assignment.model.dto.request.AssignmentSaveRequest; +import com.ays.assignment.model.dto.request.AssignmentSaveRequestBuilder; +import com.ays.common.model.AysPhoneNumberBuilder; +import com.ays.common.model.dto.response.AysResponse; +import com.ays.common.model.dto.response.AysResponseBuilder; +import com.ays.common.util.exception.model.AysError; +import com.ays.util.AysMockMvcRequestBuilders; +import com.ays.util.AysMockResultMatchersBuilders; +import org.junit.jupiter.api.Test; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; + +class AssignmentSystemTest extends AbstractSystemTest { + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignmentSavedResponse() throws Exception { + // Given + AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() + .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) + .build(); + + + // Then + String endpoint = BASE_PATH.concat("/assignment"); + + AysResponse mockAysResponse = AysResponse.SUCCESS; + + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), mockAssignmentSaveRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockAysResponse.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockAysResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + } + + + @Test + void givenValidAssignmentSaveRequest_whenUserUnauthorizedForSaving_thenReturnAccessDeniedException() throws Exception { + // Given + AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder() + .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/assignment"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, userTokenOne.getAccessToken(), mockAssignmentSaveRequest); + + AysResponse mockResponse = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + } + +} From ff65feb502b6210c349e17fa14c8a1918be42855 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Sun, 20 Aug 2023 16:40:32 +0300 Subject: [PATCH 49/53] Define file for Liquibase --- src/main/resources/db/changelog/changes/4-ays-ddl.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/db/changelog/changes/4-ays-ddl.xml b/src/main/resources/db/changelog/changes/4-ays-ddl.xml index 977e1ee77..307cbba10 100644 --- a/src/main/resources/db/changelog/changes/4-ays-ddl.xml +++ b/src/main/resources/db/changelog/changes/4-ays-ddl.xml @@ -69,10 +69,10 @@ - + - + From 6f8ab8dc8388a6c15e1bd6f9bfa794cb871e7907 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Mon, 21 Aug 2023 00:55:23 +0300 Subject: [PATCH 50/53] Revise AssignmentEntity --- .../assignment/model/entity/AssignmentEntity.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java index 2b9c38115..e2ba0e000 100644 --- a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java +++ b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java @@ -10,10 +10,7 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; -import org.locationtech.jts.geom.Coordinate; -import org.locationtech.jts.geom.CoordinateSequence; -import org.locationtech.jts.geom.GeometryFactory; -import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.*; import org.locationtech.jts.geom.impl.CoordinateArraySequence; /** @@ -69,11 +66,10 @@ public class AssignmentEntity extends BaseEntity { private UserEntity user; public void setPoint(double latitude, double longitude) { - Coordinate[] coordinates = new Coordinate[]{new Coordinate(latitude, longitude)}; - CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); - GeometryFactory geometryFactory = new GeometryFactory(); - this.point = geometryFactory.createPoint(coordinateSequence); - this.point.setSRID(4326); // Set the SRID of the created Point + final Coordinate[] coordinates = new Coordinate[]{new Coordinate(latitude, longitude)}; + final CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); + final PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING); + this.point = new GeometryFactory(precisionModel, 4326).createPoint(coordinateSequence); } public boolean isAvailable() { From 6b68522026b43a314f50a74612c0a7c08b59718d Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Mon, 21 Aug 2023 00:57:52 +0300 Subject: [PATCH 51/53] Revise AssignmentToAssignmentEntityMapper --- .../mapper/AssignmentToAssignmentEntityMapper.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentEntityMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentEntityMapper.java index b2efb9547..ce95ab640 100644 --- a/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentEntityMapper.java +++ b/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentEntityMapper.java @@ -3,9 +3,8 @@ import com.ays.assignment.model.Assignment; import com.ays.assignment.model.entity.AssignmentEntity; import com.ays.common.model.mapper.BaseMapper; -import org.locationtech.jts.geom.Coordinate; -import org.locationtech.jts.geom.GeometryFactory; -import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.*; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.factory.Mappers; @@ -48,9 +47,10 @@ static AssignmentToAssignmentEntityMapper initialize() { */ default Point mapToPoint(Double latitude, Double longitude) { if (latitude != null && longitude != null) { - Coordinate coordinate = new Coordinate(latitude, longitude); - GeometryFactory geometryFactory = new GeometryFactory(); - return geometryFactory.createPoint(coordinate); + final Coordinate[] coordinates = new Coordinate[]{new Coordinate(latitude, longitude)}; + final CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); + final PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING); + return new GeometryFactory(precisionModel, 4326).createPoint(coordinateSequence); } return null; } From 05c2cf09ce92b993caba3700b240f5ffc65c6b34 Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Mon, 21 Aug 2023 20:53:38 +0300 Subject: [PATCH 52/53] Create AysLocationUtil --- .../com/ays/common/util/AysLocationUtil.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/com/ays/common/util/AysLocationUtil.java diff --git a/src/main/java/com/ays/common/util/AysLocationUtil.java b/src/main/java/com/ays/common/util/AysLocationUtil.java new file mode 100644 index 000000000..c618bb882 --- /dev/null +++ b/src/main/java/com/ays/common/util/AysLocationUtil.java @@ -0,0 +1,28 @@ +package com.ays.common.util; + +import lombok.experimental.UtilityClass; +import org.locationtech.jts.geom.*; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; + +/** + * Utility class for handling location-related operations. + * Provides methods to generate geometric points based on latitude and longitude coordinates. + */ +@UtilityClass +public class AysLocationUtil { + + /** + * Generates a Point object representing a location based on the provided latitude and longitude coordinates. + * + * @param latitude The latitude coordinate of the location. + * @param longitude The longitude coordinate of the location. + * @return A Point object representing the location. + */ + public static Point generatePoint(final Double latitude, final Double longitude) { + final Coordinate[] coordinates = new Coordinate[]{new Coordinate(latitude, longitude)}; + final CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); + final PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING); + return new GeometryFactory(precisionModel, 4326).createPoint(coordinateSequence); + } + +} From 8cf1a3ec8da575121ea2aa1222cf3d03d527764d Mon Sep 17 00:00:00 2001 From: "DESKTOP-E7L6HLO\\Noyan" Date: Mon, 21 Aug 2023 20:54:18 +0300 Subject: [PATCH 53/53] Revise Assignment, AssignmentEntity, AssignmentSaveRequestToAssignmentMapper, AssignmentSaveRequestToAssignmentMapper, AssignmentToAssignmentEntityMapper --- .../com/ays/assignment/model/Assignment.java | 4 +-- .../model/entity/AssignmentEntity.java | 15 ++++++---- .../AssignmentEntityToAssignmentMapper.java | 29 ------------------- ...signmentSaveRequestToAssignmentMapper.java | 9 ++---- .../AssignmentToAssignmentEntityMapper.java | 28 ------------------ 5 files changed, 14 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/ays/assignment/model/Assignment.java b/src/main/java/com/ays/assignment/model/Assignment.java index f32045701..7ab447ecb 100644 --- a/src/main/java/com/ays/assignment/model/Assignment.java +++ b/src/main/java/com/ays/assignment/model/Assignment.java @@ -8,6 +8,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.SuperBuilder; +import org.locationtech.jts.geom.Point; /** * Assignment Domain Model to perform data transfer from service layer to controller @@ -22,8 +23,7 @@ public class Assignment extends BaseDomainModel { private String firstName; private String lastName; private AysPhoneNumber phoneNumber; - private Double latitude; - private Double longitude; + private Point point; private AssignmentStatus status; private User user; diff --git a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java index e2ba0e000..d1ae8afd9 100644 --- a/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java +++ b/src/main/java/com/ays/assignment/model/entity/AssignmentEntity.java @@ -2,6 +2,7 @@ import com.ays.assignment.model.enums.AssignmentStatus; import com.ays.common.model.entity.BaseEntity; +import com.ays.common.util.AysLocationUtil; import com.ays.institution.model.entity.InstitutionEntity; import com.ays.user.model.entity.UserEntity; import jakarta.persistence.*; @@ -10,8 +11,7 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; -import org.locationtech.jts.geom.*; -import org.locationtech.jts.geom.impl.CoordinateArraySequence; +import org.locationtech.jts.geom.Point; /** * Assignment entity, which holds the information regarding assignment. @@ -66,10 +66,7 @@ public class AssignmentEntity extends BaseEntity { private UserEntity user; public void setPoint(double latitude, double longitude) { - final Coordinate[] coordinates = new Coordinate[]{new Coordinate(latitude, longitude)}; - final CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); - final PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING); - this.point = new GeometryFactory(precisionModel, 4326).createPoint(coordinateSequence); + this.point = AysLocationUtil.generatePoint(latitude, longitude); } public boolean isAvailable() { @@ -97,4 +94,10 @@ public void updateAssignmentStatus(AssignmentStatus assignmentStatus) { this.status = assignmentStatus; } + public abstract static class AssignmentEntityBuilder> extends BaseEntity.BaseEntityBuilder { + public B point(final Double latitude, final Double longitude) { + this.point = AysLocationUtil.generatePoint(latitude, longitude); + return this.self(); + } + } } diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java index 45113a863..2959e2296 100644 --- a/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java +++ b/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java @@ -3,13 +3,10 @@ import com.ays.assignment.model.Assignment; import com.ays.assignment.model.entity.AssignmentEntity; import com.ays.common.model.mapper.BaseMapper; -import org.locationtech.jts.geom.Point; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.factory.Mappers; -import java.util.Optional; - /** * AssignmentEntityToAssignmentMapper is an interface that defines the mapping between an {@link AssignmentEntity} and an {@link Assignment}. * This interface uses the MapStruct annotation @Mapper to generate an implementation of this interface at compile-time. @@ -23,8 +20,6 @@ public interface AssignmentEntityToAssignmentMapper extends BaseMapper { - /** - * Maps UserAssignment to UserAssignmentEntity. - * - * @param assignment the UserAssignment object - * @return the mapped UserAssignmentEntity object - */ - @Mapping(target = "point", expression = "java(mapToPoint(assignment.getLatitude(), assignment.getLongitude()))") - AssignmentEntity map(Assignment assignment); - /** * Initializes the mapper. @@ -38,20 +26,4 @@ static AssignmentToAssignmentEntityMapper initialize() { return Mappers.getMapper(AssignmentToAssignmentEntityMapper.class); } - /** - * Maps latitude and longitude to the point field. - * - * @param latitude the latitude value - * @param longitude the longitude value - * @return the mapped Point object - */ - default Point mapToPoint(Double latitude, Double longitude) { - if (latitude != null && longitude != null) { - final Coordinate[] coordinates = new Coordinate[]{new Coordinate(latitude, longitude)}; - final CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates); - final PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING); - return new GeometryFactory(precisionModel, 4326).createPoint(coordinateSequence); - } - return null; - } }