Skip to content

Commit

Permalink
refactor: exception 처리 삭제 및 팩터리 메서드로 코드 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
pminsung12 committed Jul 15, 2024
1 parent f337af0 commit dfea456
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.depromeet.spot.jpa.review.entity;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -10,12 +12,14 @@
import org.depromeet.spot.jpa.common.entity.BaseEntity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "reviews")
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class ReviewEntity extends BaseEntity {

@Column(name = "user_id", nullable = false)
Expand All @@ -42,6 +46,33 @@ public class ReviewEntity extends BaseEntity {
@Column(name = "deleted_at")
private LocalDateTime deletedAt;

public static Review createReviewWithDetails(
ReviewEntity entity,
List<ReviewImageEntity> images,
List<ReviewKeywordEntity> keywords) {
return Review.builder()
.id(entity.getId())
.userId(entity.getUserId())
.stadiumId(entity.getStadiumId())
.blockId(entity.getBlockId())
.rowId(entity.getRowId())
.seatNumber(entity.getSeatNumber())
.dateTime(entity.getDateTime())
.content(entity.getContent())
.createdAt(entity.getCreatedAt())
.updatedAt(entity.getUpdatedAt())
.deletedAt(entity.getDeletedAt())
.images(
images.stream()
.map(ReviewImageEntity::toDomain)
.collect(Collectors.toList()))
.keywords(
keywords.stream()
.map(ReviewKeywordEntity::toDomain)
.collect(Collectors.toList()))
.build();
}

public static ReviewEntity from(Review review) {
return new ReviewEntity(
review.getUserId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ public List<KeywordCount> findTopKeywordsByBlockId(Long stadiumId, Long blockId,
.fetch();
}

public List<ReviewImageEntity> findImagesByReviewId(Long reviewId) {
public List<ReviewImageEntity> findImagesByReviewIds(List<Long> reviewIds) {
return queryFactory
.selectFrom(reviewImageEntity)
.where(
reviewImageEntity
.reviewId
.eq(reviewId)
.in(reviewIds)
.and(reviewImageEntity.deletedAt.isNull()))
.fetch();
}

public List<ReviewKeywordEntity> findKeywordsByReviewId(Long reviewId) {
public List<ReviewKeywordEntity> findKeywordsByReviewIds(List<Long> reviewIds) {
return queryFactory
.selectFrom(reviewKeywordEntity)
.where(reviewKeywordEntity.reviewId.eq(reviewId))
.where(reviewKeywordEntity.reviewId.in(reviewIds))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import java.util.List;
import java.util.stream.Collectors;

import org.depromeet.spot.common.exception.review.ReviewException;
import org.depromeet.spot.common.exception.review.ReviewException.InvalidReviewDataException;
import org.depromeet.spot.domain.review.KeywordCount;
import org.depromeet.spot.domain.review.Review;
import org.depromeet.spot.domain.review.ReviewImage;
import org.depromeet.spot.domain.review.ReviewKeyword;
import org.depromeet.spot.jpa.review.entity.ReviewEntity;
import org.depromeet.spot.jpa.review.entity.ReviewImageEntity;
import org.depromeet.spot.jpa.review.entity.ReviewKeywordEntity;
Expand All @@ -28,18 +24,13 @@ public List<Review> findByBlockId(
List<ReviewEntity> reviews =
reviewCustomRepository.findByBlockIdWithFilters(
stadiumId, blockId, rowId, seatNumber, offset, limit);
if (reviews.isEmpty()) {
throw new ReviewException.ReviewNotFoundException(
"No review found for blockId:" + blockId);
}
return reviews.stream().map(this::fetchReviewDetails).collect(Collectors.toList());
}

@Override
public int countByBlockId(Long stadiumId, Long blockId, Long rowId, Long seatNumber) {
return (int)
reviewCustomRepository.countByBlockIdWithFilters(
stadiumId, blockId, rowId, seatNumber);
public Long countByBlockId(Long stadiumId, Long blockId, Long rowId, Long seatNumber) {
return reviewCustomRepository.countByBlockIdWithFilters(
stadiumId, blockId, rowId, seatNumber);
}

@Override
Expand All @@ -48,35 +39,11 @@ public List<KeywordCount> findTopKeywordsByBlockId(Long stadiumId, Long blockId,
}

private Review fetchReviewDetails(ReviewEntity reviewEntity) {
List<ReviewImage> images =
reviewCustomRepository.findImagesByReviewId(reviewEntity.getId()).stream()
.map(ReviewImageEntity::toDomain)
.collect(Collectors.toList());
List<ReviewImageEntity> images =
reviewCustomRepository.findImagesByReviewIds(List.of(reviewEntity.getId()));
List<ReviewKeywordEntity> keywords =
reviewCustomRepository.findKeywordsByReviewIds(List.of(reviewEntity.getId()));

List<ReviewKeyword> keywords =
reviewCustomRepository.findKeywordsByReviewId(reviewEntity.getId()).stream()
.map(ReviewKeywordEntity::toDomain)
.collect(Collectors.toList());

Review review = reviewEntity.toDomain();
if (review == null) {
throw new InvalidReviewDataException(
"Failed to convert entity to domain for reviewId: " + reviewEntity.getId());
}
return Review.builder()
.id(review.getId())
.userId(review.getUserId())
.stadiumId(review.getStadiumId())
.blockId(review.getBlockId())
.rowId(review.getRowId())
.seatNumber(review.getSeatNumber())
.dateTime(review.getDateTime())
.content(review.getContent())
.createdAt(review.getCreatedAt())
.updatedAt(review.getUpdatedAt())
.deletedAt(review.getDeletedAt())
.images(images)
.keywords(keywords)
.build();
return ReviewEntity.createReviewWithDetails(reviewEntity, images, keywords);
}
}

0 comments on commit dfea456

Please sign in to comment.