-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2daf4d8
[feat] User 엔티티 연관관계 추가
seung-in-Yoo 42c2ed4
[feat] 유저 장소 저장 관련 DTO 생성
seung-in-Yoo 002dd51
[feat] UserPlaceRepository 생성
seung-in-Yoo cfcfb90
[feat] 유저 장소 -> 웨이블존 관련 레포지토리 생성
seung-in-Yoo 03f4d08
[feat] 유저 에러 로직에 에러 코드 추가
seung-in-Yoo dc37c30
[refactor] UserPlaceRequestDto로 DTO 파일 이름 변경
seung-in-Yoo b21815d
[feat] 유저 장소 저장 관련 서비스 로직 구현
seung-in-Yoo 982c0fd
[feat] 유저 장소 저장 관련 컨트롤러 구현
seung-in-Yoo f3d3f25
[refactor] 코드리뷰 반영하여 리팩토링 진행
seung-in-Yoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/wayble/server/user/controller/UserPlaceController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.wayble.server.user.controller; | ||
|
|
||
| import com.wayble.server.common.exception.ApplicationException; | ||
| import com.wayble.server.common.response.CommonResponse; | ||
| import com.wayble.server.user.dto.UserPlaceRequestDto; | ||
| import com.wayble.server.user.exception.UserErrorCase; | ||
| 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 jakarta.validation.Valid; | ||
| 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 @Valid UserPlaceRequestDto request, | ||
|
|
||
| // TODO: 로그인 구현 후 Authorization 헤더 필수로 변경 필요 | ||
| @RequestHeader(value = "Authorization", required = false) String authorizationHeader | ||
| ) { | ||
| // Path variable과 request body의 userId 일치 여부 확인 | ||
| if (!userId.equals(request.userId())) { | ||
| throw new ApplicationException(UserErrorCase.INVALID_USER_ID); | ||
| } | ||
|
|
||
| userPlaceService.saveUserPlace(request); | ||
| return CommonResponse.success("장소가 저장되었습니다."); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/wayble/server/user/dto/UserPlaceRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/wayble/server/user/repository/UserPlaceRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/wayble/server/user/repository/UserPlaceWaybleZoneMappingRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/wayble/server/user/service/UserPlaceService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 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 jakarta.transaction.Transactional; | ||
| 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; | ||
|
|
||
| @Transactional | ||
| 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
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UserPlace 엔티티 import 구문이 누락되었습니다.
새로 추가된
userPlaces필드는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
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@seung-in-Yoo 맞습니다! 제가 실수했네요. 같은 패키지 내의 클래스들은 import 없이 사용할 수 있습니다.
User클래스와UserPlace클래스가 모두com.wayble.server.user.entity패키지에 있기 때문에 별도의 import 구문이 필요하지 않습니다. 불필요한 지적을 해서 죄송합니다. 정정해 주셔서 감사합니다!🐰
╭─╮
( ・ω・) 학습했어요!
╰─╯
✏️ Learnings added