Skip to content

Commit

Permalink
refactor: 다른 DTO 없이 SortingReviewDto만 사용하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
70825 committed Sep 13, 2023
1 parent 76d449f commit 59cfb05
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.funeat.review.dto.RankingReviewsResponse;
import com.funeat.review.dto.ReviewCreateRequest;
import com.funeat.review.dto.ReviewFavoriteRequest;
import com.funeat.review.dto.SortingReviewDtoWithoutTag;
import com.funeat.review.dto.SortingReviewResponsesFuture;
import com.funeat.review.dto.SortingReviewRequestFuture;
import com.funeat.review.dto.SortingReviewDto;
Expand All @@ -33,7 +32,6 @@
import com.funeat.review.persistence.ReviewRepository;
import com.funeat.review.persistence.ReviewTagRepository;
import com.funeat.tag.domain.Tag;
import com.funeat.tag.dto.TagDto;
import com.funeat.tag.persistence.TagRepository;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -192,25 +190,21 @@ public SortingReviewResponsesFuture sortingReviewsFuture(final Long memberId,
final Long lastReviewId = request.getLastReviewId();
final Pageable pageable = request.getPageable();

final List<SortingReviewDtoWithoutTag> sortingReviews
final List<SortingReviewDto> sortingReviewsWithoutTags
= reviewRepository.findSortingReviews(findProduct, lastReviewId, pageable);
final List<SortingReviewDto> sortingReviewDtos = mapToReviewDtosWithTags(sortingReviewsWithoutTags);
final Boolean hasNextReview = hasMoreReview(pageable.getPageSize(), sortingReviewDtos.size());

final List<SortingReviewDto> sortingReviewDtoFutures = sortingReviews.stream()
.map(review -> SortingReviewDto.toDto(review, tagRepository.findTagsByReviewId(review.getId())))
.collect(Collectors.toList());

final Boolean hasNextReview = checkLastReview(pageable.getPageSize(), sortingReviews.size());

return SortingReviewResponsesFuture.toResponse(sortingReviewDtoFutures, hasNextReview);
return SortingReviewResponsesFuture.toResponse(sortingReviewDtos, hasNextReview);
}

private List<SortingReviewDto> convertSortingReviewDto(final Member member, final List<Review> sortedReviews) {
return sortedReviews.stream()
.map(review -> SortingReviewDto.toDto(review, member))
private List<SortingReviewDto> mapToReviewDtosWithTags(final List<SortingReviewDto> sortingReviews) {
return sortingReviews.stream()
.map(review -> SortingReviewDto.toDto(review, tagRepository.findTagsByReviewId(review.getId())))
.collect(Collectors.toList());
}

private Boolean checkLastReview(final int pageSize, final int reviewSize) {
private Boolean hasMoreReview(final int pageSize, final int reviewSize) {
return reviewSize == pageSize + ONE;
}
}
63 changes: 41 additions & 22 deletions backend/src/main/java/com/funeat/review/dto/SortingReviewDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,36 @@

public class SortingReviewDto {

private final Long id;
private final String userName;
private final String profileImage;
private final String image;
private final Long rating;
private final List<TagDto> tags;
private final String content;
private final boolean rebuy;
private final Long favoriteCount;
private final boolean favorite;
private final LocalDateTime createdAt;
private Long id;
private String userName;
private String profileImage;
private String image;
private Long rating;
private List<TagDto> tags;
private String content;
private boolean rebuy;
private Long favoriteCount;
private boolean favorite;
private LocalDateTime createdAt;

public SortingReviewDto() {
}

public SortingReviewDto(final Long id, final String userName, final String profileImage, final String image,
final Long rating, final String content,
final boolean rebuy, final Long favoriteCount, final boolean favorite,
final LocalDateTime createdAt) {
this.id = id;
this.userName = userName;
this.profileImage = profileImage;
this.image = image;
this.rating = rating;
this.content = content;
this.rebuy = rebuy;
this.favoriteCount = favoriteCount;
this.favorite = favorite;
this.createdAt = createdAt;
}

public SortingReviewDto(final Long id, final String userName, final String profileImage, final String image,
final Long rating, final List<TagDto> tags,
Expand All @@ -41,22 +60,22 @@ public SortingReviewDto(final Long id, final String userName, final String profi
this.createdAt = createdAt;
}

public static SortingReviewDto toDto(final SortingReviewDtoWithoutTag sortingReviewDtoWithoutTag, final List<Tag> tags) {
public static SortingReviewDto toDto(final SortingReviewDto sortingReviewDto, final List<Tag> tags) {
final List<TagDto> tagDtos = tags.stream()
.map(TagDto::toDto)
.collect(Collectors.toList());

return new SortingReviewDto(sortingReviewDtoWithoutTag.getId(),
sortingReviewDtoWithoutTag.getUserName(),
sortingReviewDtoWithoutTag.getProfileImage(),
sortingReviewDtoWithoutTag.getImage(),
sortingReviewDtoWithoutTag.getRating(),
return new SortingReviewDto(sortingReviewDto.getId(),
sortingReviewDto.getUserName(),
sortingReviewDto.getProfileImage(),
sortingReviewDto.getImage(),
sortingReviewDto.getRating(),
tagDtos,
sortingReviewDtoWithoutTag.getContent(),
sortingReviewDtoWithoutTag.isRebuy(),
sortingReviewDtoWithoutTag.getFavoriteCount(),
sortingReviewDtoWithoutTag.isFavorite(),
sortingReviewDtoWithoutTag.getCreatedAt());
sortingReviewDto.getContent(),
sortingReviewDto.isRebuy(),
sortingReviewDto.getFavoriteCount(),
sortingReviewDto.isFavorite(),
sortingReviewDto.getCreatedAt());
}

public static SortingReviewDto toDto(final Review review, final Member member) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import com.funeat.member.domain.Member;
import com.funeat.product.domain.Product;
import com.funeat.review.domain.Review;
import com.funeat.review.dto.SortingReviewDtoWithoutTag;
import com.funeat.review.dto.SortingReviewDto;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -20,7 +19,7 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {

Page<Review> findReviewsByProduct(final Pageable pageable, final Product product);

@Query("SELECT new com.funeat.review.dto.SortingReviewDtoWithoutTag(r.id, m.nickname, m.profileImage, r.image, r.rating, r.content, r.reBuy, r.favoriteCount, COALESCE(rf.favorite, false), r.createdAt) "
@Query("SELECT new com.funeat.review.dto.SortingReviewDto(r.id, m.nickname, m.profileImage, r.image, r.rating, r.content, r.reBuy, r.favoriteCount, COALESCE(rf.favorite, false), r.createdAt) "
+ "FROM Review r "
+ "JOIN r.member m "
+ "LEFT JOIN r.reviewFavorites rf "
Expand All @@ -38,9 +37,9 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {
+ "WHERE r2.id = :lastReviewId)"
+ ")"
+ ")")
List<SortingReviewDtoWithoutTag> findSortingReviews(@Param("product") final Product product,
@Param("lastReviewId") final Long lastReviewId,
final Pageable pageable);
List<SortingReviewDto> findSortingReviews(@Param("product") final Product product,
@Param("lastReviewId") final Long lastReviewId,
final Pageable pageable);

List<Review> findTop3ByOrderByFavoriteCountDesc();

Expand Down

0 comments on commit 59cfb05

Please sign in to comment.