diff --git a/src/main/java/org/likelion/likelion_12th_team05/common/EntityFinder.java b/src/main/java/org/likelion/likelion_12th_team05/common/EntityFinder.java index 30b4aab..761289b 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/common/EntityFinder.java +++ b/src/main/java/org/likelion/likelion_12th_team05/common/EntityFinder.java @@ -12,4 +12,9 @@ public static T findByIdOrThrow(Optional optionalEntity, ErrorCode errorC return optionalEntity.orElseThrow(() -> new NotFoundException(errorCode, errorCode.getMessage())); } + + public static T findByEmailOrThrow(Optional optionalEntity, ErrorCode errorCode) { + return optionalEntity.orElseThrow(() -> + new NotFoundException(errorCode, errorCode.getMessage())); + } } \ No newline at end of file diff --git a/src/main/java/org/likelion/likelion_12th_team05/curation/application/CurationService.java b/src/main/java/org/likelion/likelion_12th_team05/curation/application/CurationService.java index 80e6715..4493a83 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/curation/application/CurationService.java +++ b/src/main/java/org/likelion/likelion_12th_team05/curation/application/CurationService.java @@ -30,8 +30,8 @@ public class CurationService { // 인증된 사용자 - 큐레이션 생성 @Transactional public CurationInfoResDto curationSave(CurationSaveReqDto curationSaveReqDto, Principal principal) { - Long id = Long.parseLong(principal.getName()); // user repo에서 userId로 바꿔야 함 - User user = getUserById(id); + String email = principal.getName(); + User user = getUserByEmail(email); Curation curation = curationSaveReqDto.toEntity(user); curationRepository.save(curation); @@ -42,15 +42,14 @@ public CurationInfoResDto curationSave(CurationSaveReqDto curationSaveReqDto, Pr // 인증된 사용자 - 큐레이션 수정 @Transactional public CurationInfoResDto curationUpdate(Long curationId, CurationUpdateReqDto curationUpdateReqDto, Principal principal) { - Long id = Long.parseLong(principal.getName()); // user repo에서 userId로 바꿔야 함 - User user = getUserById(id); + String email = principal.getName(); + User user = getUserByEmail(email); Curation curation = getCurationById(curationId); - // 수정 권한 확인 -- 토큰 발급의 주체가 email이기에 email로 사용자 확인을 하였으나 - // userid로 판단 하는 것이 더 좋지 않을까 생각함. 일단 crud와 로그인 연결 테스트를 위해 email로 테스트 함 -> 테스트 성공 -> userId로 바꾸기로 함 - String LoginId = principal.getName(); - if (!id.equals(LoginId)) { + // 큐레이션 작성자 이메일과 일치하는지 확인 - 수정 권한 확인 + String CurationUserEmail = curation.getUser().getEmail(); + if (!email.equals(CurationUserEmail)) { throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION , ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage()); } @@ -65,11 +64,11 @@ public CurationInfoResDto curationUpdate(Long curationId, CurationUpdateReqDto c public void curationDelete(Long curationId, Principal principal) { Curation curation = getCurationById(curationId); - // 삭제 권한 확인 - Long id = Long.parseLong(principal.getName()); - Long LoginId = Long.parseLong(principal.getName()); + // 큐레이션 작성자 이메일과 일치하는지 확인 - 삭제 권한 확인 + String email = principal.getName(); + String CurationUserEmail = curation.getUser().getEmail(); - if (!id.equals(LoginId)) { + if (!email.equals(CurationUserEmail)) { throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION , ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage()); } @@ -123,8 +122,8 @@ private Curation getCurationById(Long curationId) { , ErrorCode.CURATIONS_NOT_FOUND_EXCEPTION); } - private User getUserById(Long userId) { - return EntityFinder.findByIdOrThrow(userRepository.findById(userId) + private User getUserByEmail(String email) { + return EntityFinder.findByEmailOrThrow(userRepository.findByEmail(email) , ErrorCode.USER_NOT_FOUND_EXCEPTION); } } diff --git a/src/main/java/org/likelion/likelion_12th_team05/like/application/LikeService.java b/src/main/java/org/likelion/likelion_12th_team05/like/application/LikeService.java index 6c28aaa..826fffe 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/like/application/LikeService.java +++ b/src/main/java/org/likelion/likelion_12th_team05/like/application/LikeService.java @@ -29,8 +29,8 @@ public class LikeService { public void likeSave(@PathVariable("curationId") Long curationId, Principal principal) { Curation curation = getCurationById(curationId); - Long id = Long.parseLong(principal.getName()); - User user = getUserById(id); + String email = principal.getName(); + User user = getUserByEmail(email); Like like = Like.of(curation, user); likeRepository.save(like); @@ -45,8 +45,8 @@ public void likeSave(@PathVariable("curationId") Long curationId, Principal prin public void likeDelete(@PathVariable("curationId") Long curationId, Principal principal) { Curation curation = getCurationById(curationId); - Long id = Long.parseLong(principal.getName()); - User user = getUserById(id); + String email = principal.getName(); + User user = getUserByEmail(email); // 큐레이션에 좋아요가 존재하는지 확인 List existingLikes = likeRepository.findByCurationAndUser(curation, user); @@ -68,8 +68,8 @@ private Curation getCurationById(Long curationId) { , ErrorCode.CURATIONS_NOT_FOUND_EXCEPTION); } - private User getUserById(Long userId) { - return EntityFinder.findByIdOrThrow(userRepository.findById(userId) + private User getUserByEmail(String email) { + return EntityFinder.findByEmailOrThrow(userRepository.findByEmail(email) , ErrorCode.USER_NOT_FOUND_EXCEPTION); } } diff --git a/src/main/java/org/likelion/likelion_12th_team05/location/api/dto/response/LocationInfoResDto.java b/src/main/java/org/likelion/likelion_12th_team05/location/api/dto/response/LocationInfoResDto.java index e2ba8e4..7b53e90 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/location/api/dto/response/LocationInfoResDto.java +++ b/src/main/java/org/likelion/likelion_12th_team05/location/api/dto/response/LocationInfoResDto.java @@ -5,6 +5,7 @@ @Builder public record LocationInfoResDto( + Long locationId, Long curationId, String name, String description, @@ -13,6 +14,7 @@ public record LocationInfoResDto( ) { public static LocationInfoResDto from(Location location) { return LocationInfoResDto.builder() + .locationId(location.getId()) .curationId(location.getCuration().getId()) .name(location.getName()) .description(location.getDescription()) diff --git a/src/main/java/org/likelion/likelion_12th_team05/location/application/LocationService.java b/src/main/java/org/likelion/likelion_12th_team05/location/application/LocationService.java index 1aae611..e4e3b05 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/location/application/LocationService.java +++ b/src/main/java/org/likelion/likelion_12th_team05/location/application/LocationService.java @@ -36,11 +36,17 @@ public class LocationService { @Transactional public LocationInfoResDto locationSave(LocationSaveReqDto locationSaveReqDto, MultipartFile multipartFile , Long curationId, Principal principal) throws IOException { - Long id = Long.parseLong(principal.getName()); - User user = getUserById(id); + String email = principal.getName(); + User user = getUserByEmail(email); Curation curation = getCurationById(curationId); + String CurationUserEmail = curation.getUser().getEmail(); + if (!email.equals(CurationUserEmail)) { + throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION + , ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage()); + } + String locationImage = s3Service.upload(multipartFile, "location"); Location location = locationSaveReqDto.toEntity(locationImage, curation, user); @@ -52,9 +58,9 @@ public LocationInfoResDto locationSave(LocationSaveReqDto locationSaveReqDto, Mu // 인증된 사용자 - 고른 위치 조회 @Transactional public LocationListResDto locationFindAll(Principal principal) { - Long id = Long.parseLong(principal.getName()); + String email = principal.getName(); - List locations = locationRepository.findByUserId(id); + List locations = locationRepository.findByUserEmail(email); List locationInfoResDtoList = locations.stream() .map(LocationInfoResDto::from) @@ -69,10 +75,10 @@ public LocationInfoResDto locationUpdate(Long locationId, LocationUpdateReqDto l Location location = getLocationById(locationId); // 수정 권한 확인 - Long id = location.getUser().getId(); - Long LoginId = Long.parseLong(principal.getName()); + String email = principal.getName(); + String LocationUserEmail = location.getUser().getEmail(); - if (!id.equals(LoginId)) { + if (!email.equals(LocationUserEmail)) { throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION , ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage()); } @@ -93,10 +99,10 @@ public LocationInfoResDto locationUpdate(Long locationId, LocationUpdateReqDto l public void locationDelete(Long locationId, Principal principal) { Location location = getLocationById(locationId); // 삭제 권한 확인 - Long id = location.getUser().getId(); - Long LoginId = Long.parseLong(principal.getName()); + String email = principal.getName(); + String LocationUserEmail = location.getUser().getEmail(); - if (!id.equals(LoginId)) { + if (!email.equals(LocationUserEmail)) { throw new NotFoundException(ErrorCode.NO_AUTHORIZATION_EXCEPTION , ErrorCode.NO_AUTHORIZATION_EXCEPTION.getMessage()); } @@ -110,8 +116,8 @@ private Curation getCurationById(Long curationId) { , ErrorCode.CURATIONS_NOT_FOUND_EXCEPTION); } - private User getUserById(Long userId) { - return EntityFinder.findByIdOrThrow(userRepository.findById(userId) + private User getUserByEmail(String email) { + return EntityFinder.findByEmailOrThrow(userRepository.findByEmail(email) , ErrorCode.USER_NOT_FOUND_EXCEPTION); } diff --git a/src/main/java/org/likelion/likelion_12th_team05/location/domain/repository/LocationRepository.java b/src/main/java/org/likelion/likelion_12th_team05/location/domain/repository/LocationRepository.java index 65f1e2e..7a16a91 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/location/domain/repository/LocationRepository.java +++ b/src/main/java/org/likelion/likelion_12th_team05/location/domain/repository/LocationRepository.java @@ -9,5 +9,5 @@ public interface LocationRepository extends JpaRepository { List findByCurationId(Long curationId); - List findByUserId(Long UserId); + List findByUserEmail(String email); }