Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#146 | Implement the process of Assignment for Save Operation #147

Merged
merged 54 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
4ff9a02
Revise AssignmentRepository
Rapter1990 Aug 15, 2023
de64acf
Define AssignmentSaveRequest and AssignmentUpdateRequest
Rapter1990 Aug 15, 2023
33d9b8a
Define AssignmentSaveService and AssignmentService
Rapter1990 Aug 15, 2023
9be6471
Implement Builder classes for Assignment
Rapter1990 Aug 15, 2023
0c948c1
Define AssignmentSaveRequestToAssignmentMapper and AssignmentToAssign…
Rapter1990 Aug 15, 2023
c0e3c41
Define AssignmentSavedResponse
Rapter1990 Aug 15, 2023
69c54b2
Define AssignmentSaveServiceImpl and AssignmentServiceImpl
Rapter1990 Aug 15, 2023
231d567
Define AssignmentController
Rapter1990 Aug 15, 2023
af8aa68
Implement AssignmentControllerTest for Save Assignment Process
Rapter1990 Aug 15, 2023
75276e0
Revise AssignmentBuilder
Rapter1990 Aug 15, 2023
ff42859
Define AssignmentSaveServiceImplTest
Rapter1990 Aug 15, 2023
d683c9b
Revise AysAssignmentAlreadyDeletedException
Rapter1990 Aug 15, 2023
867fb66
Revise AysAssignmentAlreadyAvailableException
Rapter1990 Aug 15, 2023
646aa42
Revise AssignmentEntity, AssignmentEntityBuilder, AssignmentRepositor…
Rapter1990 Aug 15, 2023
163bf79
Remove AssignmentService and AssignmentServiceImpl and Revise Assignm…
Rapter1990 Aug 15, 2023
7a4dd01
Revise AssignmentEntityBuilder and AssignmentSaveServiceImplTest
Rapter1990 Aug 15, 2023
05de41b
Revise AssignmentEntityBuilder
Rapter1990 Aug 15, 2023
6fb497d
Revise AssignmentEntityBuilder and AssignmentEntity
Rapter1990 Aug 15, 2023
780e3df
Revise AssignmentEntityBuilder, AssignmentSaveServiceImplTest, TestBu…
Rapter1990 Aug 16, 2023
e72aa88
Revise AssignmentControllerTest
Rapter1990 Aug 16, 2023
29fe104
Update AssignmentSaveRequest
Rapter1990 Aug 19, 2023
fe7045c
Update AssignmentSaveRequest and AssignmentController
Rapter1990 Aug 19, 2023
f880628
Remove AssignmentSavedResponse and AssignmentUpdateRequest
Rapter1990 Aug 19, 2023
bb8daea
Remove AssignmentStatus
Rapter1990 Aug 19, 2023
af3c4fa
Remove AssignmentToAssignmentSavedResponseMapper
Rapter1990 Aug 19, 2023
381a9be
Remove AssignmentSavedResponseBuilder
Rapter1990 Aug 19, 2023
47392dd
Revise AssignmentEntity
Rapter1990 Aug 19, 2023
bc24a59
Revise AssignmentSaveService and AssignmentSaveServiceImpl
Rapter1990 Aug 19, 2023
efbfd3f
Revise AssignmentControllerTest and AssignmentSaveServiceImplTest
Rapter1990 Aug 19, 2023
03c2d13
Revise AssignmentSaveServiceImpl
Rapter1990 Aug 19, 2023
2c3d93e
Revise AssignmentSaveServiceImplTest
Rapter1990 Aug 19, 2023
61f2501
Revise AssignmentSaveServiceImplTest
Rapter1990 Aug 19, 2023
0a4fb04
Revise AssignmentSaveServiceImplTest
Rapter1990 Aug 19, 2023
ac91a2d
Revise AssignmentEntity
Rapter1990 Aug 19, 2023
25b79e3
Revise AssignmentEntity and AssignmentSaveServiceImpl
Rapter1990 Aug 19, 2023
1bc421e
Revise some files for Liquibase
Rapter1990 Aug 19, 2023
6be7f7c
Revise AssignmentSaveServiceImpl and AssignmentSaveRequestToAssignmen…
Rapter1990 Aug 19, 2023
18c3bee
Revise AssignmentSaveRequest
Rapter1990 Aug 19, 2023
777e2ff
Revise xml files for liquibase
Rapter1990 Aug 19, 2023
6c9075a
Revise xml file for liquibase
Rapter1990 Aug 19, 2023
02f7101
Revise AssignmentControllerTest
Rapter1990 Aug 19, 2023
a46a080
Revise AssignmentSaveRequestBuilder
Rapter1990 Aug 19, 2023
0b0ec65
Revise AssignmentSaveServiceImpl
Rapter1990 Aug 19, 2023
9bbcbe6
Revise AssignmentSaveServiceImplTest
Rapter1990 Aug 19, 2023
e9983a3
Revise AssignmentRepository
Rapter1990 Aug 19, 2023
b7fe287
Merge remote-tracking branch 'origin/main' into feature/146/assignmen…
Rapter1990 Aug 20, 2023
7d604dc
Revise files for Liquibase
Rapter1990 Aug 20, 2023
7af3f06
Revise AssignmentControllerTest
Rapter1990 Aug 20, 2023
336a484
Define AssignmentSystemTest
Rapter1990 Aug 20, 2023
ff65feb
Define file for Liquibase
Rapter1990 Aug 20, 2023
6f8ab8d
Revise AssignmentEntity
Rapter1990 Aug 20, 2023
6b68522
Revise AssignmentToAssignmentEntityMapper
Rapter1990 Aug 20, 2023
05c2cf0
Create AysLocationUtil
Rapter1990 Aug 21, 2023
8cf1a3e
Revise Assignment, AssignmentEntity, AssignmentSaveRequestToAssignmen…
Rapter1990 Aug 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ays.assignment.controller;

