Skip to content
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

Feature/fix friends wishlist response #270

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.kakaoshare.backend.domain.wish.controller;

import lombok.RequiredArgsConstructor;
import org.kakaoshare.backend.domain.wish.dto.WishDetail;
import org.kakaoshare.backend.common.dto.PageResponse;
import org.kakaoshare.backend.domain.wish.dto.FriendWishDetail;
import org.kakaoshare.backend.domain.wish.dto.FriendsWishRequest;
import org.kakaoshare.backend.domain.wish.service.WishService;
import org.kakaoshare.backend.jwt.util.LoggedInMember;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -17,18 +22,27 @@
@RestController
@RequiredArgsConstructor
public class WishController {
public static final int PAGE_DEFAULT_SIZE = 20;
private final WishService wishService;

@GetMapping("/me")
public ResponseEntity<?> getWishList(@LoggedInMember String providerId) {
List<WishDetail> wishList = wishService.getMembersWishList(providerId);
public ResponseEntity<?> getWishList(@LoggedInMember String providerId,
@PageableDefault(size = PAGE_DEFAULT_SIZE) Pageable pageable) {
PageResponse<?> wishList = wishService.getMembersWishList(pageable, providerId);
return ResponseEntity.ok(wishList);
}

@PostMapping("/{wishId}/change-type")
public ResponseEntity<?> changeWishType(@LoggedInMember String providerId,
@PathVariable(name = "wishId") Long wishId) {
wishService.changeWishType(providerId,wishId);
wishService.changeWishType(providerId, wishId);
return ResponseEntity.ok().build();
}
}

@GetMapping("/friends")
public ResponseEntity<?> getFriendsWishList(@LoggedInMember String providerId,
@RequestBody FriendsWishRequest friendsWishRequest) {
List<FriendWishDetail> membersWishList = wishService.getFriendsWishList(providerId,friendsWishRequest);
return ResponseEntity.ok(membersWishList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.kakaoshare.backend.domain.wish.dto;

import com.querydsl.core.annotations.QueryProjection;
import lombok.Getter;

@Getter
public final class FriendWishDetail{
private final boolean isWished;
private final WishDetail wishDetail;

@QueryProjection
public FriendWishDetail(WishDetail wishDetail, boolean isWished) {
this.wishDetail=wishDetail;
this.isWished = isWished;
}

public boolean isWished() {
return isWished;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.kakaoshare.backend.domain.wish.dto;

public record FriendsWishRequest(String friendsProviderId, String kakaoAccessToken) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.kakaoshare.backend.domain.wish.dto;

import com.querydsl.core.annotations.QueryProjection;
import lombok.Getter;

@Getter
public final class MyWishDetail {
private final boolean isPublic;
private final WishDetail wishDetail;
@QueryProjection
public MyWishDetail(final boolean isPublic, final WishDetail wishDetail) {
this.isPublic = isPublic;
this.wishDetail = wishDetail;
}

public boolean isPublic() {
return isPublic;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
package org.kakaoshare.backend.domain.wish.dto;
public record WishDetail(Long wishId,Long productId, String productName, Long productPrice, String productPhoto, boolean isPublic) {
import com.querydsl.core.annotations.QueryProjection;
import lombok.Getter;

@Getter
public class WishDetail {
private final Long wishId;
private final Long productId;
private final String productName;
private final Long productPrice;
private final String productPhoto;
private final String brandName;
private final Integer wishCount;
@QueryProjection
public WishDetail(final Long wishId,
final Long productId,
final String productName,
final Long productPrice,
final String productPhoto,
final String brandName,
final Integer wishCount) {
this.wishId = wishId;
this.productId = productId;
this.productName = productName;
this.productPrice = productPrice;
this.productPhoto = productPhoto;
this.brandName = brandName;
this.wishCount = wishCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import org.kakaoshare.backend.domain.wish.repository.query.WishRepositoryCustom;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;


public interface WishRepository extends JpaRepository<Wish, Long> , WishRepositoryCustom {
List<Wish> findByMember_ProviderId(final String providerId);

Optional<Wish> findByMember_ProviderIdAndWishId(final String member_providerId, final Long wishId);
void deleteByMemberAndProduct(final Member member, final Product product);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.kakaoshare.backend.domain.wish.repository.query;

import org.kakaoshare.backend.domain.member.entity.Member;
import org.kakaoshare.backend.domain.wish.dto.WishDetail;
import org.kakaoshare.backend.domain.wish.dto.FriendWishDetail;
import org.kakaoshare.backend.domain.wish.dto.MyWishDetail;
import org.kakaoshare.backend.domain.wish.entity.Wish;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface WishRepositoryCustom {
List<WishDetail> findWishDetailsByProviderId(final String providerId);
Page<MyWishDetail> findWishDetailsByProviderId(final Pageable pageable, final String providerId);
List<FriendWishDetail> findWishDetailsByFriendProviderId(final String providerId, final String friendsProviderId);
boolean isContainInWishList(Wish wish, Member member, Long productId);
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,100 @@
package org.kakaoshare.backend.domain.wish.repository.query;

import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.kakaoshare.backend.domain.member.entity.Member;
import org.kakaoshare.backend.domain.wish.dto.WishDetail;
import org.kakaoshare.backend.domain.wish.dto.FriendWishDetail;
import org.kakaoshare.backend.domain.wish.dto.MyWishDetail;
import org.kakaoshare.backend.domain.wish.dto.QFriendWishDetail;
import org.kakaoshare.backend.domain.wish.dto.QMyWishDetail;
import org.kakaoshare.backend.domain.wish.dto.QWishDetail;
import org.kakaoshare.backend.domain.wish.entity.QWish;
import org.kakaoshare.backend.domain.wish.entity.Wish;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.kakaoshare.backend.common.util.RepositoryUtils.toPage;
import static org.kakaoshare.backend.domain.member.entity.QMember.member;
import static org.kakaoshare.backend.domain.product.entity.QProduct.product;
import static org.kakaoshare.backend.domain.wish.entity.QWish.wish;

@Repository
@RequiredArgsConstructor
public class WishRepositoryCustomImpl implements WishRepositoryCustom {
private static final int FRIEND_WISH_LIMIT = 10;
private final JPAQueryFactory queryFactory;

@Override
public List<WishDetail> findWishDetailsByProviderId(final String providerId) {
return queryFactory
public Page<MyWishDetail> findWishDetailsByProviderId(final Pageable pageable,
final String providerId) {
JPAQuery<Long> countQuery = queryFactory.select(wish.count())
.from(wish)
.join(wish.member, member)
.on(wish.member.providerId.eq(providerId))
.join(wish.product, product);

JPAQuery<MyWishDetail> contentQuery = queryFactory
.select(
Projections.constructor(
WishDetail.class,
wish.wishId,
product.productId,
product.name,
product.price,
product.photo,
wish.isPublic))
new QMyWishDetail(wish.isPublic,
new QWishDetail(
wish.wishId,
product.productId,
product.name,
product.price,
product.photo,
product.brandName,
product.wishCount)))
.from(wish)
.join(wish.member, member)
.on(wish.member.providerId.eq(providerId))
.join(wish.product, product)
.offset(pageable.getOffset())
.limit(pageable.getPageSize());
return toPage(pageable, contentQuery, countQuery);
}

private static BooleanExpression isInMyWishList(final String providerId, final QWish myWish, final QWish friendWish) {
return JPAExpressions.selectOne()
.from(myWish)
.where(myWish.member.providerId.eq(providerId)
.and(myWish.product.eq(friendWish.product)))
.exists();
}

@Override
public List<FriendWishDetail> findWishDetailsByFriendProviderId(final String providerId,
final String friendsProviderId) {
QWish friendWish = new QWish("friendWish");
QWish myWish = new QWish("myWish");

return queryFactory
.select(
new QFriendWishDetail(
new QWishDetail(
friendWish.wishId,
product.productId,
product.name,
product.price,
product.photo,
product.brandName,
product.wishCount
),
isInMyWishList(providerId, myWish, friendWish)
))
.from(friendWish)
.join(friendWish.member, member)
.on(member.providerId.eq(friendsProviderId)
.and(friendWish.isPublic.isTrue()))
.join(friendWish.product, product)
//TODO 2024 05 27 20:04:17 : 친구 위시리스트 조회시 정렬 조건 구체화 후 논의
.orderBy(friendWish.product.wishCount.desc())
.limit(FRIEND_WISH_LIMIT)
.fetch();
}

Expand All @@ -50,7 +108,7 @@ public boolean isContainInWishList(Wish wish, Member member, Long productId) {
.from(QWish.wish)
.where(existsCondition)
.fetchOne();

return count != null && count > 0;
}
}
}
Loading
Loading