-
Notifications
You must be signed in to change notification settings - Fork 1
[feat] 유저 장소 저장 API 구현 #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
2daf4d8
42c2ed4
002dd51
cfcfb90
03f4d08
dc37c30
b21815d
982c0fd
f3d3f25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.wayble.server.user.controller; | ||
|
|
||
| import com.wayble.server.common.response.CommonResponse; | ||
| import com.wayble.server.user.dto.UserPlaceRequestDto; | ||
| import com.wayble.server.user.service.UserPlaceService; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/users/{userId}/places") | ||
| @RequiredArgsConstructor | ||
| public class UserPlaceController { | ||
|
|
||
| private final UserPlaceService userPlaceService; | ||
|
|
||
| @PostMapping | ||
| @Operation(summary = "유저 장소 저장", description = "유저가 웨이블존을 장소로 저장합니다.") | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "장소 저장 성공"), | ||
| @ApiResponse(responseCode = "400", description = "이미 저장한 장소입니다."), | ||
| @ApiResponse(responseCode = "404", description = "해당 유저 또는 웨이블존이 존재하지 않음") | ||
| }) | ||
| public CommonResponse<String> saveUserPlace( | ||
| @PathVariable Long userId, | ||
| @RequestBody UserPlaceRequestDto request, | ||
|
|
||
| // 테스트를 위해 임시 허용 (로그인 구현되면 삭제) | ||
| @RequestHeader(value = "Authorization", required = false) String authorizationHeader | ||
| ) { | ||
| userPlaceService.saveUserPlace(request); | ||
| return CommonResponse.success("장소가 저장되었습니다."); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.wayble.server.user.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record UserPlaceRequestDto( | ||
| @NotNull Long userId, | ||
| @NotNull Long waybleZoneId, | ||
| @NotNull String title | ||
| ) {} |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -58,5 +58,6 @@ public class User extends BaseEntity { | |||||||||||||
|
|
||||||||||||||
| // TODO 프로필 이미지 관련 작업 필요 | ||||||||||||||
|
|
||||||||||||||
| // TODO 내가 저장한 장소 관련 작업 필요 | ||||||||||||||
| @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) | ||||||||||||||
| private List<UserPlace> userPlaces = new ArrayList<>(); | ||||||||||||||
|
Comment on lines
+61
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UserPlace 엔티티 import 구문이 누락되었습니다. 새로 추가된 파일 상단에 다음 import를 추가해주세요: import com.wayble.server.common.entity.BaseEntity;
import com.wayble.server.review.entity.Review;
+import com.wayble.server.user.entity.UserPlace;
import jakarta.persistence.*;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 같은 패키지 내 클래스여서 import 없이도 사용할 수 있는거 아니야? 파일 내에서도 컴파일 오류 안나고 있어. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @seung-in-Yoo 맞습니다! 제가 실수했네요. 같은 패키지 내의 클래스들은 import 없이 사용할 수 있습니다.
🐰 ✏️ Learnings added
|
||||||||||||||
| } | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.wayble.server.user.repository; | ||
|
|
||
| import com.wayble.server.user.entity.UserPlace; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface UserPlaceRepository extends JpaRepository<UserPlace, Long> { | ||
| Optional<UserPlace> findByUser_IdAndTitle(Long userId, String title); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.wayble.server.user.repository; | ||
|
|
||
| import com.wayble.server.user.entity.UserPlaceWaybleZoneMapping; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface UserPlaceWaybleZoneMappingRepository extends JpaRepository<UserPlaceWaybleZoneMapping, Long> { | ||
| boolean existsByUserPlace_User_IdAndWaybleZone_Id(Long userId, Long zoneId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.wayble.server.user.service; | ||
|
|
||
|
|
||
| import com.wayble.server.common.exception.ApplicationException; | ||
| import com.wayble.server.user.dto.UserPlaceRequestDto; | ||
| import com.wayble.server.user.entity.User; | ||
| import com.wayble.server.user.entity.UserPlace; | ||
| import com.wayble.server.user.entity.UserPlaceWaybleZoneMapping; | ||
| import com.wayble.server.user.exception.UserErrorCase; | ||
| import com.wayble.server.user.repository.UserPlaceRepository; | ||
| import com.wayble.server.user.repository.UserPlaceWaybleZoneMappingRepository; | ||
| import com.wayble.server.user.repository.UserRepository; | ||
| import com.wayble.server.wayblezone.entity.WaybleZone; | ||
| import com.wayble.server.wayblezone.repository.WaybleZoneRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class UserPlaceService { | ||
|
|
||
| private final UserRepository userRepository; | ||
| private final WaybleZoneRepository waybleZoneRepository; | ||
| private final UserPlaceRepository userPlaceRepository; | ||
| private final UserPlaceWaybleZoneMappingRepository mappingRepository; | ||
|
|
||
| public void saveUserPlace(UserPlaceRequestDto request) { | ||
| // 유저 존재 확인 | ||
| User user = userRepository.findById(request.userId()) | ||
| .orElseThrow(() -> new ApplicationException(UserErrorCase.USER_NOT_FOUND)); | ||
|
|
||
| // 웨이블존 존재 확인 | ||
| WaybleZone waybleZone = waybleZoneRepository.findById(request.waybleZoneId()) | ||
| .orElseThrow(() -> new ApplicationException(UserErrorCase.WAYBLE_ZONE_NOT_FOUND)); | ||
|
|
||
| // 중복 저장 확인 | ||
| boolean alreadySaved = mappingRepository.existsByUserPlace_User_IdAndWaybleZone_Id(request.userId(), request.waybleZoneId()); | ||
| if (alreadySaved) { | ||
| throw new ApplicationException(UserErrorCase.PLACE_ALREADY_SAVED); | ||
| } | ||
|
|
||
| // 저장 | ||
| UserPlace userPlace = userPlaceRepository.save( | ||
| UserPlace.builder() | ||
| .title(request.title()) | ||
| .user(user) | ||
| .build() | ||
| ); | ||
|
|
||
| mappingRepository.save( | ||
| UserPlaceWaybleZoneMapping.builder() | ||
| .userPlace(userPlace) | ||
| .waybleZone(waybleZone) | ||
| .build() | ||
| ); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.