import com.ays.assignment.model.dto.request.AssignmentSaveRequest;
import com.ays.assignment.service.AssignmentSaveService;
import com.ays.common.model.dto.response.AysResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* REST controller class for managing assignment-related operations via HTTP requests.
* This controller handles the CRUD operations for assignments in the system.
* The mapping path for this controller is "/api/v1/assignment".
*/
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
@Validated
class AssignmentController {

private final AssignmentSaveService assignmentSaveService;


/**
* Saves a new assignment to the system.
* Requires ADMIN authority.
*
* @param saveRequest The request object containing the assignment data to be saved.
* @return A response object containing the saved assignment data.
*/
@PostMapping("/assignment")
@PreAuthorize("hasAnyAuthority('ADMIN')")
public AysResponse<Void> saveAssignment(@RequestBody @Valid AssignmentSaveRequest saveRequest) {
assignmentSaveService.saveAssignment(saveRequest);
return AysResponse.SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.ays.assignment.model.dto.request;

import com.ays.common.model.AysPhoneNumber;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

/**
* A DTO class representing the request data for saving an assignment.
* <p>
* This class provides getters and setters for the description, first name, last name, latitude, longitude and phone number fields.
* It also includes a builder pattern implementation for constructing instances of this class with optional parameters.
* <p>
* The purpose of this class is to encapsulate the request data related to saving an assignment, allowing for easy
* transfer of the data between different layers of the application.
*/
@Getter
@Setter
@Builder
public class AssignmentSaveRequest {

@NotBlank
private String description;

@NotBlank
private String firstName;

@NotBlank
private String lastName;

@Valid
@NotNull
private AysPhoneNumber phoneNumber;
agitrubard marked this conversation as resolved.
Show resolved Hide resolved

@NotNull
private Double latitude;

@NotNull
private Double longitude;
}
agitrubard marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -55,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
Expand All @@ -67,9 +69,11 @@ 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);
this.point.setSRID(4326); // Set the SRID of the created Point
}
agitrubard marked this conversation as resolved.
Show resolved Hide resolved

