Skip to content

Commit

Permalink
게시글 조회 쿼리 조건 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomeo184 committed Nov 30, 2023
1 parent f89fef5 commit e401a52
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.offer.authentication.application.response;

import lombok.Builder;
import lombok.Getter;

/**
* 타 사용자 프로필 조회
* */
@Getter
public class MemberProfileResponse {

private Long id;
private String nickname;
private String profileImageUrl;
private long offerLevel;
private long sellingProductCount;
private long soldProductCount;
private long reviewCount;

@Builder
public MemberProfileResponse(Long id, String nickname, String profileImageUrl,
long offerLevel, long sellingProductCount, long soldProductCount, long reviewCount) {
this.id = id;
this.nickname = nickname;
this.profileImageUrl = profileImageUrl;
this.offerLevel = offerLevel;
this.sellingProductCount = sellingProductCount;
this.soldProductCount = soldProductCount;
this.reviewCount = reviewCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ public class MemberResponse {
private Long id;
private String nickname;
private String profileImageUrl;
private long offerLevel;
private long sellingProductCount;
private long soldProductCount;
private long reviewCount;
private long likeProductCount;

@Builder
public MemberResponse(Long id, String nickname, String profileImageUrl,
long sellingProductCount,
long offerLevel, long sellingProductCount,
long soldProductCount, long reviewCount, long likeProductCount) {
this.id = id;
this.nickname = nickname;
this.profileImageUrl = profileImageUrl;
this.offerLevel = offerLevel;
this.sellingProductCount = sellingProductCount;
this.soldProductCount = soldProductCount;
this.reviewCount = reviewCount;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/offer/member/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.offer.member;

import com.offer.authentication.application.response.MemberProfileResponse;
import com.offer.authentication.application.response.MemberResponse;
import com.offer.authentication.application.response.OAuthLoginResponse;
import com.offer.authentication.presentation.AuthenticationPrincipal;
import com.offer.authentication.presentation.LoginMember;
import com.offer.common.response.ApiResponse;
Expand All @@ -18,6 +18,7 @@
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.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -40,7 +41,7 @@ public ResponseEntity<ApiResponse<Map<String, Boolean>>> getNicknameDuplication(
);
}

@Operation(summary = "회원정보 조회(토큰으로)", security = {@SecurityRequirement(name = "jwt")})
@Operation(summary = "내 프로필 조회(토큰으로)", security = {@SecurityRequirement(name = "jwt")})
@GetMapping("/member/access-token/me")
public ResponseEntity<ApiResponse<MemberResponse>> getMember(
@Schema(hidden = true) @AuthenticationPrincipal LoginMember loginMember) {
Expand All @@ -50,8 +51,16 @@ public ResponseEntity<ApiResponse<MemberResponse>> getMember(
);
}

@Operation(summary = "회원정보 수정", security = {@SecurityRequirement(name = "jwt")})
@Operation(summary = "타 사용자 프로필 조회")
@GetMapping("/member/{memberId}")
public ResponseEntity<ApiResponse<MemberProfileResponse>> getMember(@PathVariable Long memberId) {
return ResponseEntity.ok(
ApiResponse.of(ResponseMessage.SUCCESS, memberService.getMemberProfile(memberId))
);
}

@Operation(summary = "회원정보 수정", security = {@SecurityRequirement(name = "jwt")})
@PutMapping("/member/{memberId}")
public ResponseEntity<ApiResponse<Long>> updateMember(
@Schema(hidden = true) @AuthenticationPrincipal LoginMember loginMember,
@PathVariable Long memberId, MemberUpdateRequest request) {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/offer/member/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.offer.member;

import com.offer.authentication.application.response.MemberProfileResponse;
import com.offer.authentication.application.response.MemberResponse;
import com.offer.member.request.MemberUpdateRequest;
import com.offer.post.domain.LikeRepository;
Expand Down Expand Up @@ -38,6 +39,7 @@ public MemberResponse getMember(Long memberId) {
return MemberResponse.builder()
.id(member.getId())
.profileImageUrl(member.getProfileImageUrl())
.offerLevel(member.getOfferLevel())
.nickname(member.getNickname())
.sellingProductCount(selling)
.soldProductCount(sold)
Expand All @@ -60,4 +62,23 @@ public Long updateMember(Long memberId, MemberUpdateRequest request) {
member.changeNickname(nickname);
return member.getId();
}

public MemberProfileResponse getMemberProfile(Long memberId) {
if (memberId == null) {
throw new IllegalArgumentException("memberId is null");
}
Member member = memberRepository.getById(memberId);
int selling = postRepository.countBySellerAndTradeStatus(member, TradeStatus.SELLING);
int sold = postRepository.countBySellerAndTradeStatus(member, TradeStatus.SOLD);
int review = reviewRepository.countByRevieweeIdOrReviewerId(memberId, memberId);
return MemberProfileResponse.builder()
.id(member.getId())
.nickname(member.getNickname())
.profileImageUrl(member.getProfileImageUrl())
.offerLevel(member.getOfferLevel())
.reviewCount(review)
.sellingProductCount(selling)
.soldProductCount(sold)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class PostReadParams {
private String sort;
private String tradeType;
private String category;
private Long sellerId;
private String tradeStatus;
private int minPrice = 0;
private int maxPrice = Integer.MAX_VALUE;
private Long lastId;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/offer/post/application/response/PostSummary.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.offer.post.application.response;

import com.offer.post.domain.Post;
import com.offer.post.domain.TradeStatus;
import java.time.LocalDateTime;
import java.util.Set;
import lombok.Builder;
Expand All @@ -18,22 +19,26 @@ public class PostSummary {
private String location;
private String thumbnailImageUrl;
private boolean liked;
private TradeStatus tradeStatus;
private int likeCount;
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime createdAt;

@Builder(toBuilder = true)
public PostSummary(Long id, String title, int price, String location, String thumbnailImageUrl,
boolean liked, LocalDateTime createdAt) {
boolean liked, TradeStatus tradeStatus, int likeCount, LocalDateTime createdAt) {
this.id = id;
this.title = title;
this.price = price;
this.location = location;
this.thumbnailImageUrl = thumbnailImageUrl;
this.liked = liked;
this.tradeStatus = tradeStatus;
this.likeCount = likeCount;
this.createdAt = createdAt;
}

public static PostSummary from(Post post, Set<Long> likePostIds) {
public static PostSummary from(Post post, Set<Long> likePostIds, int likeCount) {
boolean liked = false;
if (likePostIds.contains(post.getId())) {
liked = true;
Expand All @@ -46,6 +51,8 @@ public static PostSummary from(Post post, Set<Long> likePostIds) {
.location(post.getLocation())
.thumbnailImageUrl(post.getThumbnailImageUrl())
.liked(liked) // TODO: 2023/09/24 NOT IMPLEMENTED
.tradeStatus(post.getTradeStatus())
.likeCount(likeCount)
.createdAt(post.getCreatedAt())
.build();
}
Expand All @@ -58,6 +65,7 @@ public static PostSummary from(Post post, boolean isLiked) {
.location(post.getLocation())
.thumbnailImageUrl(post.getThumbnailImageUrl())
.liked(isLiked)
.tradeStatus(post.getTradeStatus())
.createdAt(post.getCreatedAt())
.build();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/offer/post/domain/LikeRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface LikeRepository extends JpaRepository<Like, Long> {
List<Like> findAllByMemberId(Long memberId); // TODO: remove from PostQueryRepository

int countByMemberId(Long memberId);

int countByPost(Post post);
}
32 changes: 27 additions & 5 deletions src/main/java/com/offer/post/domain/PostQueryRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.offer.post.domain.QPost.post;

import com.offer.member.Member;
import com.offer.member.MemberRepository;
import com.offer.post.application.request.PostReadParams;
import com.offer.post.application.response.PostSummaries;
import com.offer.post.application.response.PostSummary;
Expand All @@ -22,10 +24,14 @@ public class PostQueryRepository {

private final JPAQueryFactory query;
private final LikeRepository likeRepository;
private final MemberRepository memberRepository;

public PostSummaries searchPost(PostReadParams params, Long memberId) {
public PostSummaries searchPost(PostReadParams params, Long loginMemberId) {
Long sellerId = params.getSellerId();
Member seller = sellerId != null && memberRepository.existsById(sellerId) ?
memberRepository.getById(sellerId) : null;

Set<Long> likePostIds = getLikePostId(memberId);
Set<Long> likePostIds = getLikePostId(loginMemberId);

BooleanBuilder booleanBuilder = new BooleanBuilder();
if (params.getLastId() != null) {
Expand All @@ -35,7 +41,9 @@ public PostSummaries searchPost(PostReadParams params, Long memberId) {
List<Post> posts = query.selectFrom(post)
.where(lastIdLt(params.getLastId()),
categoryEq(params.getCategory()),
priceBetween(params.getMinPrice(), params.getMaxPrice())
priceBetween(params.getMinPrice(), params.getMaxPrice()),
sellerEq(seller),
tradeStatus(TradeStatus.from(params.getTradeStatus()))
)
.orderBy(post.id.desc())
.limit(params.getLimit() + 1)
Expand All @@ -45,15 +53,21 @@ public PostSummaries searchPost(PostReadParams params, Long memberId) {
posts.remove(params.getLimit());
return PostSummaries.builder()
.posts(posts.stream()
.map(post -> PostSummary.from(post, likePostIds))
.map(post -> {
int likeCount = likeRepository.countByPost(post);
return PostSummary.from(post, likePostIds, likeCount);
})
.collect(Collectors.toList()))
.hasNext(true)
.build();
}

return PostSummaries.builder()
.posts(posts.stream()
.map(post -> PostSummary.from(post, likePostIds))
.map(post -> {
int likeCount = likeRepository.countByPost(post);
return PostSummary.from(post, likePostIds, likeCount);
})
.collect(Collectors.toList()))
.hasNext(false)
.build();
Expand Down Expand Up @@ -83,6 +97,14 @@ private BooleanExpression priceGoe(Integer minPrice) {
return minPrice != null ? post.price.goe(minPrice) : null;
}

private BooleanExpression sellerEq(Member seller) {
return seller != null ? post.seller.eq(seller) : null;
}

private BooleanExpression tradeStatus(TradeStatus tradeStatus) {
return tradeStatus != TradeStatus.UNKNOWN ? post.tradeStatus.eq(tradeStatus) : null;
}

private BooleanExpression priceLoe(Integer maxPrice) {
return maxPrice != null ? post.price.loe(maxPrice) : null;
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/offer/post/domain/TradeStatus.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package com.offer.post.domain;

import java.util.Arrays;
import lombok.Getter;

@Getter
public enum TradeStatus {
SELLING,
SOLD
SOLD,
UNKNOWN
;

public static TradeStatus from(String name) {
if (name == null) {
return UNKNOWN;
}
return Arrays.stream(values())
.filter(tradeStatus -> tradeStatus.name().equals(name))
.findFirst()
.orElse(UNKNOWN);
}
}

0 comments on commit e401a52

Please sign in to comment.