Skip to content

Commit

Permalink
Merge pull request #41 from 9oormthonUniv-seoultech/feat/#40
Browse files Browse the repository at this point in the history
feat: 전체 리뷰 조회 API (페이징 기술 적용)
Jeongh00 authored Oct 22, 2024
2 parents 11ddf81 + 2bfea9f commit 380fd18
Showing 11 changed files with 176 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

import java.util.List;

public record ReviewGetRecentResponseDto(
public record ReviewGetResponseDto(
int reviewCount,
List<ReviewPreviewDto> reviews
) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pocket.domain.port.review;

import com.pocket.domain.dto.review.ReviewGetResponseDto;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface ReviewGetAllPort {

ReviewGetResponseDto getAllReviews(Long photoboothId, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.pocket.domain.port.review;

import com.pocket.domain.dto.review.ReviewGetRecentResponseDto;
import com.pocket.domain.dto.review.ReviewGetResponseDto;

public interface ReviewGetRecentPort {

ReviewGetRecentResponseDto getRecentReview(Long photoboothId);
ReviewGetResponseDto getRecentReview(Long photoboothId);

}
Original file line number Diff line number Diff line change
@@ -6,12 +6,13 @@
import com.pocket.domain.port.review.*;
import com.pocket.domain.usecase.review.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;

import java.util.List;

@DomainService
@RequiredArgsConstructor
public class ReviewService implements ReviewRegisterUseCase, ReviewGet6ImagesUseCase, ReviewGetRecentUseCase, ReviewGetAllImagesUseCase, ReviewBoothFeatureCountUseCase, ReviewPhotoFeatureCountUseCase, ReviewGetBoothFeatureUseCase, ReviewGetPhotoFeatureUseCase
public class ReviewService implements ReviewRegisterUseCase, ReviewGet6ImagesUseCase, ReviewGetRecentUseCase, ReviewGetAllImagesUseCase, ReviewBoothFeatureCountUseCase, ReviewPhotoFeatureCountUseCase, ReviewGetBoothFeatureUseCase, ReviewGetPhotoFeatureUseCase, ReviewGetAllUseCase

{

@@ -23,6 +24,7 @@ public class ReviewService implements ReviewRegisterUseCase, ReviewGet6ImagesUse
private final ReviewPhotoFeatureCountPort reviewPhotoFeatureCountPort;
private final ReviewGetBoothFeaturePort reviewGetBoothFeaturePort;
private final ReviewGetPhotoFeaturePort reviewGetPhotoFeaturePort;
private final ReviewGetAllPort reviewGetAllPort;

@Override
public ReviewRegisterResponseDto registerReviewResponse(ReviewRegisterRequestDto reviewRegisterRequestDto, String name) {
@@ -35,7 +37,7 @@ public ReviewGet6ImagesResponseDto get6Images(Long photoboothId) {
}

@Override
public ReviewGetRecentResponseDto getRecentReview(Long photoboothId) {
public ReviewGetResponseDto getRecentReview(Long photoboothId) {
return reviewGetRecentPort.getRecentReview(photoboothId);
}

@@ -63,4 +65,9 @@ public List<BoothFeatureDto> getBoothFeatures() {
public List<PhotoFeatureDto> getPhotoFeatures() {
return reviewGetPhotoFeaturePort.getPhotoFeatures();
}

@Override
public ReviewGetResponseDto getAllReviews(Long photoboothId, Pageable pageable) {
return reviewGetAllPort.getAllReviews(photoboothId, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pocket.domain.usecase.review;

import com.pocket.domain.dto.review.ReviewGetResponseDto;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface ReviewGetAllUseCase {

ReviewGetResponseDto getAllReviews(Long photoboothId, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.pocket.domain.usecase.review;

import com.pocket.domain.dto.review.ReviewGetRecentResponseDto;
import com.pocket.domain.dto.review.ReviewGetResponseDto;


public interface ReviewGetRecentUseCase {

ReviewGetRecentResponseDto getRecentReview(Long photoboohtId);
ReviewGetResponseDto getRecentReview(Long photoboohtId);

}
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
import com.pocket.domain.dto.user.UserInfoDTO;
import com.pocket.domain.usecase.review.*;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@@ -23,6 +25,7 @@ public class ReviewController implements ReviewControllerDocs {
private final ReviewPhotoFeatureCountUseCase reviewPhotoFeatureCountUseCase;
private final ReviewGetBoothFeatureUseCase reviewGetBoothFeatureUseCase;
private final ReviewGetPhotoFeatureUseCase reviewGetPhotoFeatureUseCase;
private final ReviewGetAllUseCase reviewGetAllUseCase;

@PostMapping
public ApplicationResponse<ReviewRegisterResponseDto> postReview(
@@ -44,6 +47,7 @@ public ApplicationResponse<List<PhotoFeatureDto>> getAllPhotoFeature() {
return ApplicationResponse.ok(response);
}


@GetMapping("/images/{photobooth_id}")
public ApplicationResponse<ReviewGet6ImagesResponseDto> getReviewHomeImage(
@PathVariable("photobooth_id") Long photoboothId
@@ -53,10 +57,10 @@ public ApplicationResponse<ReviewGet6ImagesResponseDto> getReviewHomeImage(
}

@GetMapping("/reviews/{photobooth_id}")
public ApplicationResponse<ReviewGetRecentResponseDto> getRecentReview(
public ApplicationResponse<ReviewGetResponseDto> getRecentReview(
@PathVariable("photobooth_id") Long photoboothId
) {
ReviewGetRecentResponseDto response = reviewGetRecentUseCase.getRecentReview(photoboothId);
ReviewGetResponseDto response = reviewGetRecentUseCase.getRecentReview(photoboothId);
return ApplicationResponse.ok(response);
}

@@ -84,4 +88,13 @@ public ApplicationResponse<List<PhotoFeatureCountDto>> getReviewPhotoFeatures(
return ApplicationResponse.ok(response);
}

@GetMapping("/allreviews/{photobooth_id}")
public ApplicationResponse<ReviewGetResponseDto> getAllReviews(
@PathVariable("photobooth_id") Long photoboothId,
@ParameterObject final Pageable pageable
) {
ReviewGetResponseDto response = reviewGetAllUseCase.getAllReviews(photoboothId, pageable);
return ApplicationResponse.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -5,11 +5,14 @@
import com.pocket.domain.dto.review.*;
import com.pocket.domain.dto.user.UserInfoDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
@@ -52,7 +55,7 @@ ApplicationResponse<ReviewGet6ImagesResponseDto> getReviewHomeImage(
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))})
})
@Operation(summary = "최신 리뷰 조회", description = "특정 포토부스에 대한 최신 리뷰를 조회하는 API")
ApplicationResponse<ReviewGetRecentResponseDto> getRecentReview(
ApplicationResponse<ReviewGetResponseDto> getRecentReview(
@PathVariable("photobooth_id") Long photoboothId
);

@@ -111,4 +114,20 @@ ApplicationResponse<List<PhotoFeatureCountDto>> getReviewPhotoFeatures(
})
@Operation(summary = "포토부스의 모든 사진 특징 조회", description = "포토부스에서 찍힌 모든 사진의 특징을 가져오는 API")
ApplicationResponse<List<PhotoFeatureDto>> getAllPhotoFeature();

@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "400", description = "BAD REQUEST",
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}),
@ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR",
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))})
})
@Operation(summary = "전체 리뷰 조회", description = "페이징을 이용한 전체 리뷰 조회 API (ex. /api/v1/review/allreviews/336?page=0&size=10&sort=id,desc)")
ApplicationResponse<ReviewGetResponseDto> getAllReviews(
@Parameter(description = "포토부스 ID", example = "336")
@PathVariable("photobooth_id") Long photoboothId,

@ParameterObject Pageable pageable
);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.pocket.outbound.adapter.review.adapter;


import com.pocket.core.aop.annotation.AdapterService;
import com.pocket.core.exception.review.ReviewCustomException;
import com.pocket.core.exception.review.ReviewErrorCode;
import com.pocket.domain.dto.review.ReviewGetResponseDto;
import com.pocket.domain.dto.review.ReviewPreviewDto;
import com.pocket.domain.port.review.ReviewGetAllPort;
import com.pocket.outbound.adapter.review.mapper.ReviewOutBoundMapper;
import com.pocket.outbound.entity.review.JpaBoothFeature;
import com.pocket.outbound.entity.review.JpaPhotoFeature;
import com.pocket.outbound.entity.review.JpaReview;
import com.pocket.outbound.entity.review.JpaReviewImage;
import com.pocket.outbound.repository.review.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@AdapterService
@RequiredArgsConstructor
public class ReviewGetAllAdapter implements ReviewGetAllPort {

private final ReviewRepository reviewRepository;
private final ReviewImageRepository reviewImageRepository;
private final ReviewBoothFeatureRepository reviewBoothFeatureRepository;
private final ReviewPhotoFeatureRepository reviewPhotoFeatureRepository;
private final BoothFeatureRepository boothFeatureRepository;
private final PhotoFeatureRepository photoFeatureRepository;
private final ReviewOutBoundMapper reviewOutBoundMapper;

@Override
public ReviewGetResponseDto getAllReviews(Long photoboothId, Pageable pageable) {
int totalReviewCount = reviewRepository.countByPhotoBoothId(photoboothId);

Page<JpaReview> reviews = reviewRepository.findByPhotoBoothId(photoboothId, pageable);

List<ReviewPreviewDto> reviewPreviews = reviews.stream()
.map(this::createReviewPreview)
.collect(Collectors.toList());

return new ReviewGetResponseDto(totalReviewCount, reviewPreviews);
}

private ReviewPreviewDto createReviewPreview(JpaReview review) {
String imageUrl = getFirstImageUrlForReview(review.getId());
int imageCount = getReviewImageCount(review.getId());

List<String> descriptions = getDescriptionsForReview(review);

return reviewOutBoundMapper.toReviewPreviewDto(descriptions, review, imageUrl, imageCount);
}

private String getFirstImageUrlForReview(Long reviewId) {
List<JpaReviewImage> images = reviewImageRepository.findByReviewId(reviewId);
return images.isEmpty() ? "" : images.get(0).getImage().getImageUrl();
}

private int getReviewImageCount(Long reviewId) {
List<JpaReviewImage> images = reviewImageRepository.findByReviewId(reviewId);
return images.size();
}

private List<String> getDescriptionsForReview(JpaReview review) {
List<String> descriptions = new ArrayList<>();

List<Long> boothFeatureIds = reviewBoothFeatureRepository.findBoothFeatureIdByReviewId(review.getId());
List<JpaBoothFeature> boothFeatures = getBoothFeatures(boothFeatureIds);
boothFeatures.forEach(boothFeature -> descriptions.add(boothFeature.getBoothFeature().getDescription()));

List<Long> photoFeatureIds = reviewPhotoFeatureRepository.findPhotoFeatureIdByReviewId(review.getId());
List<JpaPhotoFeature> photoFeatures = getPhotoFeatures(photoFeatureIds);
photoFeatures.forEach(photoFeature -> descriptions.add(photoFeature.getPhotoFeature().getDescription()));

return descriptions;
}

private List<JpaBoothFeature> getBoothFeatures(List<Long> boothFeatureIds) {
return boothFeatureIds.stream()
.map(boothFeatureId -> boothFeatureRepository.findById(boothFeatureId)
.orElseThrow(() -> new ReviewCustomException(ReviewErrorCode.BOOTH_FEATURE_NOT_FOUND)))
.collect(Collectors.toList());
}

private List<JpaPhotoFeature> getPhotoFeatures(List<Long> photoFeatureIds) {
return photoFeatureIds.stream()
.map(photoFeatureId -> photoFeatureRepository.findById(photoFeatureId)
.orElseThrow(() -> new ReviewCustomException(ReviewErrorCode.PHOTO_FEATURE_NOT_FOUND)))
.collect(Collectors.toList());
}


}
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import com.pocket.core.aop.annotation.AdapterService;
import com.pocket.core.exception.review.ReviewCustomException;
import com.pocket.core.exception.review.ReviewErrorCode;
import com.pocket.domain.dto.review.ReviewGetRecentResponseDto;
import com.pocket.domain.dto.review.ReviewGetResponseDto;
import com.pocket.domain.dto.review.ReviewPreviewDto;
import com.pocket.domain.port.review.ReviewGetRecentPort;
import com.pocket.outbound.adapter.review.mapper.ReviewOutBoundMapper;
@@ -32,7 +32,7 @@ public class ReviewGetRecentAdapter implements ReviewGetRecentPort {
private final ReviewOutBoundMapper reviewOutBoundMapper;

@Override
public ReviewGetRecentResponseDto getRecentReview(Long photoboothId) {
public ReviewGetResponseDto getRecentReview(Long photoboothId) {
int totalReviewCount = reviewRepository.countByPhotoBoothId(photoboothId);

List<JpaReview> recentReviews = reviewRepository.findTop2ByPhotoBoothIdOrderByIdDesc(photoboothId);
@@ -67,6 +67,6 @@ public ReviewGetRecentResponseDto getRecentReview(Long photoboothId) {
return reviewOutBoundMapper.toReviewPreviewDto(descriptions, review, imageUrl, imageCount);
}).collect(Collectors.toList());

return new ReviewGetRecentResponseDto(totalReviewCount, reviewPreviews);
return new ReviewGetResponseDto(totalReviewCount, reviewPreviews);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pocket.outbound.repository.review;

import com.pocket.outbound.entity.review.JpaReview;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
@@ -17,4 +19,5 @@ public interface ReviewRepository extends JpaRepository<JpaReview, Long> {
@Query("SELECT r.id FROM JpaReview r WHERE r.photoBooth.id = :photoBoothId")
List<Long> findReviewIdsByPhotoBoothId(@Param("photoBoothId") Long photoBoothId);

Page<JpaReview> findByPhotoBoothId(Long photoboothId, Pageable pageable);
}

0 comments on commit 380fd18

Please sign in to comment.