Skip to content

Commit

Permalink
feat: 게시물 좋아요 취소 API
Browse files Browse the repository at this point in the history
  • Loading branch information
ksj000625 committed Dec 23, 2024
1 parent b5ce264 commit 2e6d763
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ public BaseResponse<String> likePost(@RequestBody @Valid PostsRequest.PostIdRequ
return new BaseResponse<>(null);
}

@Operation(summary = "게시물 좋아요 취소", description = "해당 게시물에 좋아요를 취소하는 API, 위시리스트 삭제")
@DeleteMapping("/likes")
@ResponseStatus(HttpStatus.OK)
public BaseResponse<String> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostsResponse.Post> items = findAndConvertItems(category, pageable, null,userLocation);
List<PostsResponse.Post> items = findAndConvertItems(category, pageable, null, userLocation);
return PostsConverter.toViewAllList(items);
}

Expand Down Expand Up @@ -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<PostsResponse.Post> items = findAndConvertItems(category, pageable, keyword,userLocation);
List<PostsResponse.Post> items = findAndConvertItems(category, pageable, keyword, userLocation);
//사용자가 검색어 저장을 허용했을 경우
String tempKeyword = keyword.replaceAll("\\+", " ");
// if(state){
Expand Down Expand Up @@ -312,15 +312,31 @@ public void likePost(String userId, String postId) {
} else {
itemsLike = ItemsLikeJpaEntity.builder()
.id(id)
.items(item)
.user(user)
// .items(item)
// .user(user)
.delYn(false)
.build();
}

itemsLikeRepository.save(itemsLike);
}

@Transactional
@Override
public void dislikePost(String userId, String postId) {
ItemsLikeId id = ItemsLikeId.builder()
.itemId(postId)
.userId(userId)
.build();

Optional<ItemsLikeJpaEntity> 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) {
Expand All @@ -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"; // 기본 정렬
};
//정렬 순서
Expand All @@ -349,9 +365,9 @@ private Pageable createPageable(int page, Sort.Direction direction, String order
);
}

private List<PostsResponse.Post> findAndConvertItems(String category, Pageable pageable, String keyword,UserLocationJpaEntity userLocation) {
private List<PostsResponse.Post> findAndConvertItems(String category, Pageable pageable, String keyword, UserLocationJpaEntity userLocation) {
// Repository 호출
Page<ItemsJpaEntity> itemsPage = itemsRepository.findItemsWithConditions(category, pageable, null, keyword,userLocation.getLatitude(),userLocation.getLongitude());
Page<ItemsJpaEntity> itemsPage = itemsRepository.findItemsWithConditions(category, pageable, null, keyword, userLocation.getLatitude(), userLocation.getLongitude());

// 빈 결과 체크
if (itemsPage.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public List<PostHistoryResponse> 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()))
Expand Down Expand Up @@ -230,7 +230,7 @@ public List<BorrowHistoryResponse> 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());
Expand Down Expand Up @@ -275,9 +275,9 @@ public List<WishlistResponse> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemsLikeJpaEntity, ItemsLikeId> {
}

0 comments on commit 2e6d763

Please sign in to comment.