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; - } }