public boolean isAvailable() {
Expand All @@ -92,6 +96,7 @@ public boolean isDone() {
return AssignmentStatus.DONE.equals(this.status);
}


public void updateAssignmentStatus(AssignmentStatus assignmentStatus) {
this.status = assignmentStatus;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ays.assignment.model.mapper;

import com.ays.assignment.model.Assignment;
import com.ays.assignment.model.dto.request.AssignmentSaveRequest;
import com.ays.assignment.model.entity.AssignmentEntity;
import com.ays.assignment.model.enums.AssignmentStatus;
import com.ays.common.model.mapper.BaseMapper;
import com.ays.common.util.AysRandomUtil;
import com.ays.user.model.entity.UserEntity;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
* UserToUserResponseMapper is an interface that defines the mapping between an {@link AssignmentSaveRequest} and an {@link Assignment}.
* This interface uses the MapStruct annotation @Mapper to generate an implementation of this interface at compile-time.
* <p>The class provides a static method {@code initialize()} that returns an instance of the generated mapper implementation.
* <p>The interface extends the MapStruct interface {@link BaseMapper}, which defines basic mapping methods.
* The interface adds no additional mapping methods, but simply defines the types to be used in the mapping process.
*/
@Mapper
public interface AssignmentSaveRequestToAssignmentMapper extends BaseMapper<AssignmentSaveRequest, Assignment> {

/**
* Maps an {@link AssignmentSaveRequest} object to an {@link AssignmentEntity} object for saving in the database.
*
* @param saveRequest the {@link AssignmentSaveRequest} object to be mapped.
* @param institutionId the {@link String} object.
* @return the mapped {@link UserEntity} object.
*/
default AssignmentEntity mapForSaving(AssignmentSaveRequest saveRequest, String institutionId) {

AssignmentEntity assignmentEntity = 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();

assignmentEntity.setPoint(saveRequest.getLatitude(), saveRequest.getLongitude());

return assignmentEntity;
}
agitrubard marked this conversation as resolved.
Show resolved Hide resolved

/**
* Initializes the mapper.
*
* @return the initialized mapper object.
*/
static AssignmentSaveRequestToAssignmentMapper initialize() {
return Mappers.getMapper(AssignmentSaveRequestToAssignmentMapper.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.ays.assignment.model.entity.AssignmentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
* Repository interface for performing CRUD operations on UserAssignmentEntity objects.
*/
public interface AssignmentEntityRepository extends JpaRepository<AssignmentEntity, String> {
public interface AssignmentRepository extends JpaRepository<AssignmentEntity, String>, JpaSpecificationExecutor<AssignmentEntity> {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ays.assignment.service;

import com.ays.assignment.model.dto.request.AssignmentSaveRequest;

/**
* Assignment Save Service to perform assignment related save business operations.
*/
public interface AssignmentSaveService {

/**
* Saves a saveRequest to the database.
*
* @param saveRequest the AssignmentSaveRequest entity
* @return Assignment
*/
void saveAssignment(AssignmentSaveRequest saveRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.ays.assignment.service.impl;

import com.ays.assignment.model.dto.request.AssignmentSaveRequest;
import com.ays.assignment.model.entity.AssignmentEntity;
import com.ays.assignment.model.mapper.AssignmentSaveRequestToAssignmentMapper;
import com.ays.assignment.repository.AssignmentRepository;
import com.ays.assignment.service.AssignmentSaveService;
import com.ays.auth.model.AysIdentity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
* UserSaveServiceImpl is an implementation of the {@link AssignmentSaveService} interface.
* It provides methods for saving assignment data and performing related operations by admin.
*/
@Service
@RequiredArgsConstructor
class AssignmentSaveServiceImpl implements AssignmentSaveService {

private final AssignmentRepository assignmentRepository;

private final AysIdentity identity;

private static final AssignmentSaveRequestToAssignmentMapper assignmentSaveRequestToAssignmentMapper = AssignmentSaveRequestToAssignmentMapper.initialize();

/**
* Saves a new assignment based on the provided save request.
*
* @param saveRequest the request object containing assignment data to be saved
* @return the saved assignment
*/
@Override
public void saveAssignment(AssignmentSaveRequest saveRequest) {

final AssignmentEntity assignmentEntity = assignmentSaveRequestToAssignmentMapper.mapForSaving(saveRequest, identity.getInstitutionId());
agitrubard marked this conversation as resolved.
Show resolved Hide resolved

assignmentRepository.save(assignmentEntity);

}
agitrubard marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
20 changes: 10 additions & 10 deletions src/main/resources/db/changelog/changes/4-ays-ddl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="USER_ID" type="VARCHAR(36)">
<constraints unique="true"
foreignKeyName="FK__AYS_ASSIGNMENT__USER_ID"
referencedTableName="AYS_USER"
referencedColumnNames="ID"
<constraints
foreignKeyName="FK__AYS_ASSIGNMENT__USER_ID"
referencedTableName="AYS_USER"
referencedColumnNames="ID"
/>
</column>
<column name="INSTITUTION_ID" type="VARCHAR(36)">
<constraints unique="true"
foreignKeyName="FK__AYS_ASSIGNMENT__INSTITUTION_ID"
referencedTableName="AYS_USER"
referencedColumnNames="ID"
<constraints
foreignKeyName="FK__AYS_ASSIGNMENT__INSTITUTION_ID"
referencedTableName="AYS_INSTITUTION"
referencedColumnNames="ID"
/>
</column>
<column name="DESCRIPTION" type="VARCHAR(2048)">
Expand All @@ -69,10 +69,10 @@
<column name="LAST_NAME" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="COUNTRY_CODE" type="INTEGER(5)">
<column name="COUNTRY_CODE" type="VARCHAR(5)">
<constraints nullable="false"/>
</column>
<column name="LINE_NUMBER" type="INTEGER(10)">
<column name="LINE_NUMBER" type="VARCHAR(10)">
<constraints nullable="false"/>
</column>
<column name="STATUS" type="VARCHAR(50)">
Expand Down
22 changes: 11 additions & 11 deletions src/main/resources/db/changelog/changes/5-ays-dml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
<!-- ========================== -->
<sql>
<!-- Kadiköy, Moda (40.981210, 29.026083) -->
INSERT INTO AYS_ASSIGNMENT (ID, DESCRIPTION, FIRST_NAME, LAST_NAME, COUNTRY_CODE, LINE_NUMBER, STATUS,
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', ST_GeomFromText('POINT(40.981210 29.026083)', 4326),
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,
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', ST_GeomFromText('POINT(40.981210 29.000000)', 4326),
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,
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),
'AYS', CURRENT_TIMESTAMP);
</sql>
<!-- ========================== -->
<!-- DML of AYS_ASSIGNMENT -->
Expand Down
Loading