-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#99 | Save Latest Location of User Flow (#151)
- Loading branch information
1 parent
02035a6
commit e35e7bb
Showing
17 changed files
with
548 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/ays/location/controller/UserLocationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ays.location.controller; | ||
|
||
import com.ays.common.model.dto.response.AysResponse; | ||
import com.ays.location.model.dto.request.UserLocationSaveRequest; | ||
import com.ays.location.service.UserLocationService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
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; | ||
|
||
/** | ||
* Controller class responsible for handling user location-related API endpoints. | ||
* Provides endpoints for saving user location information. | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
class UserLocationController { | ||
|
||
private final UserLocationService userLocationService; | ||
|
||
/** | ||
* Saves the user's location based on the provided UserLocationSaveRequest. | ||
* | ||
* @param saveRequest The request containing the user's location information to be saved. | ||
* @return A response indicating the success of the operation. | ||
*/ | ||
@PostMapping("/user/location") | ||
@PreAuthorize("hasAnyAuthority('USER')") | ||
public AysResponse<Void> saveUserLocation(@RequestBody @Valid final UserLocationSaveRequest saveRequest) { | ||
userLocationService.saveUserLocation(saveRequest); | ||
return AysResponse.SUCCESS; | ||
} | ||
|
||
} |
14 changes: 10 additions & 4 deletions
14
...odel/dto/request/UserLocationRequest.java → .../dto/request/UserLocationSaveRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
package com.ays.location.model.dto.request; | ||
|
||
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 updating user location. | ||
* A DTO class representing the request data for saving/updating user location. | ||
* <p> | ||
* This class provides getters and setters for the latitude, and longitude 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 updating user location, allowing for easy | ||
* transfer of the data between different layers of the application. | ||
*/ | ||
@Data | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class UserLocationRequest { | ||
public class UserLocationSaveRequest { | ||
|
||
@NotNull | ||
private Double latitude; | ||
|
||
@NotNull | ||
private Double longitude; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...java/com/ays/location/model/mapper/UserLocationSaveRequestToUserLocationEntityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ays.location.model.mapper; | ||
|
||
import com.ays.common.model.mapper.BaseMapper; | ||
import com.ays.location.model.dto.request.UserLocationSaveRequest; | ||
import com.ays.location.model.entity.UserLocationEntity; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* UserLocationSaveRequestToUserLocationEntityMapper is an interface that defines the mapping between an {@link UserLocationSaveRequest} and an {@link UserLocationEntity}. | ||
* 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 UserLocationSaveRequestToUserLocationEntityMapper extends BaseMapper<UserLocationSaveRequest, UserLocationEntity> { | ||
|
||
/** | ||
* Maps an {@link UserLocationSaveRequest} object to an {@link UserLocationEntity} object for saving in the database. | ||
* | ||
* @param saveRequest the {@link UserLocationSaveRequest} object to be mapped. | ||
* @param userId the {@link String} object. | ||
* @return the mapped {@link UserLocationEntity} object. | ||
*/ | ||
default UserLocationEntity mapForSaving(UserLocationSaveRequest saveRequest, String userId) { | ||
return UserLocationEntity.builder() | ||
.userId(userId) | ||
.point(saveRequest.getLatitude(), saveRequest.getLongitude()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Initializes the mapper. | ||
* | ||
* @return the initialized mapper object. | ||
*/ | ||
static UserLocationSaveRequestToUserLocationEntityMapper initialize() { | ||
return Mappers.getMapper(UserLocationSaveRequestToUserLocationEntityMapper.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ays/location/service/UserLocationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ays.location.service; | ||
|
||
import com.ays.location.model.dto.request.UserLocationSaveRequest; | ||
|
||
/** | ||
* The UserLocationService interface provides methods to manage and store user location data. | ||
* Implementing classes should define the behavior to save user location information. | ||
*/ | ||
public interface UserLocationService { | ||
|
||
/** | ||
* Saves the user's location based on the provided UserLocationSaveRequest. | ||
* | ||
* @param saveRequest The request containing the user's location information to be saved. | ||
*/ | ||
void saveUserLocation(UserLocationSaveRequest saveRequest); | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/ays/location/service/impl/UserLocationServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.ays.location.service.impl; | ||
|
||
import com.ays.assignment.model.enums.AssignmentStatus; | ||
import com.ays.assignment.repository.AssignmentRepository; | ||
import com.ays.auth.model.AysIdentity; | ||
import com.ays.location.model.dto.request.UserLocationSaveRequest; | ||
import com.ays.location.model.entity.UserLocationEntity; | ||
import com.ays.location.model.mapper.UserLocationSaveRequestToUserLocationEntityMapper; | ||
import com.ays.location.repository.UserLocationRepository; | ||
import com.ays.location.service.UserLocationService; | ||
import com.ays.location.util.exception.AysUserLocationCannotBeUpdatedException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Implementation of the UserLocationService interface that manages and stores user location data. | ||
* This service utilizes a repository to interact with the database for saving user location information. | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
class UserLocationServiceImpl implements UserLocationService { | ||
|
||
private final UserLocationRepository userLocationRepository; | ||
private final AssignmentRepository assignmentRepository; | ||
|
||
private final AysIdentity identity; | ||
|
||
|
||
private final UserLocationSaveRequestToUserLocationEntityMapper userLocationSaveRequestToUserLocationEntityMapper = UserLocationSaveRequestToUserLocationEntityMapper.initialize(); | ||
|
||
/** | ||
* Saves the user's location based on the provided UserLocationSaveRequest. | ||
* If the user's location already exists in the database, updates the location; otherwise, creates a new entry. | ||
* | ||
* @param saveRequest The request containing the user's location information to be saved. | ||
*/ | ||
@Override | ||
public void saveUserLocation(final UserLocationSaveRequest saveRequest) { | ||
|
||
final boolean isAssignmentInProgress = assignmentRepository | ||
.existsByUserIdAndStatus(identity.getUserId(), AssignmentStatus.IN_PROGRESS); | ||
if (!isAssignmentInProgress) { | ||
throw new AysUserLocationCannotBeUpdatedException(); | ||
} | ||
|
||
userLocationRepository.findByUserId(identity.getUserId()) | ||
.ifPresentOrElse( | ||
userLocationEntityFromDatabase -> { | ||
userLocationEntityFromDatabase.setPoint(saveRequest.getLatitude(), saveRequest.getLongitude()); | ||
userLocationRepository.save(userLocationEntityFromDatabase); | ||
}, | ||
() -> { | ||
final UserLocationEntity userLocationEntity = userLocationSaveRequestToUserLocationEntityMapper | ||
.mapForSaving(saveRequest, identity.getUserId()); | ||
userLocationRepository.save(userLocationEntity); | ||
} | ||
); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
.../com/ays/common/util/AysLocationUtil.java → ...om/ays/location/util/AysLocationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/ays/location/util/exception/AysUserLocationCannotBeUpdatedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.ays.location.util.exception; | ||
|
||
import com.ays.common.util.exception.AysProcessException; | ||
|
||
import java.io.Serial; | ||
|
||
/** | ||
* This exception is thrown when a user's location cannot be updated. Reasons for not being able to update the user's | ||
* location may include the absence of an assigned task or the assigned task not being in progress status. | ||
* <p> | ||
* This exception is typically derived from the {@link AysProcessException} class and represents a specific process state | ||
* or condition. | ||
* | ||
* @see AysProcessException | ||
*/ | ||
public class AysUserLocationCannotBeUpdatedException extends AysProcessException { | ||
|
||
/** | ||
* A special field containing version information along with the serial number. | ||
*/ | ||
@Serial | ||
private static final long serialVersionUID = 2733712280590701217L; | ||
|
||
/** | ||
* Constructs an exception for the specified identifier (id). | ||
* | ||
* @param id The identifier (id) that describes the reason for the unupdatable user location. | ||
*/ | ||
public AysUserLocationCannotBeUpdatedException() { | ||
super("USER LOCATION CANNOT BE UPDATED BECAUSE ASSIGNMENT NOT EXIST OR NOT IN PROGRESS STATUS!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.