Skip to content

Commit

Permalink
가격순서정렬
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomeo184 committed Dec 27, 2023
1 parent 0244701 commit f0a9680
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 32 deletions.
18 changes: 11 additions & 7 deletions src/main/java/com/offer/offer/application/OfferService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@
import com.offer.offer.application.response.OffersResponse;
import com.offer.offer.domain.Offer;
import com.offer.offer.domain.OfferRepository;
import com.offer.post.application.request.OfferReadParams;
import com.offer.post.application.request.SortPageReadParam;
import com.offer.post.domain.Post;
import com.offer.post.domain.PostRepository;

import com.offer.review.application.response.ReviewInfoResponse;
import com.offer.review.domain.Review;
import com.offer.review.domain.ReviewRepository;
import java.util.List;

import com.offer.utils.SliceUtils;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -88,13 +84,21 @@ public OffersResponse getAllOffersByPost(Long postId, Long memberId, String sort
}

@Transactional(readOnly = true)
public OfferSummaries getAllOffersByMember(OfferReadParams params, LoginMember loginMember) {
public OfferSummaries getAllOffersByMember(SortPageReadParam params, LoginMember loginMember) {
if (loginMember.getId() == null) {
throw new IllegalArgumentException("잘못된 토큰입니다");
}

Member offerer = memberRepository.getById(loginMember.getId());
List<Offer> offers = offerRepository.findAllByOfferer(offerer);
List<Offer> offers = offerRepository.findAllByOffererOrderByIdDesc(offerer);

if (params.getSort() != null && params.getSort().equals("PRICE_DESC")) {
offers = offerRepository.findAllByOffererOrderByPriceDesc(offerer);
}

if (params.getSort() != null && params.getSort().equals("PRICE_ASC")) {
offers = offerRepository.findAllByOffererOrderByPriceAsc(offerer);
}

if (offers.size() > params.getLimit()) {
offers.remove(params.getLimit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static OfferResponse from(Offer offer) {
new OffererResponse(offer.getOfferer().getId(),
offer.getOfferer().getNickname(),
offer.getPost().getLocation(),
String.valueOf(offer.getOfferer().getOfferLevel()),
offer.getOfferer().getOfferLevel(),
new EnumResponse(offer.getTradeType().name(),
offer.getTradeType().getDescription()),
offer.getOfferer().getProfileImageUrl()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class OffererResponse {
private Long id;
private String nickname;
private String location;
private String level;
private int level;
private EnumResponse tradeType;
private String profileImageUrl;

@Builder
public OffererResponse(Long id, String nickname, String location, String level,
public OffererResponse(Long id, String nickname, String location, int level,
EnumResponse tradeType, String profileImageUrl) {
this.id = id;
this.nickname = nickname;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/offer/offer/domain/OfferRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.offer.post.domain.Post;
import java.util.List;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OfferRepository extends JpaRepository<Offer, Long> {
Expand All @@ -17,5 +15,11 @@ public interface OfferRepository extends JpaRepository<Offer, Long> {

List<Offer> findAllByOfferer(Member offerer);

List<Offer> findAllByOffererOrderByIdDesc(Member offerer);

int countByPostAndOfferer(Post post, Member offerer);

List<Offer> findAllByOffererOrderByPriceDesc(Member offerer);

List<Offer> findAllByOffererOrderByPriceAsc(Member offerer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import com.offer.offer.application.request.OfferCreateRequest;
import com.offer.offer.application.response.OfferSummaries;
import com.offer.offer.application.response.OffersResponse;
import com.offer.post.application.request.OfferReadParams;
import com.offer.post.application.response.PostSummaries;
import com.offer.post.application.request.SortPageReadParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
Expand Down Expand Up @@ -55,7 +54,7 @@ public ResponseEntity<ApiResponse<OffersResponse>> getAllOffersByPost(@PathVaria
@GetMapping("/api/posts/offers")
public ResponseEntity<ApiResponse<OfferSummaries>> showMyOfferPosts(
@Schema(hidden = true) @AuthenticationPrincipal LoginMember loginMember,
OfferReadParams params) {
SortPageReadParam params) {

OfferSummaries response = offerService.getAllOffersByMember(params, loginMember);
return ResponseEntity.ok(
Expand Down
49 changes: 40 additions & 9 deletions src/main/java/com/offer/post/application/LikeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import com.offer.common.response.ResponseMessage;
import com.offer.common.response.exception.BusinessException;
import com.offer.config.Properties;
import com.offer.member.Member;
import com.offer.member.MemberRepository;
import com.offer.post.application.request.SortPageReadParam;
import com.offer.post.application.request.ToggleLikeRequest;
import com.offer.post.application.response.PostSummaries;
import com.offer.post.application.response.PostSummary;
import com.offer.post.domain.Like;
import com.offer.post.domain.LikeRepository;
import com.offer.post.domain.Post;
import com.offer.post.domain.PostRepository;
import com.offer.utils.SliceUtils;
import java.util.Comparator;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -48,13 +47,45 @@ public void toggleStatus(ToggleLikeRequest request, Long memberId) {
}

@Transactional(readOnly = true)
public List<PostSummary> findLikePosts(int page, Long memberId) {
PageRequest pageRequest = PageRequest.of(SliceUtils.getSliceNumber(page), Properties.DEFAULT_SLICE_SIZE);
public PostSummaries findLikePosts(SortPageReadParam params, Long memberId) {
if (memberId == null) {
throw new IllegalArgumentException("잘못된 토큰입니다");
}

Slice<Like> likes = likeRepository.findSliceByMemberId(pageRequest, memberId);
Member member = memberRepository.getById(memberId);
List<Like> likes = likeRepository.findAllByMemberOrderByIdDesc(member);
List<Post> posts = likes.stream()
.map(Like::getPost)
.collect(Collectors.toList());

return likes.stream()
.map(l -> PostSummary.from(l.getPost(), true))
if (params.getSort() != null && params.getSort().equals("PRICE_DESC")) {
posts = posts.stream()
.sorted((o1, o2) -> -Integer.compare(o1.getPrice(), o2.getPrice()))
.collect(Collectors.toList());
}

if (posts.size() > params.getLimit()) {
posts.remove(params.getLimit());
return PostSummaries.builder()
.posts(posts.stream()
.map(post -> {
int likeCount = likeRepository.countByPost(post);
return PostSummary.from(post, true, likeCount);
})
.collect(Collectors.toList()))
.hasNext(true)
.build();
}


return PostSummaries.builder()
.posts(posts.stream()
.map(post -> {
int likeCount = likeRepository.countByPost(post);
return PostSummary.from(post, true, likeCount);
})
.collect(Collectors.toList()))
.hasNext(false)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.offer.post.application.request;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
public class OfferReadParams {
@Setter
@NoArgsConstructor
public class SortPageReadParam {

private String sort;
private Long lastId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ public static PostSummary from(Post post, Set<Long> likePostIds, int likeCount,
.build();
}

public static PostSummary from(Post post, boolean isLiked) {
public static PostSummary from(Post post, boolean isLiked, int likeCount) {
return PostSummary.builder()
.id(post.getId())
.title(post.getTitle())
.price(post.getPrice())
.location(post.getLocation())
.thumbnailImageUrl(post.getThumbnailImageUrl())
.liked(isLiked)
.likeCount(likeCount)
.tradeStatus(new EnumResponse(post.getTradeStatus().name(), post.getTradeStatus().getDescription()))
.createdAt(post.getCreatedAt())
.seller(SellerDetail.from(post.getSeller()))
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/offer/post/domain/LikeRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.offer.post.domain;

import com.offer.member.Member;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -15,7 +16,8 @@ public interface LikeRepository extends JpaRepository<Like, Long> {

Slice<Like> findSliceByMemberId(PageRequest pageRequest, Long memberId);

List<Like> findAllByMemberId(Long memberId); // TODO: remove from PostQueryRepository
List<Like> findAllByMemberId(Long memberId);
List<Like> findAllByMemberOrderByIdDesc(Member member);

int countByMemberId(Long memberId);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/offer/post/domain/PostQueryRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public PostSummaries searchPost(PostReadParams params, Long loginMemberId) {
memberRepository.getById(sellerId) : null;

Set<Long> likePostIds = getLikePostId(loginMemberId);
boolean priceDesc = "PRICE_DESC".equals(params.getSort());

BooleanBuilder booleanBuilder = new BooleanBuilder();
if (params.getLastId() != null) {
Expand All @@ -48,7 +49,7 @@ public PostSummaries searchPost(PostReadParams params, Long loginMemberId) {
sellerEq(seller),
tradeStatus(TradeStatus.from(params.getTradeStatus()))
)
.orderBy(post.id.desc())
.orderBy(priceDesc ? post.price.desc() : post.id.desc())
.limit(params.getLimit() + 1)
.fetch();

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/offer/post/presentation/LikeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.offer.common.response.ApiResponse;
import com.offer.common.response.ResponseMessage;
import com.offer.post.application.LikeService;
import com.offer.post.application.request.SortPageReadParam;
import com.offer.post.application.request.ToggleLikeRequest;
import com.offer.post.application.response.PostSummaries;
import com.offer.post.application.response.PostSummary;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -37,11 +39,11 @@ public ResponseEntity<ApiResponse> toggle(

@Operation(summary = "내가 좋아한 모든 게시물", security = {@SecurityRequirement(name = "jwt")})
@GetMapping("/posts/likes")
public ResponseEntity<ApiResponse<List<PostSummary>>> getAll(
public ResponseEntity<ApiResponse<PostSummaries>> getAll(
@Schema(hidden = true) @AuthenticationPrincipal LoginMember loginMember,
@RequestParam(required = true) int page) {
SortPageReadParam params) {

List<PostSummary> response = likeService.findLikePosts(page, loginMember.getId());
PostSummaries response = likeService.findLikePosts(params, loginMember.getId());

return ResponseEntity.ok(
ApiResponse.of(ResponseMessage.SUCCESS, response)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ insert into sort_item(sort_group_id, name, code) values (2, '최신순', 'CREATE


insert into post(price, created_at, modified_at, seller_id, category, description, location, product_condition, thumbnail_image_url, title, trade_type, trade_status)
values (10000, now(), now(), 1, 'MEN_FASHION', '남성 청바지 팔아요~~', '동작구 사당동', 'SECONDHAND', 'https://picsum.photos/200', '남성 청바지 팝니다.', 'FACE_TO_FACE', 'SELLING');
values (30000, now(), now(), 1, 'MEN_FASHION', '남성 청바지 팔아요~~', '동작구 사당동', 'SECONDHAND', 'https://picsum.photos/200', '남성 청바지 팝니다.', 'FACE_TO_FACE', 'SELLING');

insert into post(price, created_at, modified_at, seller_id, category, description, location, product_condition, thumbnail_image_url, title, trade_type, trade_status)
values (11000, now(), now(), 1, 'MEN_FASHION', '후드티 팔아요~~', '동작구 사당동', 'SECONDHAND', 'https://picsum.photos/200', '후드티 판매', 'FACE_TO_FACE', 'SELLING');
Expand Down

0 comments on commit f0a9680

Please sign in to comment.