Skip to content

Commit

Permalink
Revise Assignment, AssignmentEntity, AssignmentSaveRequestToAssignmen…
Browse files Browse the repository at this point in the history
…tMapper, AssignmentSaveRequestToAssignmentMapper, AssignmentToAssignmentEntityMapper
  • Loading branch information
Rapter1990 committed Aug 21, 2023
1 parent 05c2cf0 commit 8cf1a3e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/ays/assignment/model/Assignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -97,4 +94,10 @@ public void updateAssignmentStatus(AssignmentStatus assignmentStatus) {
this.status = assignmentStatus;
}

public abstract static class AssignmentEntityBuilder<C extends AssignmentEntity, B extends AssignmentEntityBuilder<C, B>> extends BaseEntity.BaseEntityBuilder<C, B> {
public B point(final Double latitude, final Double longitude) {
this.point = AysLocationUtil.generatePoint(latitude, longitude);
return this.self();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,8 +20,6 @@ public interface AssignmentEntityToAssignmentMapper extends BaseMapper<Assignmen
@Override
@Mapping(target = "phoneNumber.countryCode", source = "source.countryCode")
@Mapping(target = "phoneNumber.lineNumber", source = "source.lineNumber")
@Mapping(target = "latitude", expression = "java(mapToLatitude(source.getPoint()))")
@Mapping(target = "longitude", expression = "java(mapToLongitude(source.getPoint()))")
Assignment map(AssignmentEntity source);

/**
Expand All @@ -36,28 +31,4 @@ static AssignmentEntityToAssignmentMapper initialize() {
return Mappers.getMapper(AssignmentEntityToAssignmentMapper.class);
}

/**
* Maps a Point object to latitude.
*
* @param point The Point object.
* @return The latitude coordinate.
*/
default Double mapToLatitude(Point point) {
return Optional.ofNullable(point)
.map(Point::getY)
.orElse(null);
}

/**
* Maps a Point object to longitude.
*
* @param point The Point object.
* @return The longitude coordinate.
*/
default Double mapToLongitude(Point point) {
return Optional.ofNullable(point)
.map(Point::getX)
.orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ public interface AssignmentSaveRequestToAssignmentMapper extends BaseMapper<Assi
*/
default AssignmentEntity mapForSaving(AssignmentSaveRequest saveRequest, String institutionId) {

AssignmentEntity assignmentEntity = AssignmentEntity.builder()
return AssignmentEntity.builder()
.id(AysRandomUtil.generateUUID())
.institutionId(institutionId)
.description(saveRequest.getDescription())
.firstName(saveRequest.getFirstName())
.lastName(saveRequest.getLastName())
.lineNumber(saveRequest.getPhoneNumber().getLineNumber())
.countryCode(saveRequest.getPhoneNumber().getCountryCode())
.description(saveRequest.getDescription())
.point(saveRequest.getLatitude(), saveRequest.getLongitude())
.status(AssignmentStatus.AVAILABLE)
.build();

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

return assignmentEntity;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
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.*;
import org.locationtech.jts.geom.impl.CoordinateArraySequence;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
Expand All @@ -19,15 +16,6 @@
@Mapper
public interface AssignmentToAssignmentEntityMapper extends BaseMapper<Assignment, AssignmentEntity> {

/**
* 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.
Expand All @@ -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;
}
}

0 comments on commit 8cf1a3e

Please sign in to comment.