Skip to content

Commit

Permalink
[feat] 내 위치 업데이트 API 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
ksj000625 committed Dec 10, 2024
1 parent b8ab9f4 commit 4869bcb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<UserLocationJpaEntity> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,4 +126,12 @@ public BaseResponse<String> updateDevice(@RequestBody DeviceRequest request) {
userService.updateDevice(request);
return new BaseResponse<>(null);
}

@Operation(summary = "내 위치 업데이트", description = "내 위치 정보를 업데이트하는 API")
@ResponseStatus(HttpStatus.OK)
@PostMapping("/location")
public BaseResponse<String> updateLocation(@RequestBody LocationRequest request) {
userService.saveLocation(null, request);
return new BaseResponse<>(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,4 +28,6 @@ public interface UserService {
List<WishlistResponse> getWishlists(Pageable pageable);

void updateDevice(DeviceRequest request);

void saveLocation(String userId, LocationRequest location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +31,7 @@
import site.billbill.apiserver.repository.user.UserRepository;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Slf4j
Expand All @@ -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);
Expand All @@ -51,7 +59,7 @@ public ProfileResponse getProfileInfo() {
public ProfileResponse getProfileInfo(String userId) {
Optional<UserJpaEntity> user = userRepository.findById(userId);
Optional<UserIdentityJpaEntity> userIdentity = userIdentityRepository.findById(userId);
Optional<UserLocationJpaEntity> userLocation = userLocationReposity.findById(userId);
Optional<UserLocationJpaEntity> userLocation = userLocationRepository.findById(userId);

if (user.isEmpty() || userIdentity.isEmpty()) {
throw new CustomException(ErrorCode.NotFound, "회원을 찾을 수 없습니다.", HttpStatus.NOT_FOUND);
Expand Down Expand Up @@ -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<UserLocationJpaEntity> 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);
}
}
7 changes: 4 additions & 3 deletions src/main/java/site/billbill/apiserver/model/BaseTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

0 comments on commit 4869bcb

Please sign in to comment.