diff --git a/src/main/java/Journey/Together/domain/bookbark/controller/BookmarkController.java b/src/main/java/Journey/Together/domain/bookbark/controller/BookmarkController.java index 549cb4f..35b4d1f 100644 --- a/src/main/java/Journey/Together/domain/bookbark/controller/BookmarkController.java +++ b/src/main/java/Journey/Together/domain/bookbark/controller/BookmarkController.java @@ -40,10 +40,17 @@ public ApiResponse> getPlanBookmarks( } - @PatchMapping("/{placeId}") - public ApiResponse> getBookmarkPlaceNames( + @PatchMapping("/place/{placeId}") + public ApiResponse updatePlaceBookmark( @AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable Long placeId){ - bookmarkService.bookmark(principalDetails.getMember(), placeId); + bookmarkService.placeBookmark(principalDetails.getMember(), placeId); + return ApiResponse.success(Success.CHANGE_BOOKMARK_SUCCESS); + } + + @PatchMapping("/plan/{planId}") + public ApiResponse updatePlanBookmark( + @AuthenticationPrincipal PrincipalDetails principalDetails, @PathVariable Long planId){ + bookmarkService.planBookmark(principalDetails.getMember(), planId); return ApiResponse.success(Success.CHANGE_BOOKMARK_SUCCESS); } diff --git a/src/main/java/Journey/Together/domain/bookbark/repository/PlanBookmarkRepository.java b/src/main/java/Journey/Together/domain/bookbark/repository/PlanBookmarkRepository.java index 590fd6d..683c8a4 100644 --- a/src/main/java/Journey/Together/domain/bookbark/repository/PlanBookmarkRepository.java +++ b/src/main/java/Journey/Together/domain/bookbark/repository/PlanBookmarkRepository.java @@ -3,6 +3,7 @@ import Journey.Together.domain.bookbark.entity.PlaceBookmark; import Journey.Together.domain.bookbark.entity.PlanBookmark; +import Journey.Together.domain.dairy.entity.Plan; import Journey.Together.domain.member.entity.Member; import Journey.Together.domain.place.entity.Place; import org.springframework.data.jpa.repository.JpaRepository; @@ -13,5 +14,7 @@ public interface PlanBookmarkRepository extends JpaRepository findAllByMemberOrderByCreatedAtDesc(Member member); + PlanBookmark findPlanBookmarkByPlanAndMember(Plan plan, Member member); + } diff --git a/src/main/java/Journey/Together/domain/bookbark/service/BookmarkService.java b/src/main/java/Journey/Together/domain/bookbark/service/BookmarkService.java index c8f992f..7bacf05 100644 --- a/src/main/java/Journey/Together/domain/bookbark/service/BookmarkService.java +++ b/src/main/java/Journey/Together/domain/bookbark/service/BookmarkService.java @@ -6,9 +6,11 @@ import Journey.Together.domain.bookbark.entity.PlanBookmarkRes; import Journey.Together.domain.bookbark.repository.PlaceBookmarkRepository; import Journey.Together.domain.bookbark.repository.PlanBookmarkRepository; +import java.util.Optional; import Journey.Together.domain.dairy.entity.Day; import Journey.Together.domain.dairy.entity.Plan; import Journey.Together.domain.dairy.repository.DayRepository; +import Journey.Together.domain.dairy.repository.PlanRepository; import Journey.Together.domain.dairy.service.PlanService; import Journey.Together.domain.member.entity.Member; import Journey.Together.domain.place.dto.response.PlaceBookmarkDto; @@ -35,6 +37,7 @@ public class BookmarkService { private final PlaceRepository placeRepository; private final DayRepository dayRepository; + private final PlanRepository planRepository; //북마크한 여행지 이름만 가져오기 @@ -49,10 +52,10 @@ public List getBookmarkPlaceNames(Member member){ @Transactional // 북마크 상태변경 - public void bookmark(Member member, Long placeId){ + public void placeBookmark(Member member, Long placeId){ Place place = getPlace(placeId); - PlaceBookmark placeBookmark = placeBookmarkRepository.findPlaceBookmarkByPlaceAndMember(place, member);// 북마크 설정 + PlaceBookmark placeBookmark = placeBookmarkRepository.findPlaceBookmarkByPlaceAndMember(place, member); if (placeBookmark == null) { PlaceBookmark newPlaceBookmark = PlaceBookmark.builder() .place(place) @@ -65,6 +68,25 @@ public void bookmark(Member member, Long placeId){ } } + @Transactional + // 북마크 상태변경 + public void planBookmark(Member member, Long planId){ + Plan plan = planRepository.findById(planId) + .orElseThrow(() -> new ApplicationException(ErrorCode.NOT_FOUND_PLACE_EXCEPTION)); + + PlanBookmark planBookmark = planBookmarkRepository.findPlanBookmarkByPlanAndMember(plan, member); + if (planBookmark == null) { + PlanBookmark newPlanBookmark = PlanBookmark.builder() + .plan(plan) + .member(member) + .build(); + planBookmarkRepository.save(newPlanBookmark); + } else { + // 북마크 해체 + planBookmarkRepository.delete(planBookmark); + } + } + private Place getPlace(Long placeId){ return placeRepository.findById(placeId).orElseThrow( ()->new ApplicationException(ErrorCode.NOT_FOUND_PLACE_EXCEPTION)); diff --git a/src/main/java/Journey/Together/domain/dairy/repository/PlanRepository.java b/src/main/java/Journey/Together/domain/dairy/repository/PlanRepository.java index 15c3c9a..f8e2239 100644 --- a/src/main/java/Journey/Together/domain/dairy/repository/PlanRepository.java +++ b/src/main/java/Journey/Together/domain/dairy/repository/PlanRepository.java @@ -5,12 +5,14 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; import java.time.LocalDate; import java.util.List; public interface PlanRepository extends JpaRepository { Plan findPlanByPlanIdAndDeletedAtIsNull(Long planId); + Plan findPlanByMemberAndPlanIdAndDeletedAtIsNull(Member member,Long planId); List findAllByMemberAndDeletedAtIsNull(Member member); Plan findPlanByMemberAndPlanIdAndEndDateIsBeforeAndDeletedAtIsNull(Member member, Long planId, LocalDate today);