Skip to content

Commit

Permalink
[#65] feat: 일정 북마크 상태변경
Browse files Browse the repository at this point in the history
  • Loading branch information
mmihye committed Jun 10, 2024
1 parent 9dd3c27 commit 6854f6d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ public ApiResponse<List<PlanBookmarkRes>> getPlanBookmarks(
}


@PatchMapping("/{placeId}")
public ApiResponse<List<PlaceBookmarkDto>> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,5 +14,7 @@ public interface PlanBookmarkRepository extends JpaRepository<PlanBookmark, Long

List<PlanBookmark> findAllByMemberOrderByCreatedAtDesc(Member member);

PlanBookmark findPlanBookmarkByPlanAndMember(Plan plan, Member member);


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +37,7 @@ public class BookmarkService {
private final PlaceRepository placeRepository;

private final DayRepository dayRepository;
private final PlanRepository planRepository;


//북마크한 여행지 이름만 가져오기
Expand All @@ -49,10 +52,10 @@ public List<PlaceBookmarkDto> 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)
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, Long> {
Plan findPlanByPlanIdAndDeletedAtIsNull(Long planId);

Plan findPlanByMemberAndPlanIdAndDeletedAtIsNull(Member member,Long planId);
List<Plan> findAllByMemberAndDeletedAtIsNull(Member member);
Plan findPlanByMemberAndPlanIdAndEndDateIsBeforeAndDeletedAtIsNull(Member member, Long planId, LocalDate today);
Expand Down

0 comments on commit 6854f6d

Please sign in to comment.