diff --git a/src/main/java/com/wayble/server/user/controller/UserPlaceController.java b/src/main/java/com/wayble/server/user/controller/UserPlaceController.java index c6c3bf83..fc38fded 100644 --- a/src/main/java/com/wayble/server/user/controller/UserPlaceController.java +++ b/src/main/java/com/wayble/server/user/controller/UserPlaceController.java @@ -12,6 +12,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -22,7 +23,6 @@ public class UserPlaceController { private final UserPlaceService userPlaceService; - private final JwtTokenProvider jwtProvider; @PostMapping @@ -30,24 +30,20 @@ public class UserPlaceController { @ApiResponses({ @ApiResponse(responseCode = "200", description = "장소 저장 성공"), @ApiResponse(responseCode = "400", description = "이미 저장한 장소입니다."), - @ApiResponse(responseCode = "404", description = "해당 유저 또는 웨이블존이 존재하지 않음") + @ApiResponse(responseCode = "404", description = "해당 유저 또는 웨이블존이 존재하지 않음"), + @ApiResponse(responseCode = "403", description = "권한이 없습니다.") }) public CommonResponse saveUserPlace( @PathVariable Long userId, @RequestBody @Valid UserPlaceRequestDto request, @RequestHeader(value = "Authorization") String authorizationHeader ) { - String token = authorizationHeader.replace("Bearer ", ""); - if (!jwtProvider.validateToken(token)) { + Long tokenUserId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + if (!userId.equals(tokenUserId)) { throw new ApplicationException(UserErrorCase.FORBIDDEN); } - Long tokenUserId = jwtProvider.getUserId(token); - // Path variable과 request body의 userId 일치 여부 확인 - if (!userId.equals(request.userId()) || !userId.equals(tokenUserId)) { - throw new ApplicationException(UserErrorCase.FORBIDDEN); - } - userPlaceService.saveUserPlace(request); + userPlaceService.saveUserPlace(userId, request); // userId 파라미터로 넘김 return CommonResponse.success("장소가 저장되었습니다."); } @@ -65,12 +61,7 @@ public CommonResponse> getUserPlaces( @PathVariable Long userId, @RequestHeader("Authorization") String authorizationHeader ) { - String token = authorizationHeader.replace("Bearer ", ""); - if (!jwtProvider.validateToken(token)) { - throw new ApplicationException(UserErrorCase.FORBIDDEN); - } - Long tokenUserId = jwtProvider.getUserId(token); - + Long tokenUserId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (!userId.equals(tokenUserId)) { throw new ApplicationException(UserErrorCase.FORBIDDEN); } diff --git a/src/main/java/com/wayble/server/user/dto/KakaoLoginRequestDto.java b/src/main/java/com/wayble/server/user/dto/KakaoLoginRequestDto.java new file mode 100644 index 00000000..57d3790c --- /dev/null +++ b/src/main/java/com/wayble/server/user/dto/KakaoLoginRequestDto.java @@ -0,0 +1,5 @@ +package com.wayble.server.user.dto; + +public record KakaoLoginRequestDto( + String accessToken +) {} \ No newline at end of file diff --git a/src/main/java/com/wayble/server/user/dto/KakaoLoginResponseDto.java b/src/main/java/com/wayble/server/user/dto/KakaoLoginResponseDto.java new file mode 100644 index 00000000..575d2d82 --- /dev/null +++ b/src/main/java/com/wayble/server/user/dto/KakaoLoginResponseDto.java @@ -0,0 +1,18 @@ +package com.wayble.server.user.dto; + +import lombok.Builder; + +@Builder +public record KakaoLoginResponseDto( + String accessToken, + String refreshToken, + boolean isNewUser, + UserDto user +) { + @Builder + public record UserDto( + Long id, + String nickname, + String email + ) {} +} \ No newline at end of file diff --git a/src/main/java/com/wayble/server/user/dto/KakaoUserInfoDto.java b/src/main/java/com/wayble/server/user/dto/KakaoUserInfoDto.java new file mode 100644 index 00000000..69a46937 --- /dev/null +++ b/src/main/java/com/wayble/server/user/dto/KakaoUserInfoDto.java @@ -0,0 +1,25 @@ +package com.wayble.server.user.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class KakaoUserInfoDto { + private Long id; + private KakaoAccount kakao_account; + + @Getter + @Setter + public static class KakaoAccount { + private String email; + private Profile profile; + + @Getter + @Setter + public static class Profile { + private String nickname; + private String profile_image_url; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/wayble/server/user/dto/UserPlaceRequestDto.java b/src/main/java/com/wayble/server/user/dto/UserPlaceRequestDto.java index 5285ab76..6643f396 100644 --- a/src/main/java/com/wayble/server/user/dto/UserPlaceRequestDto.java +++ b/src/main/java/com/wayble/server/user/dto/UserPlaceRequestDto.java @@ -3,7 +3,6 @@ import jakarta.validation.constraints.NotNull; public record UserPlaceRequestDto( - @NotNull Long userId, @NotNull Long waybleZoneId, @NotNull String title ) {} diff --git a/src/main/java/com/wayble/server/user/service/UserPlaceService.java b/src/main/java/com/wayble/server/user/service/UserPlaceService.java index 24885ee4..75e6d28a 100644 --- a/src/main/java/com/wayble/server/user/service/UserPlaceService.java +++ b/src/main/java/com/wayble/server/user/service/UserPlaceService.java @@ -29,9 +29,9 @@ public class UserPlaceService { private final UserPlaceWaybleZoneMappingRepository mappingRepository; @Transactional - public void saveUserPlace(UserPlaceRequestDto request) { + public void saveUserPlace(Long userId, UserPlaceRequestDto request) { // 유저 존재 확인 - User user = userRepository.findById(request.userId()) + User user = userRepository.findById(userId) .orElseThrow(() -> new ApplicationException(UserErrorCase.USER_NOT_FOUND)); // 웨이블존 존재 확인 @@ -39,7 +39,7 @@ public void saveUserPlace(UserPlaceRequestDto request) { .orElseThrow(() -> new ApplicationException(UserErrorCase.WAYBLE_ZONE_NOT_FOUND)); // 중복 저장 확인 - boolean alreadySaved = mappingRepository.existsByUserPlace_User_IdAndWaybleZone_Id(request.userId(), request.waybleZoneId()); + boolean alreadySaved = mappingRepository.existsByUserPlace_User_IdAndWaybleZone_Id(userId, request.waybleZoneId()); if (alreadySaved) { throw new ApplicationException(UserErrorCase.PLACE_ALREADY_SAVED); } @@ -72,8 +72,7 @@ public List getUserPlaces(Long userId) { WaybleZone waybleZone = mapping.getWaybleZone(); // 웨이블존 대표 이미지 가져오기 - String imageUrl = waybleZone.getWaybleZoneImageList().stream() - .findFirst().map(img -> img.getImageUrl()).orElse(null); + String imageUrl = waybleZone.getMainImageUrl(); return UserPlaceListResponseDto.builder() .place_id(userPlace.getId()) diff --git a/src/main/java/com/wayble/server/wayblezone/entity/WaybleZone.java b/src/main/java/com/wayble/server/wayblezone/entity/WaybleZone.java index 9f36d70a..ad0a4e42 100644 --- a/src/main/java/com/wayble/server/wayblezone/entity/WaybleZone.java +++ b/src/main/java/com/wayble/server/wayblezone/entity/WaybleZone.java @@ -57,4 +57,13 @@ public class WaybleZone extends BaseEntity { @OneToMany(mappedBy = "waybleZone", cascade = CascadeType.ALL, orphanRemoval = true) private List userPlaceMappings = new ArrayList<>(); + + // 대표 이미지 필드 추가 + @Column(name = "main_image_url") + private String mainImageUrl; + + // 혹시 필요할수도 있어서 추가해놓음 + public void setMainImageUrl(String mainImageUrl) { + this.mainImageUrl = mainImageUrl; + } }