diff --git a/src/main/java/site/billbill/apiserver/api/auth/service/AuthServiceImpl.java b/src/main/java/site/billbill/apiserver/api/auth/service/AuthServiceImpl.java index 4a11a53..bafea2c 100644 --- a/src/main/java/site/billbill/apiserver/api/auth/service/AuthServiceImpl.java +++ b/src/main/java/site/billbill/apiserver/api/auth/service/AuthServiceImpl.java @@ -17,6 +17,7 @@ import site.billbill.apiserver.api.auth.dto.request.LocationRequest; import site.billbill.apiserver.api.auth.dto.request.LoginRequest; import site.billbill.apiserver.api.auth.dto.request.SignupRequest; +import site.billbill.apiserver.api.users.service.UserService; import site.billbill.apiserver.common.enums.exception.ErrorCode; import site.billbill.apiserver.common.enums.user.UserRole; import site.billbill.apiserver.common.utils.ULID.ULIDUtil; @@ -38,10 +39,9 @@ public class AuthServiceImpl implements AuthService { private final UserDeviceRepository userDeviceRepository; private final UserAgreeHistRepository userAgreeHistRepository; private final BCryptPasswordEncoder bCryptPasswordEncoder; + private final UserService userService; private final JWTUtil jwtUtil; - private final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326); - @Override @Transactional public JwtDto signup(SignupRequest request) { @@ -69,7 +69,7 @@ public JwtDto signup(SignupRequest request) { userAgreeHistRepository.save(userAgree); // save location - saveLocation(userId, request.getLocation()); + userService.saveLocation(userId, request.getLocation()); return jwtUtil.generateJwtDto(userId, UserRole.USER); } @@ -143,31 +143,4 @@ private boolean isUserWithdraw(String userId) { private boolean checkPassword(String password, String encryptedPassword) { return bCryptPasswordEncoder.matches(password, encryptedPassword); } - - /** - * Method that save location - * - * @param location address, longitude, latitude - */ - @Transactional - protected void saveLocation( - String userId, - LocationRequest location - ) { - // TODO location.service 패키지로 이동 예정 - // check if location already exists - Optional locationJpaEntity = userLocationRepository.findByUserId(userId); - UserLocationJpaEntity userLocation = locationJpaEntity.orElseGet(UserLocationJpaEntity::new); // if not exists, use new - - // 좌표 계산 - Point coordinates = geometryFactory.createPoint(new Coordinate(location.getLongitude(), location.getLatitude())); - - userLocation.setUserId(userId); - userLocation.setLatitude(location.getLatitude()); - userLocation.setLongitude(location.getLongitude()); - userLocation.setAddress(location.getAddress()); - userLocation.setCoordinates(coordinates); - - userLocationRepository.save(userLocation); - } } diff --git a/src/main/java/site/billbill/apiserver/api/users/controller/UserController.java b/src/main/java/site/billbill/apiserver/api/users/controller/UserController.java index 5164c53..4d8788d 100644 --- a/src/main/java/site/billbill/apiserver/api/users/controller/UserController.java +++ b/src/main/java/site/billbill/apiserver/api/users/controller/UserController.java @@ -13,6 +13,7 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import site.billbill.apiserver.api.auth.dto.request.DeviceRequest; +import site.billbill.apiserver.api.auth.dto.request.LocationRequest; import site.billbill.apiserver.api.users.dto.request.BlacklistRequest; import site.billbill.apiserver.api.users.dto.response.*; import site.billbill.apiserver.api.users.service.UserService; @@ -125,4 +126,12 @@ public BaseResponse updateDevice(@RequestBody DeviceRequest request) { userService.updateDevice(request); return new BaseResponse<>(null); } + + @Operation(summary = "내 위치 업데이트", description = "내 위치 정보를 업데이트하는 API") + @ResponseStatus(HttpStatus.OK) + @PostMapping("/location") + public BaseResponse updateLocation(@RequestBody LocationRequest request) { + userService.saveLocation(null, request); + return new BaseResponse<>(null); + } } diff --git a/src/main/java/site/billbill/apiserver/api/users/service/UserService.java b/src/main/java/site/billbill/apiserver/api/users/service/UserService.java index 6f5c93f..2cdf420 100644 --- a/src/main/java/site/billbill/apiserver/api/users/service/UserService.java +++ b/src/main/java/site/billbill/apiserver/api/users/service/UserService.java @@ -2,6 +2,7 @@ import org.springframework.data.domain.Pageable; import site.billbill.apiserver.api.auth.dto.request.DeviceRequest; +import site.billbill.apiserver.api.auth.dto.request.LocationRequest; import site.billbill.apiserver.api.users.dto.response.*; import site.billbill.apiserver.common.utils.posts.ItemHistoryType; @@ -27,4 +28,6 @@ public interface UserService { List getWishlists(Pageable pageable); void updateDevice(DeviceRequest request); + + void saveLocation(String userId, LocationRequest location); } diff --git a/src/main/java/site/billbill/apiserver/api/users/service/UserServiceImpl.java b/src/main/java/site/billbill/apiserver/api/users/service/UserServiceImpl.java index fbb09ba..bb7c7ef 100644 --- a/src/main/java/site/billbill/apiserver/api/users/service/UserServiceImpl.java +++ b/src/main/java/site/billbill/apiserver/api/users/service/UserServiceImpl.java @@ -2,12 +2,17 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.PrecisionModel; import org.slf4j.MDC; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import site.billbill.apiserver.api.auth.dto.request.DeviceRequest; +import site.billbill.apiserver.api.auth.dto.request.LocationRequest; import site.billbill.apiserver.api.users.dto.response.*; import site.billbill.apiserver.common.enums.exception.ErrorCode; import site.billbill.apiserver.common.utils.jwt.JWTUtil; @@ -26,6 +31,7 @@ import site.billbill.apiserver.repository.user.UserRepository; import java.util.List; +import java.util.Objects; import java.util.Optional; @Slf4j @@ -37,9 +43,11 @@ public class UserServiceImpl implements UserService { private final UserBlacklistRepository userBlacklistRepository; private final ItemsRepository itemsRepository; private final JWTUtil jWTUtil; - private final UserLocationReposity userLocationReposity; + private final UserLocationReposity userLocationRepository; private final UserDeviceRepository userDeviceRepository; + private final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326); + @Override public ProfileResponse getProfileInfo() { String userId = MDC.get(JWTUtil.MDC_USER_ID); @@ -51,7 +59,7 @@ public ProfileResponse getProfileInfo() { public ProfileResponse getProfileInfo(String userId) { Optional user = userRepository.findById(userId); Optional userIdentity = userIdentityRepository.findById(userId); - Optional userLocation = userLocationReposity.findById(userId); + Optional userLocation = userLocationRepository.findById(userId); if (user.isEmpty() || userIdentity.isEmpty()) { throw new CustomException(ErrorCode.NotFound, "회원을 찾을 수 없습니다.", HttpStatus.NOT_FOUND); @@ -153,4 +161,33 @@ public void updateDevice(DeviceRequest request) { userDevice.setAppVersion(request.getAppVersion()); userDeviceRepository.save(userDevice); } + + /** + * Method that save location + * + * @param location address, longitude, latitude + */ + @Override + @Transactional + public void saveLocation( + String userId, + LocationRequest location + ) { + userId = Objects.requireNonNullElse(userId, MDC.get(JWTUtil.MDC_USER_ID)); + + // check if location already exists + Optional locationJpaEntity = userLocationRepository.findByUserId(userId); + UserLocationJpaEntity userLocation = locationJpaEntity.orElseGet(UserLocationJpaEntity::new); // if not exists, use new + + // 좌표 계산 + Point coordinates = geometryFactory.createPoint(new Coordinate(location.getLongitude(), location.getLatitude())); + + userLocation.setUserId(userId); + userLocation.setLatitude(location.getLatitude()); + userLocation.setLongitude(location.getLongitude()); + userLocation.setAddress(location.getAddress()); + userLocation.setCoordinates(coordinates); + + userLocationRepository.save(userLocation); + } } diff --git a/src/main/java/site/billbill/apiserver/model/BaseTime.java b/src/main/java/site/billbill/apiserver/model/BaseTime.java index 1a55e1b..91e0d5b 100644 --- a/src/main/java/site/billbill/apiserver/model/BaseTime.java +++ b/src/main/java/site/billbill/apiserver/model/BaseTime.java @@ -6,6 +6,7 @@ import lombok.Getter; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import java.time.OffsetDateTime; @@ -15,10 +16,10 @@ @EntityListeners(AuditingEntityListener.class) public abstract class BaseTime { @CreationTimestamp - @Column(name = "created_at", insertable = true, updatable = false) + @Column(name = "created_at", updatable = false) private final OffsetDateTime createdAt = OffsetDateTime.now(); - @CreationTimestamp - @Column(name = "updated_at", insertable = false, updatable = true) + @UpdateTimestamp + @Column(name = "updated_at") private OffsetDateTime updatedAt = OffsetDateTime.now(); }