Skip to content

Commit

Permalink
[BSVR]-119 내 시야기록 조회 API 구현 (#36)
Browse files Browse the repository at this point in the history
* refactor: 파일명과 줄바꿈 포맷 변경

* feat: 자신 필터링된 리뷰 조회 서비스 메서드 작성

* feat: 자신의 리뷰 조회 컨트롤러와 request dto 구현

* test: fake repository 메서드 구현

* test: 유저별 리뷰 조회 테스트 메서드 구현

* refactor: 디버깅용 코드 삭제
  • Loading branch information
pminsung12 authored Jul 17, 2024
1 parent c75652c commit d92b939
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.depromeet.spot.application.review;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;

import org.depromeet.spot.application.review.dto.request.BlockReviewRequest;
import org.depromeet.spot.application.review.dto.request.MyReviewRequest;
import org.depromeet.spot.application.review.dto.response.ReviewListResponse;
import org.depromeet.spot.domain.review.ReviewListResult;
import org.depromeet.spot.usecase.port.in.review.ReviewReadUsecase;
Expand Down Expand Up @@ -38,21 +38,31 @@ public ReviewListResponse findReviewsByBlockId(
@Positive
@Parameter(description = "블록 PK", required = true)
Long blockId,
@RequestParam(required = false) @Parameter(description = "열 ID (필터링)") Long rowId,
@RequestParam(required = false) @Parameter(description = "좌석 번호 (필터링)") Long seatNumber,
@RequestParam(defaultValue = "0")
@PositiveOrZero
@Parameter(description = "시작 위치 (기본값: 0)")
int offset,
@RequestParam(defaultValue = "10")
@Positive
@Max(50)
@Parameter(description = "조회할 리뷰 수 (기본값: 10, 최대: 50)")
int limit) {

@ModelAttribute BlockReviewRequest request) {
ReviewListResult result =
reviewReadUsecase.findReviewsByBlockId(
stadiumId, blockId, rowId, seatNumber, offset, limit);
stadiumId,
blockId,
request.rowId(),
request.seatNumber(),
request.offset(),
request.limit());
return ReviewListResponse.from(result);
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/reviews")
@Operation(
summary = " 자신이 작성한 리뷰 목록을 조회한다.",
description = "연도와 월로 필터링할 수 있다. 필터링 없이 전체를 조회하려면 year와 month를 null로 입력한다.")
public ReviewListResponse findMyReviews(@ModelAttribute MyReviewRequest request) {
ReviewListResult result =
reviewReadUsecase.findMyReviews(
request.userId(),
request.offset(),
request.limit(),
request.year(),
request.month());
return ReviewListResponse.from(result);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.depromeet.spot.application.review.dto.request;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;

import io.swagger.v3.oas.annotations.Parameter;

public record ReviewBlockRequest(
public record BlockReviewRequest(
@Parameter(description = "열 ID (필터링)") Long rowId,
@Parameter(description = "좌석 번호 (필터링)") Long seatNumber,
@PositiveOrZero @Parameter(description = "시작 위치 (기본값: 0)") int offset,
@Max(50) @Parameter(description = "조회할 리뷰 수 (기본값: 10, 최대: 50)") int limit) {}
@Positive @Max(50) @Parameter(description = "조회할 리뷰 수 (기본값: 10, 최대: 50)") int limit) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.depromeet.spot.application.review.dto.request;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.PositiveOrZero;

import io.swagger.v3.oas.annotations.Parameter;

public record MyReviewRequest(
@Parameter(description = "유저 ID") Long userId,
@PositiveOrZero @Parameter(description = "시작 위치 (기본값: 0)") Integer offset,
@Min(1) @Max(50) @Parameter(description = "조회할 리뷰 수 (기본값: 10, 최대: 50)") Integer limit,
@Min(1000) @Max(9999) @Parameter(description = "년도 (4자리 숫자)") Integer year,
@Min(1) @Max(12) @Parameter(description = "월 (1-12)") Integer month) {}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ public List<ReviewEntity> findByBlockIdWithFilters(
.fetch();
}

public List<ReviewEntity> findByUserIdWithFilters(
Long userId, int offset, int limit, Integer year, Integer month) {
return queryFactory
.selectFrom(reviewEntity)
.where(
reviewEntity
.userId
.eq(userId)
.and(year != null ? reviewEntity.createdAt.year().eq(year) : null)
.and(
month != null
? reviewEntity.createdAt.month().eq(month)
: null)
.and(reviewEntity.deletedAt.isNull()))
.orderBy(reviewEntity.createdAt.desc())
.offset(offset)
.limit(limit)
.fetch();
}

public long countByBlockIdWithFilters(
Long stadiumId, Long blockId, Long rowId, Long seatNumber) {
return queryFactory
Expand All @@ -60,7 +80,25 @@ public long countByBlockIdWithFilters(
? reviewEntity.seatNumber.eq(seatNumber)
: null)
.and(reviewEntity.deletedAt.isNull()))
.fetchCount();
.stream()
.count();
}

public long countByUserIdWithFilters(Long userId, Integer year, Integer month) {
return queryFactory
.selectFrom(reviewEntity)
.where(
reviewEntity
.userId
.eq(userId)
.and(year != null ? reviewEntity.createdAt.year().eq(year) : null)
.and(
month != null
? reviewEntity.createdAt.month().eq(month)
: null)
.and(reviewEntity.deletedAt.isNull()))
.stream()
.count();
}

public List<KeywordCount> findTopKeywordsByBlockId(Long stadiumId, Long blockId, int limit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,25 @@ public List<Review> findByBlockId(
return reviews.stream().map(this::fetchReviewDetails).collect(Collectors.toList());
}

@Override
public List<Review> findByUserId(
Long userId, int offset, int limit, Integer year, Integer month) {
List<ReviewEntity> reviews =
reviewCustomRepository.findByUserIdWithFilters(userId, offset, limit, year, month);
return reviews.stream().map(this::fetchReviewDetails).collect(Collectors.toList());
}

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

@Override
public Long countByUserId(Long userId, Integer year, Integer month) {
return reviewCustomRepository.countByUserIdWithFilters(userId, year, month);
}

@Override
public List<KeywordCount> findTopKeywordsByBlockId(Long stadiumId, Long blockId, int limit) {
return reviewCustomRepository.findTopKeywordsByBlockId(stadiumId, blockId, limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface ReviewReadUsecase {
ReviewListResult findReviewsByBlockId(
Long stadiumId, Long blockId, Long rowId, Long seatNumber, int offset, int limit);

ReviewListResult findMyReviews(Long userId, int offset, int limit, Integer year, Integer month);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public interface ReviewRepository {
List<Review> findByBlockId(
Long stadiumId, Long blockId, Long rowId, Long seatNumber, int offset, int limit);

List<Review> findByUserId(Long userId, int offset, int limit, Integer year, Integer month);

Long countByBlockId(Long stadiumId, Long blockId, Long rowId, Long seatNumber);

Long countByUserId(Long userId, Integer year, Integer month);

List<KeywordCount> findTopKeywordsByBlockId(Long stadiumId, Long blockId, int limit);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.depromeet.spot.usecase.service.review;

import java.util.Collections;
import java.util.List;

import org.depromeet.spot.domain.review.KeywordCount;
Expand Down Expand Up @@ -30,4 +31,14 @@ public ReviewListResult findReviewsByBlockId(

return new ReviewListResult(reviews, topKeywords, totalCount, offset, limit);
}

@Override
public ReviewListResult findMyReviews(
Long userId, int offset, int limit, Integer year, Integer month) {
List<Review> reviews = reviewRepository.findByUserId(userId, offset, limit, year, month);
Long totalCount = reviewRepository.countByUserId(userId, year, month);
List<KeywordCount> topKeywords = Collections.emptyList(); // 사용자 리뷰에 대한 탑 키워드 모음 필요 없음

return new ReviewListResult(reviews, topKeywords, totalCount, offset, limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ public List<KeywordCount> findTopKeywordsByBlockId(Long stadiumId, Long blockId,
.collect(Collectors.toList());
}

@Override
public List<Review> findByUserId(
Long userId, int offset, int limit, Integer year, Integer month) {
return data.stream()
.filter(review -> review.getUserId().equals(userId))
.filter(review -> year == null || review.getCreatedAt().getYear() == year)
.filter(review -> month == null || review.getCreatedAt().getMonthValue() == month)
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
}

@Override
public Long countByUserId(Long userId, Integer year, Integer month) {
return data.stream()
.filter(review -> review.getUserId().equals(userId))
.filter(review -> year == null || review.getCreatedAt().getYear() == year)
.filter(review -> month == null || review.getCreatedAt().getMonthValue() == month)
.count();
}

public void save(Review review) {
data.add(review);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,35 @@ void setUp() {
assertEquals(10, result.reviews().size());
assertEquals(20, result.totalCount());
}

@Test
void findMyReviews_유저별_리뷰를_정상적으로_반환한다() {
// Given
Review review1 =
Review.builder()
.id(1L)
.userId(1L)
.content("Great game!")
.keywords(Collections.singletonList(createReviewKeyword(1L, 1L, 1L, true)))
.build();
Review review2 =
Review.builder()
.id(2L)
.userId(1L)
.content("Amazing view!")
.keywords(
Collections.singletonList(
createReviewKeyword(2L, 2L, 2L, true))) // 다른 keywordId 사용
.build();
fakeReviewRepository.save(review1);
fakeReviewRepository.save(review2);

// When
ReviewListResult result = reviewReadService.findMyReviews(1L, 0, 10, null, null);

// Then
assertEquals(2, result.reviews().size());
assertEquals(2, result.totalCount());
assertEquals(0, result.topKeywords().size());
}
}

0 comments on commit d92b939

Please sign in to comment.