-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feat]:Review 조회 기능 v2 API 마이그레이션 작업
- Loading branch information
Showing
10 changed files
with
324 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sopt.org.homepage.common.type; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum Part { | ||
iOS("iOS"), | ||
PLAN("PLAN"), | ||
DESIGN("DESIGN"), | ||
SERVER("SERVER"), | ||
ANDROID("ANDROID"), | ||
WEB("WEB"); | ||
|
||
private final String value; | ||
|
||
Part(String value) { | ||
this.value = value; | ||
} | ||
|
||
@JsonValue | ||
public String getValue() { | ||
return value; | ||
} | ||
public static Part fromValue(String value) { | ||
for (Part part : values()) { | ||
if (part.getValue().equalsIgnoreCase(value)) { | ||
return part; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown Part value: " + value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.value; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
src/main/java/sopt/org/homepage/project/dto/request/GetProjectsRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/sopt/org/homepage/review/ReviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package sopt.org.homepage.review; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springdoc.core.annotations.ParameterObject; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import sopt.org.homepage.common.dto.PaginateResponseDto; | ||
import sopt.org.homepage.review.dto.request.ReviewsRequestDto; | ||
import sopt.org.homepage.review.dto.response.ReviewsResponseDto; | ||
|
||
@Tag(name = "Reviews") | ||
@RestController | ||
@RequestMapping("reviews") | ||
@RequiredArgsConstructor | ||
public class ReviewController { | ||
|
||
private final ReviewService reviewService; | ||
|
||
@Operation(summary = "활동 후기 가져오기") | ||
@GetMapping | ||
public ResponseEntity<PaginateResponseDto<ReviewsResponseDto>> getReviews( | ||
@ParameterObject @ModelAttribute ReviewsRequestDto reviewsRequestDto | ||
) { | ||
return ResponseEntity.ok(reviewService.getReviews(reviewsRequestDto)); | ||
} | ||
|
||
@GetMapping("/random") | ||
@Operation(summary = "랜덤 활동 후기 파트별로 하나씩 가져오기", | ||
description = "만약 특정 파트에 리뷰가 없다면 그 파트의 데이터는 나오지 않습니다.") | ||
public ResponseEntity<List<ReviewsResponseDto>> getRandomReviewByPart() { | ||
return ResponseEntity.ok(reviewService.getRandomReviewByPart()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package sopt.org.homepage.review; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import sopt.org.homepage.common.dto.PaginateResponseDto; | ||
import sopt.org.homepage.common.mapper.ResponseMapper; | ||
import sopt.org.homepage.common.type.Part; | ||
import sopt.org.homepage.review.dto.request.ReviewsRequestDto; | ||
import sopt.org.homepage.review.dto.response.ReviewsResponseDto; | ||
import sopt.org.homepage.review.repository.ReviewQueryRepository; | ||
import sopt.org.homepage.review.repository.ReviewRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class ReviewService { | ||
|
||
private final ReviewQueryRepository reviewQueryRepository; | ||
private final ReviewRepository reviewRepository; | ||
|
||
private final ResponseMapper responseMapper; | ||
|
||
public PaginateResponseDto<ReviewsResponseDto> getReviews(ReviewsRequestDto requestDto) { | ||
long totalCount = reviewQueryRepository.countWithFilters(requestDto); | ||
|
||
var reviews = reviewQueryRepository.findAllWithFilters( | ||
requestDto, | ||
requestDto.getOffset(), | ||
requestDto.getLimit() | ||
); | ||
|
||
var reviewDtos = reviews.stream() | ||
.map(responseMapper::toReviewResponseDto) | ||
.toList(); | ||
|
||
return new PaginateResponseDto<>( | ||
reviewDtos, | ||
(int) totalCount, | ||
requestDto.getLimit(), | ||
requestDto.getPageNo() | ||
); | ||
} | ||
|
||
public List<ReviewsResponseDto> getRandomReviewByPart() { | ||
return Arrays.stream(Part.values()) | ||
.map(reviewQueryRepository::findRandomReviewByPart) | ||
.filter(Objects::nonNull) | ||
.map(responseMapper::toReviewResponseDto) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/sopt/org/homepage/review/dto/request/ReviewsRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sopt.org.homepage.review.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Getter; | ||
import org.springframework.validation.annotation.Validated; | ||
import sopt.org.homepage.common.dto.PaginateRequest; | ||
import sopt.org.homepage.common.type.Part; | ||
|
||
@Validated | ||
@Getter | ||
@Schema(description = "리뷰 조회 요청") | ||
public class ReviewsRequestDto extends PaginateRequest { | ||
@Parameter(description = "Part, 전체를 불러올땐 아무값도 안넣으면 됩니다.") | ||
private final Part part; | ||
|
||
@Parameter(description = "활동기수로 필터링 합니다, 값을 넣지 않을 경우 전체 조회합니다.") | ||
private final Integer generation; | ||
|
||
public ReviewsRequestDto(Integer pageNo, Integer limit, Part part, Integer generation) { | ||
super(pageNo, limit); | ||
this.part = part; | ||
this.generation = generation; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/sopt/org/homepage/review/dto/response/ReviewsResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package sopt.org.homepage.review.dto.response; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import sopt.org.homepage.common.type.Part; | ||
|
||
@Schema(description = "리뷰 응답") | ||
@Getter | ||
public class ReviewsResponseDto { | ||
|
||
@Schema(description = "리뷰 ID(item의 기본 키)") | ||
private final Long id; | ||
|
||
@Schema(description = "활동 리뷰 타이틀") | ||
private final String title; | ||
|
||
@Schema(description = "작성자") | ||
private final String author; | ||
|
||
@Schema(description = "작성자 프로필 이미지") | ||
private final String authorProfileImageUrl; | ||
|
||
@Schema(description = "활동 기수") | ||
private final Integer generation; | ||
|
||
@Schema(description = "활동후기 설명") | ||
private final String description; | ||
|
||
@Schema(description = "파트(활동 기수)") | ||
private final Part part; | ||
|
||
@Schema(description = "주제") | ||
private final String subject; | ||
|
||
@Schema(description = "썸네일 URL(활동후기 타이틀)") | ||
private final String thumbnailUrl; | ||
|
||
@Schema(description = "후기 작성 플랫폼") | ||
private final String platform; | ||
|
||
@Schema(description = "Redirect Link") | ||
private final String url; | ||
|
||
@Builder | ||
private ReviewsResponseDto(Long id, String title, String author, String authorProfileImageUrl, | ||
Integer generation, String description, Part part, String subject, | ||
String thumbnailUrl, String platform, String url) { | ||
this.id = id; | ||
this.title = title; | ||
this.author = author; | ||
this.authorProfileImageUrl = authorProfileImageUrl; | ||
this.generation = generation; | ||
this.description = description; | ||
this.part = part; | ||
this.subject = subject; | ||
this.thumbnailUrl = thumbnailUrl; | ||
this.platform = platform; | ||
this.url = url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/sopt/org/homepage/review/repository/ReviewQueryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package sopt.org.homepage.review.repository; | ||
|
||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
|
||
import sopt.org.homepage.common.type.Part; | ||
import sopt.org.homepage.review.entity.QReviewEntity; | ||
import sopt.org.homepage.review.entity.ReviewEntity; | ||
import sopt.org.homepage.review.dto.request.ReviewsRequestDto; | ||
|
||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ReviewQueryRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
private final QReviewEntity review = QReviewEntity.reviewEntity; | ||
|
||
public List<ReviewEntity> findAllWithFilters(ReviewsRequestDto requestDto, long offset, int limit) { | ||
var query = queryFactory | ||
.selectFrom(review); | ||
|
||
if (requestDto.getPart() != null) { | ||
query.where(review.part.eq(requestDto.getPart())); | ||
} | ||
|
||
if (requestDto.getGeneration() != null) { | ||
query.where(review.generation.eq(requestDto.getGeneration())); | ||
} | ||
|
||
return query | ||
.orderBy(review.generation.desc()) | ||
.orderBy(review.author.asc()) | ||
.orderBy(review.id.asc()) | ||
.offset(offset) | ||
.limit(limit) | ||
.fetch(); | ||
} | ||
|
||
public long countWithFilters(ReviewsRequestDto requestDto) { | ||
var query = queryFactory | ||
.select(review.count()) | ||
.from(review); | ||
|
||
if (requestDto.getPart() != null) { | ||
query.where(review.part.eq(requestDto.getPart())); | ||
} | ||
|
||
if (requestDto.getGeneration() != null) { | ||
query.where(review.generation.eq(requestDto.getGeneration())); | ||
} | ||
|
||
return query.fetchOne(); | ||
} | ||
|
||
public ReviewEntity findRandomReviewByPart(Part part) { | ||
return queryFactory | ||
.selectFrom(review) | ||
.where(review.part.eq(part)) | ||
.orderBy(Expressions.numberTemplate(Double.class, "RANDOM()").asc()) | ||
.limit(1) | ||
.fetchFirst(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/sopt/org/homepage/review/repository/ReviewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package sopt.org.homepage.review.repository; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import sopt.org.homepage.review.entity.ReviewEntity; | ||
|
||
public interface ReviewRepository extends JpaRepository<ReviewEntity, Long> { | ||
} |