diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java index 9b99f9e..ae161a9 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java @@ -181,6 +181,15 @@ public BaseResponse likePost(@RequestBody @Valid PostsRequest.PostIdRequ return new BaseResponse<>(null); } + @Operation(summary = "게시물 좋아요 취소", description = "해당 게시물에 좋아요를 취소하는 API, 위시리스트 삭제") + @DeleteMapping("/likes") + @ResponseStatus(HttpStatus.OK) + public BaseResponse dislikePost(@RequestBody @Valid PostsRequest.PostIdRequest request) { + String userId = MDC.get(JWTUtil.MDC_USER_ID); + postsService.dislikePost(userId, request.getPostId()); + return new BaseResponse<>(null); + } + @Operation(summary = "대여 기록 삭제", description = "대여 기록 삭제 API") @DeleteMapping("/history") @ResponseStatus(HttpStatus.OK) diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java index 7ee5898..badc5cd 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsService.java @@ -34,5 +34,7 @@ public interface PostsService { void likePost(String userId, String postId); + void dislikePost(String userId, String postId); + void deleteBorrowHistory(String userId, Long borrowSeq); } diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java index db90d1f..9070a48 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/service/PostsServiceImpl.java @@ -83,11 +83,11 @@ public PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest } public PostsResponse.ViewAllResultResponse ViewAllPostService( - String category, int page, Sort.Direction direction, String orderType,String userId) { + String category, int page, Sort.Direction direction, String orderType, String userId) { UserLocationJpaEntity userLocation = userLocationReposity.findByUserId(userId).orElse(null); Pageable pageable = createPageable(page, direction, orderType); log.info(category); - List items = findAndConvertItems(category, pageable, null,userLocation); + List items = findAndConvertItems(category, pageable, null, userLocation); return PostsConverter.toViewAllList(items); } @@ -175,10 +175,10 @@ public String UpdatePostService(String postId, String userId, PostsRequest.Uploa @Transactional public PostsResponse.ViewAllResultResponse ViewSearchPostService(String userId, String category, int page, Sort.Direction direction, String orderType, String keyword, boolean state) { - UserLocationJpaEntity userLocation= userLocationReposity.findByUserId(userId).orElse(null); + UserLocationJpaEntity userLocation = userLocationReposity.findByUserId(userId).orElse(null); Pageable pageable = createPageable(page, direction, orderType); - List items = findAndConvertItems(category, pageable, keyword,userLocation); + List items = findAndConvertItems(category, pageable, keyword, userLocation); //사용자가 검색어 저장을 허용했을 경우 String tempKeyword = keyword.replaceAll("\\+", " "); // if(state){ @@ -312,8 +312,8 @@ public void likePost(String userId, String postId) { } else { itemsLike = ItemsLikeJpaEntity.builder() .id(id) - .items(item) - .user(user) +// .items(item) +// .user(user) .delYn(false) .build(); } @@ -321,6 +321,22 @@ public void likePost(String userId, String postId) { itemsLikeRepository.save(itemsLike); } + @Transactional + @Override + public void dislikePost(String userId, String postId) { + ItemsLikeId id = ItemsLikeId.builder() + .itemId(postId) + .userId(userId) + .build(); + + Optional itemsLike = itemsLikeRepository.findById(id); + + if(itemsLike.isEmpty()) throw new CustomException(ErrorCode.NotFound, "좋아요가 존재하지 않습니다.", HttpStatus.NOT_FOUND); + + itemsLike.get().setDelYn(true); + itemsLikeRepository.save(itemsLike.get()); + } + @Transactional @Override public void deleteBorrowHistory(String userId, Long borrowSeq) { @@ -336,7 +352,7 @@ private Pageable createPageable(int page, Sort.Direction direction, String order case "price" -> "price"; case "createdAt" -> "createdAt"; case "likeCount" -> "likeCount"; - case "distance"->"distance"; + case "distance" -> "distance"; default -> "createdAt"; // 기본 정렬 }; //정렬 순서 @@ -349,9 +365,9 @@ private Pageable createPageable(int page, Sort.Direction direction, String order ); } - private List findAndConvertItems(String category, Pageable pageable, String keyword,UserLocationJpaEntity userLocation) { + private List findAndConvertItems(String category, Pageable pageable, String keyword, UserLocationJpaEntity userLocation) { // Repository 호출 - Page itemsPage = itemsRepository.findItemsWithConditions(category, pageable, null, keyword,userLocation.getLatitude(),userLocation.getLongitude()); + Page itemsPage = itemsRepository.findItemsWithConditions(category, pageable, null, keyword, userLocation.getLatitude(), userLocation.getLongitude()); // 빈 결과 체크 if (itemsPage.isEmpty()) { diff --git a/src/main/java/site/billbill/apiserver/model/post/ItemsLikeJpaEntity.java b/src/main/java/site/billbill/apiserver/model/post/ItemsLikeJpaEntity.java index c3ea59c..91eed37 100644 --- a/src/main/java/site/billbill/apiserver/model/post/ItemsLikeJpaEntity.java +++ b/src/main/java/site/billbill/apiserver/model/post/ItemsLikeJpaEntity.java @@ -20,15 +20,15 @@ public class ItemsLikeJpaEntity extends BaseTime { @EmbeddedId private ItemsLikeId id; - @ManyToOne - @MapsId - @JoinColumn(name="item_id") - private ItemsJpaEntity items; - - @ManyToOne - @MapsId - @JoinColumn(name="user_id") - private UserJpaEntity user; +// @ManyToOne +// @MapsId +// @JoinColumn(name="item_id") +// private ItemsJpaEntity items; +// +// @ManyToOne +// @MapsId +// @JoinColumn(name="user_id") +// private UserJpaEntity user; @Column(name = "del_yn", nullable = false) @Convert(converter = BooleanConverter.class) diff --git a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java index 93a61ff..d004d32 100644 --- a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java +++ b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java @@ -189,7 +189,7 @@ public List getPostHistory(String userId, Pageable pageable ) .from(qItems) .leftJoin(qBorrow).on(qItems.id.eq(qBorrow.id)) - .leftJoin(qLike).on(qItems.id.eq(qLike.items.id).and(qLike.delYn.isFalse())) + .leftJoin(qLike).on(qItems.id.eq(qLike.id.itemId).and(qLike.delYn.isFalse())) .leftJoin(qChatChannel).on(qItems.id.eq(qChatChannel.item.id).and(qChatChannel.delYn.isFalse())) .where(qItems.owner.userId.eq(userId) .and(qItems.delYn.isFalse())) @@ -230,7 +230,7 @@ public List getBorrowHistory(String userId, Pageable page ) .from(qItems) .leftJoin(qBorrow).on(qItems.id.eq(qBorrow.id)) - .leftJoin(qLike).on(qItems.id.eq(qLike.items.id).and(qLike.delYn.isFalse())) + .leftJoin(qLike).on(qItems.id.eq(qLike.id.itemId).and(qLike.delYn.isFalse())) .leftJoin(qChatChannel).on(qItems.id.eq(qChatChannel.item.id).and(qChatChannel.delYn.isFalse())) .rightJoin(qBorrowHist).on(qItems.id.eq(qBorrowHist.item.id).and(qBorrowHist.delYn.isFalse())) .where(qItems.delYn.isFalse()); @@ -275,9 +275,9 @@ public List getWishlists(String userId, Pageable pageable) { ) .from(qItems) .leftJoin(qBorrow).on(qItems.id.eq(qBorrow.id)) - .leftJoin(qLike).on(qItems.id.eq(qLike.items.id).and(qLike.delYn.isFalse())) + .leftJoin(qLike).on(qItems.id.eq(qLike.id.itemId).and(qLike.delYn.isFalse())) .leftJoin(qChatChannel).on(qItems.id.eq(qChatChannel.item.id).and(qChatChannel.delYn.isFalse())) - .where(qLike.user.userId.eq(userId) + .where(qLike.id.userId.eq(userId) .and(qItems.delYn.isFalse())) .groupBy(qItems.id) // 그룹핑 필요 .offset(pageable.getOffset()) diff --git a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsLikeRepository.java b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsLikeRepository.java index 9afe9bc..0b6f46d 100644 --- a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsLikeRepository.java +++ b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsLikeRepository.java @@ -5,6 +5,8 @@ import site.billbill.apiserver.model.post.ItemsLikeJpaEntity; import site.billbill.apiserver.model.post.embeded.ItemsLikeId; +import java.util.Optional; + @Repository public interface ItemsLikeRepository extends JpaRepository { }