Skip to content

Commit

Permalink
Merge pull request #175 from PawWithU/feat/174-review-api
Browse files Browse the repository at this point in the history
[Feature] 이동봉사 후기 조회 API 추가 수정
  • Loading branch information
hojeong2747 authored May 5, 2024
2 parents c6e6ca1 + 9501f9b commit 4dd88eb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,29 @@ public ResponseEntity<IntermediaryGetInfoResponse> getIntermediaryInfo(@PathVari
return ResponseEntity.ok(response);
}

@Operation(summary = "중개 프로필 - 후기 조회", description = "중개 프로필에서 후기를 조회합니다.",
@Operation(summary = "이동봉사자 - 중개 프로필 - 후기 조회", description = "중개 프로필에서 후기를 조회합니다.",
security = { @SecurityRequirement(name = "bearer-key") },
responses = {@ApiResponse(responseCode = "200", description = "중개 프로필 후기 조회 성공")
, @ApiResponse(responseCode = "400"
, description = "M2, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping(value = {"/volunteers/intermediaries/{intermediaryId}/reviews", "/intermediaries/{intermediaryId}/reviews"})
public ResponseEntity<List<IntermediaryGetReviewsResponse>> getIntermediaryReviews(@PathVariable Long intermediaryId,
Pageable pageable) {
List<IntermediaryGetReviewsResponse> response = intermediaryService.getIntermediaryReviews(intermediaryId, pageable);
@GetMapping("/volunteers/intermediaries/{intermediaryId}/reviews")
public ResponseEntity<List<IntermediaryGetReviewsResponse>> volunteerGetIntermediaryReviews(@PathVariable Long intermediaryId, Pageable pageable) {
List<IntermediaryGetReviewsResponse> response = intermediaryService.volunteerGetIntermediaryReviews(intermediaryId, pageable);
return ResponseEntity.ok(response);
}

@Operation(summary = "모집자 - 중개 프로필 - 후기 조회", description = "중개 프로필에서 후기를 조회합니다.",
security = { @SecurityRequirement(name = "bearer-key") },
responses = {@ApiResponse(responseCode = "200", description = "중개 프로필 후기 조회 성공")
, @ApiResponse(responseCode = "400"
, description = "M2, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/intermediaries/reviews")
public ResponseEntity<List<IntermediaryGetReviewsResponse>> intermediaryGetIntermediaryReviews(@AuthenticationPrincipal UserDetails loginUser, Pageable pageable) {
List<IntermediaryGetReviewsResponse> response = intermediaryService.intermediaryGetIntermediaryReviews(loginUser.getUsername(), pageable);
return ResponseEntity.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.time.LocalDateTime;
import java.util.List;

public record IntermediaryGetReviewsResponse(Integer profileImageNum, String dogName, String volunteerNickname,
public record IntermediaryGetReviewsResponse(Long reviewId, Integer profileImageNum, String dogName, String volunteerNickname,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
LocalDate createdDate,
String mainImage, List<String> images, String content,
Expand All @@ -21,17 +21,17 @@ public record IntermediaryGetReviewsResponse(Integer profileImageNum, String dog
) {

// 후기 이미지 리스트 필드를 제외한 생성자
public IntermediaryGetReviewsResponse(Integer profileImageNum, String dogName, String volunteerNickname, LocalDateTime createdDate,
public IntermediaryGetReviewsResponse(Long reviewId, Integer profileImageNum, String dogName, String volunteerNickname, LocalDateTime createdDate,
String mainImage, String content,
Long postId, String postMainImage, LocalDate startDate, LocalDate endDate, String departureLoc, String arrivalLoc,
Long intermediaryId, String intermediaryName) {
this(profileImageNum, dogName, volunteerNickname, createdDate.toLocalDate(), mainImage, null, content,
this(reviewId, profileImageNum, dogName, volunteerNickname, createdDate.toLocalDate(), mainImage, null, content,
postId, postMainImage, startDate, endDate, departureLoc, arrivalLoc, intermediaryId, intermediaryName);
}

// 후기 이미지 리스트 필드를 포함한 생성자
public static IntermediaryGetReviewsResponse of(IntermediaryGetReviewsResponse response, List<String> images) {
return new IntermediaryGetReviewsResponse(response.profileImageNum, response.dogName, response.volunteerNickname, response.createdDate,
return new IntermediaryGetReviewsResponse(response.reviewId, response.profileImageNum, response.dogName, response.volunteerNickname, response.createdDate,
response.mainImage, images, response.content,
response.postId, response.postMainImage, response.startDate, response.endDate, response.departureLoc, response.arrivalLoc, response.intermediaryId, response.intermediaryName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -37,7 +38,7 @@ public class IntermediaryService {
@Transactional(readOnly = true)
public List<IntermediaryGetPostsResponse> volunteerGetIntermediaryPosts(Long intermediaryId, String orderCondition, Pageable pageable) {
// 이동봉사 중개
if (!intermediaryRepository.existsById(intermediaryId)){
if (!intermediaryRepository.existsById(intermediaryId)) {
throw new BadRequestException(INTERMEDIARY_NOT_FOUND);
}
List<IntermediaryGetPostsResponse> intermediaryPosts = customPostRepository.getIntermediaryPosts(intermediaryId, orderCondition, pageable);
Expand All @@ -48,7 +49,7 @@ public List<IntermediaryGetPostsResponse> volunteerGetIntermediaryPosts(Long int
public IntermediaryGetInfoResponse getIntermediaryInfo(Long intermediaryId) {
Intermediary intermediary = intermediaryRepository.findById(intermediaryId).orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));
// 봉사 완료 건수
Long completedPostCount = customPostRepository.getCountOfCompletedPosts(intermediaryId);
Long completedPostCount = customPostRepository.getCountOfCompletedPosts(intermediaryId);

// 받은 후기 총 건수
Long reviewCount = customReviewRepository.getIntermediaryCountOfReviews(intermediaryId);
Expand All @@ -61,19 +62,49 @@ public IntermediaryGetInfoResponse getIntermediaryInfo(Long intermediaryId) {
}

@Transactional(readOnly = true)
public List<IntermediaryGetReviewsResponse> getIntermediaryReviews(Long intermediaryId, Pageable pageable) {
public List<IntermediaryGetReviewsResponse> volunteerGetIntermediaryReviews(Long intermediaryId, Pageable pageable) {
// 이동봉사 중개
if (!intermediaryRepository.existsById(intermediaryId)){
if (!intermediaryRepository.existsById(intermediaryId)) {
throw new BadRequestException(INTERMEDIARY_NOT_FOUND);
}
List<IntermediaryGetReviewsResponse> intermediaryReviews = customReviewRepository.getIntermediaryReviews(intermediaryId, pageable);
return intermediaryReviews;

List<IntermediaryGetReviewsResponse> resultReviews = new ArrayList<>();

// 후기 조회 (대표 이미지 포함)
List<IntermediaryGetReviewsResponse> reviews = customReviewRepository.getIntermediaryReviews(intermediaryId, pageable);

for (IntermediaryGetReviewsResponse intermediaryGetReviewsResponse : reviews) {
// 후기 이미지 조회 (대표 이미지 제외)
List<String> oneReviewImages = customReviewRepository.getOneReviewImages(intermediaryGetReviewsResponse.reviewId());
IntermediaryGetReviewsResponse review = IntermediaryGetReviewsResponse.of(intermediaryGetReviewsResponse, oneReviewImages);
resultReviews.add(review);
}

return resultReviews;
}

@Transactional(readOnly = true)
public List<IntermediaryGetReviewsResponse> intermediaryGetIntermediaryReviews(String email, Pageable pageable) {
Intermediary intermediary = intermediaryRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));
List<IntermediaryGetReviewsResponse> resultReviews = new ArrayList<>();

// 후기 조회 (대표 이미지 포함)
List<IntermediaryGetReviewsResponse> reviews = customReviewRepository.getIntermediaryReviews(intermediary.getId(), pageable);

for (IntermediaryGetReviewsResponse intermediaryGetReviewsResponse : reviews) {
// 후기 이미지 조회 (대표 이미지 제외)
List<String> oneReviewImages = customReviewRepository.getOneReviewImages(intermediaryGetReviewsResponse.reviewId());
IntermediaryGetReviewsResponse review = IntermediaryGetReviewsResponse.of(intermediaryGetReviewsResponse, oneReviewImages);
resultReviews.add(review);
}

return resultReviews;
}

@Transactional(readOnly = true)
public List<IntermediaryGetDogStatusesResponse> getIntermediaryDogStatuses(Long intermediaryId, Pageable pageable) {
// 이동봉사 중개
if (!intermediaryRepository.existsById(intermediaryId)){
if (!intermediaryRepository.existsById(intermediaryId)) {
throw new BadRequestException(INTERMEDIARY_NOT_FOUND);
}
List<IntermediaryGetDogStatusesResponse> intermediaryDogStatuses = customDogStatusRepository.getIntermediaryDogStatuses(intermediaryId, pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.time.LocalDateTime;
import java.util.List;

public record ReviewGetAllResponse(Integer profileImageNum, String dogName, String volunteerNickname,
public record ReviewGetAllResponse(Long reviewId, Integer profileImageNum, String dogName, String volunteerNickname,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
LocalDate createdDate,
String mainImage, List<String> images, String content,
Expand All @@ -20,17 +20,17 @@ public record ReviewGetAllResponse(Integer profileImageNum, String dogName, Stri
) {

// 후기 이미지 리스트 필드를 제외한 생성자
public ReviewGetAllResponse(Integer profileImageNum, String dogName, String volunteerNickname, LocalDateTime createdDate,
public ReviewGetAllResponse(Long reviewId, Integer profileImageNum, String dogName, String volunteerNickname, LocalDateTime createdDate,
String mainImage, String content,
Long postId, String postMainImage, LocalDate startDate, LocalDate endDate, String departureLoc, String arrivalLoc,
Long intermediaryId, String intermediaryName) {
this(profileImageNum, dogName, volunteerNickname, createdDate.toLocalDate(), mainImage, null, content,
this(reviewId, profileImageNum, dogName, volunteerNickname, createdDate.toLocalDate(), mainImage, null, content,
postId, postMainImage, startDate, endDate, departureLoc, arrivalLoc, intermediaryId, intermediaryName);
}

// 후기 이미지 리스트 필드를 포함한 생성자
public static ReviewGetAllResponse of(ReviewGetAllResponse response, List<String> images) {
return new ReviewGetAllResponse(response.profileImageNum, response.dogName, response.volunteerNickname, response.createdDate,
return new ReviewGetAllResponse(response.reviewId, response.profileImageNum, response.dogName, response.volunteerNickname, response.createdDate,
response.mainImage, images, response.content,
response.postId, response.postMainImage, response.startDate, response.endDate, response.departureLoc, response.arrivalLoc, response.intermediaryId, response.intermediaryName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.pawwithu.connectdog.domain.review.dto.response.ReviewGetOneResponse;
import com.pawwithu.connectdog.domain.review.repository.CustomReviewRepository;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -64,7 +65,7 @@ public ReviewGetOneResponse getOneReview(Long reviewId) {
public List<ReviewGetAllResponse> getAllReviews(Pageable pageable) {
List<ReviewGetAllResponse> reviews = queryFactory
.select(Projections.constructor(ReviewGetAllResponse.class,
volunteer.profileImageNum, dog.name, volunteer.nickname, review.createdDate,
review.id, volunteer.profileImageNum, dog.name, volunteer.nickname, review.createdDate,
reviewImage.image, review.content,
post.id, post.mainImage.image, post.startDate, post.endDate, post.departureLoc, post.arrivalLoc,
intermediary.id, intermediary.name))
Expand All @@ -87,7 +88,7 @@ public List<ReviewGetAllResponse> getAllReviews(Pageable pageable) {
public List<IntermediaryGetReviewsResponse> getIntermediaryReviews(Long intermediaryId, Pageable pageable) {
List<IntermediaryGetReviewsResponse> reviews = queryFactory
.select(Projections.constructor(IntermediaryGetReviewsResponse.class,
volunteer.profileImageNum, dog.name, volunteer.nickname, review.createdDate,
review.id, volunteer.profileImageNum, dog.name, volunteer.nickname, review.createdDate,
reviewImage.image, review.content,
post.id, post.mainImage.image, post.startDate, post.endDate, post.departureLoc, post.arrivalLoc,
intermediary.id, intermediary.name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

Expand Down Expand Up @@ -89,7 +90,18 @@ public ReviewGetOneResponse getOneReview(String email, Long reviewId) {

@Transactional(readOnly = true)
public List<ReviewGetAllResponse> getAllReviews(Pageable pageable) {
List<ReviewGetAllResponse> resultReviews = new ArrayList<>();

// 후기 조회 (대표 이미지 포함)
List<ReviewGetAllResponse> reviews = customReviewRepository.getAllReviews(pageable);
return reviews;

for (ReviewGetAllResponse reviewGetAllResponse : reviews) {
// 후기 이미지 조회 (대표 이미지 제외)
List<String> oneReviewImages = customReviewRepository.getOneReviewImages(reviewGetAllResponse.reviewId());
ReviewGetAllResponse review = ReviewGetAllResponse.of(reviewGetAllResponse, oneReviewImages);
resultReviews.add(review);
}

return resultReviews;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ void setUp() {
}

@Test
void 이동봉사_중개_프로필_후기_조회() throws Exception {
void 이동봉사자_중개_프로필_후기_조회() throws Exception {
// given
Long intermediaryId = 1L;
Long reviewId = 2L;
Long postId = 3L;
LocalDate createdDate = LocalDateTime.of(2023, 10, 31, 0, 0, 0).toLocalDate();
List<IntermediaryGetReviewsResponse> response = new ArrayList<>();
Expand All @@ -140,15 +141,15 @@ void setUp() {
images.add("image1");
images.add("image2");

response.add(new IntermediaryGetReviewsResponse(1, "봄이", "호짱", createdDate,
response.add(new IntermediaryGetReviewsResponse(reviewId, 1, "봄이", "호짱", createdDate,
"mainImage", images, "후기 조회 테스트입니다.", postId, "postMainImage",
startDate, endDate, "서울시 노원구", "서울시 성북구", intermediaryId, "이동봉사 중개"));
response.add(new IntermediaryGetReviewsResponse(2, "겨울이", "호짱", createdDate,
response.add(new IntermediaryGetReviewsResponse(reviewId, 2, "겨울이", "호짱", createdDate,
"mainImage", images, "후기 조회 테스트입니다.", postId, "postMainImage",
startDate, endDate, "서울시 노원구", "서울시 성북구", intermediaryId, "이동봉사 중개"));

// when
given(intermediaryService.getIntermediaryReviews(anyLong(), any())).willReturn(response);
given(intermediaryService.volunteerGetIntermediaryReviews(anyLong(), any())).willReturn(response);
ResultActions result = mockMvc.perform(
get("/volunteers/intermediaries/{intermediaryId}/reviews", intermediaryId)
.param("page", "0")
Expand All @@ -157,7 +158,42 @@ void setUp() {

// then
result.andExpect(status().isOk());
verify(intermediaryService, times(1)).getIntermediaryReviews(anyLong(), any());
verify(intermediaryService, times(1)).volunteerGetIntermediaryReviews(anyLong(), any());
}

@Test
void 모집자_중개_프로필_후기_조회() throws Exception {
// given
Long intermediaryId = 1L;
Long reviewId = 2L;
Long postId = 3L;
LocalDate createdDate = LocalDateTime.of(2023, 10, 31, 0, 0, 0).toLocalDate();
List<IntermediaryGetReviewsResponse> response = new ArrayList<>();
LocalDate startDate = LocalDate.of(2023, 10, 2);
LocalDate endDate = LocalDate.of(2023, 11, 7);

List<String> images = new ArrayList<>();
images.add("image1");
images.add("image2");

response.add(new IntermediaryGetReviewsResponse(reviewId, 1, "봄이", "호짱", createdDate,
"mainImage", images, "후기 조회 테스트입니다.", postId, "postMainImage",
startDate, endDate, "서울시 노원구", "서울시 성북구", intermediaryId, "이동봉사 중개"));
response.add(new IntermediaryGetReviewsResponse(reviewId, 2, "겨울이", "호짱", createdDate,
"mainImage", images, "후기 조회 테스트입니다.", postId, "postMainImage",
startDate, endDate, "서울시 노원구", "서울시 성북구", intermediaryId, "이동봉사 중개"));

// when
given(intermediaryService.intermediaryGetIntermediaryReviews(anyString(), any())).willReturn(response);
ResultActions result = mockMvc.perform(
get("/intermediaries/reviews", intermediaryId)
.param("page", "0")
.param("size", "2")
);

// then
result.andExpect(status().isOk());
verify(intermediaryService, times(1)).intermediaryGetIntermediaryReviews(anyString(), any());
}

@Test
Expand Down
Loading

0 comments on commit 4dd88eb

Please sign in to comment.