From 6b49cb705ab345f462debe1c0b5913cbcfac7e4c Mon Sep 17 00:00:00 2001 From: chaeyoungeee Date: Fri, 26 Jul 2024 18:20:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=A0=84=EC=B2=B4=20=ED=9B=84=EA=B8=B0?= =?UTF-8?q?=20=EA=B0=84=EB=8B=A8=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 페이징 및 검색을 포함한 전체 후기 간단 조회 기능 추가함 --- .../api/review/ReviewController.java | 26 +++++++++++ .../MZConnent/config/SecurityConfig.java | 1 + .../MZConnent/domain/review/Review.java | 6 +-- .../member/response/MemberInfoResponse.java | 7 ++- .../paging/response/PageContentResponse.java | 25 ++++++++++ .../MZConnent/dto/review/ReviewDto.java | 33 ------------- .../response/ReviewsSimpleResponse.java | 33 +++++++++++++ .../repository/review/ReviewRepository.java | 8 ++++ .../service/review/ReviewService.java | 46 +++++++++++++++++++ 9 files changed, 145 insertions(+), 40 deletions(-) create mode 100644 src/main/java/likelion/MZConnent/api/review/ReviewController.java create mode 100644 src/main/java/likelion/MZConnent/dto/paging/response/PageContentResponse.java delete mode 100644 src/main/java/likelion/MZConnent/dto/review/ReviewDto.java create mode 100644 src/main/java/likelion/MZConnent/dto/review/response/ReviewsSimpleResponse.java create mode 100644 src/main/java/likelion/MZConnent/service/review/ReviewService.java diff --git a/src/main/java/likelion/MZConnent/api/review/ReviewController.java b/src/main/java/likelion/MZConnent/api/review/ReviewController.java new file mode 100644 index 0000000..19b3930 --- /dev/null +++ b/src/main/java/likelion/MZConnent/api/review/ReviewController.java @@ -0,0 +1,26 @@ +package likelion.MZConnent.api.review; + +import likelion.MZConnent.dto.paging.response.PageContentResponse; +import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse; +import likelion.MZConnent.service.review.ReviewService; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@Slf4j +public class ReviewController { + private final ReviewService reviewService; + @GetMapping("/api/reviews") + ResponseEntity getReviewSimpleList(@RequestParam(required = false, defaultValue = "", value = "keyword") String keyword, @RequestParam(required = false, defaultValue = "0", value = "page") int page ) { + + PageContentResponse response = reviewService.getReviewsSimpleList(keyword, page); + return ResponseEntity.ok(response); + } +} diff --git a/src/main/java/likelion/MZConnent/config/SecurityConfig.java b/src/main/java/likelion/MZConnent/config/SecurityConfig.java index 8ea181d..819b894 100644 --- a/src/main/java/likelion/MZConnent/config/SecurityConfig.java +++ b/src/main/java/likelion/MZConnent/config/SecurityConfig.java @@ -30,6 +30,7 @@ public class SecurityConfig { "/api/auth/login", "/api/auth/logout", // 회원 "/api/categories/culture", "/api/cultures", // 문화 "/api/clubs", "/api/clubs/*/join", // 동아리 + "/api/reviews", // 후기 "/api/clubs/*", "/swagger", "/swagger-ui.html", "/swagger-ui/**", "/api-docs", "/api-docs/**", "/v3/api-docs/**", //swagger }; diff --git a/src/main/java/likelion/MZConnent/domain/review/Review.java b/src/main/java/likelion/MZConnent/domain/review/Review.java index 6cd3396..af71b79 100644 --- a/src/main/java/likelion/MZConnent/domain/review/Review.java +++ b/src/main/java/likelion/MZConnent/domain/review/Review.java @@ -37,7 +37,7 @@ public class Review { private String reviewImageUrl4; @Column(nullable = false) - private LocalDateTime createDate; + private LocalDateTime createdDate; @Column(nullable = false) private int likeCount; @@ -60,14 +60,14 @@ public class Review { private List reviewLikes; @Builder - public Review(String title, String content, String reviewImageUrl1, String reviewImageUrl2, String reviewImageUrl3, String reviewImageUrl4, LocalDateTime createDate, int likeCount, int commentCount, Culture culture, Member member) { + public Review(String title, String content, String reviewImageUrl1, String reviewImageUrl2, String reviewImageUrl3, String reviewImageUrl4, LocalDateTime createdDate, int likeCount, int commentCount, Culture culture, Member member) { this.title = title; this.content = content; this.reviewImageUrl1 = reviewImageUrl1 != null ? reviewImageUrl1 : ""; // 나중에 기본 이미지 URL 추가 this.reviewImageUrl2 = reviewImageUrl2 != null ? reviewImageUrl2 : ""; this.reviewImageUrl3 = reviewImageUrl3 != null ? reviewImageUrl3 : ""; this.reviewImageUrl4 = reviewImageUrl4 != null ? reviewImageUrl4 : ""; - this.createDate = createDate; + this.createdDate = createdDate; this.likeCount = likeCount; this.commentCount = commentCount; this.culture = culture; diff --git a/src/main/java/likelion/MZConnent/dto/member/response/MemberInfoResponse.java b/src/main/java/likelion/MZConnent/dto/member/response/MemberInfoResponse.java index 3c79357..969c66e 100644 --- a/src/main/java/likelion/MZConnent/dto/member/response/MemberInfoResponse.java +++ b/src/main/java/likelion/MZConnent/dto/member/response/MemberInfoResponse.java @@ -3,9 +3,8 @@ import likelion.MZConnent.domain.member.Age; import likelion.MZConnent.domain.member.Gender; import likelion.MZConnent.domain.member.Member; -import likelion.MZConnent.domain.member.Role; import likelion.MZConnent.dto.culture.CultureCategoryDto; -import likelion.MZConnent.dto.review.ReviewDto; +import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse; import lombok.*; import java.math.BigDecimal; @@ -27,7 +26,7 @@ public class MemberInfoResponse { private String profileImageUrl; private BigDecimal averageMannersScore; private List selfIntroductions = new ArrayList<>(); - private List reviews = new ArrayList<>(); + private List reviews = new ArrayList<>(); public MemberInfoResponse(Member member) { @@ -44,7 +43,7 @@ public MemberInfoResponse(Member member) { .map((selfIntroduction)-> new CultureCategoryDto(selfIntroduction.getCultureCategory())) .collect(Collectors.toList()); this.reviews = member.getReviews().stream() - .map(ReviewDto::new) + .map(ReviewsSimpleResponse::new) .collect(Collectors.toList()); } } diff --git a/src/main/java/likelion/MZConnent/dto/paging/response/PageContentResponse.java b/src/main/java/likelion/MZConnent/dto/paging/response/PageContentResponse.java new file mode 100644 index 0000000..bbbe1f7 --- /dev/null +++ b/src/main/java/likelion/MZConnent/dto/paging/response/PageContentResponse.java @@ -0,0 +1,25 @@ +package likelion.MZConnent.dto.paging.response; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.data.domain.Page; + +import java.util.List; + +@Getter +@NoArgsConstructor +public class PageContentResponse { + private List content; + private int totalPages; + private long totalElements; + private int size; + + @Builder + public PageContentResponse(List content, int totalPages, long totalElements, int size) { + this.content = content; + this.totalPages = totalPages; + this.totalElements = totalElements; + this.size = size; + } +} diff --git a/src/main/java/likelion/MZConnent/dto/review/ReviewDto.java b/src/main/java/likelion/MZConnent/dto/review/ReviewDto.java deleted file mode 100644 index f38fa9c..0000000 --- a/src/main/java/likelion/MZConnent/dto/review/ReviewDto.java +++ /dev/null @@ -1,33 +0,0 @@ -package likelion.MZConnent.dto.review; - -import likelion.MZConnent.domain.review.Review; -import likelion.MZConnent.dto.member.MemberProfileDto; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import java.time.LocalDateTime; - -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class ReviewDto { - private Long reviewId; - private MemberProfileDto reviewer; - private String title; - private String reviewImageUrl; - private String cultureName; - private LocalDateTime createDate; - private int likeCount; - private int commentCount; - - public ReviewDto(Review review) { - reviewId = review.getReviewId(); - reviewer = new MemberProfileDto(review.getMember()); - title = review.getTitle(); - reviewImageUrl = review.getReviewImageUrl1(); - cultureName = review.getCulture().getName(); - createDate = review.getCreateDate(); - likeCount = review.getLikeCount(); - commentCount = review.getCommentCount(); - } -} diff --git a/src/main/java/likelion/MZConnent/dto/review/response/ReviewsSimpleResponse.java b/src/main/java/likelion/MZConnent/dto/review/response/ReviewsSimpleResponse.java new file mode 100644 index 0000000..d738635 --- /dev/null +++ b/src/main/java/likelion/MZConnent/dto/review/response/ReviewsSimpleResponse.java @@ -0,0 +1,33 @@ +package likelion.MZConnent.dto.review.response; + + +import likelion.MZConnent.domain.review.Review; +import likelion.MZConnent.dto.member.MemberProfileDto; +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@NoArgsConstructor +public class ReviewsSimpleResponse { + private Long reviewId; + private MemberProfileDto reviewer; + private String title; + private String reviewImageUrl; + private String cultureName; + private LocalDateTime createdDate; + private int likeCount; + private int commentCount; + + @Builder + public ReviewsSimpleResponse(Review review) { + this.reviewId = review.getReviewId(); + this.reviewer = new MemberProfileDto(review.getMember()); + this.title = review.getTitle(); + this.reviewImageUrl = review.getReviewImageUrl1(); + this.cultureName = review.getCulture().getName(); + this.createdDate = review.getCreatedDate(); + this.likeCount = review.getLikeCount(); + this.commentCount = review.getCommentCount(); + } +} \ No newline at end of file diff --git a/src/main/java/likelion/MZConnent/repository/review/ReviewRepository.java b/src/main/java/likelion/MZConnent/repository/review/ReviewRepository.java index ee8761c..48d3799 100644 --- a/src/main/java/likelion/MZConnent/repository/review/ReviewRepository.java +++ b/src/main/java/likelion/MZConnent/repository/review/ReviewRepository.java @@ -1,9 +1,17 @@ package likelion.MZConnent.repository.review; +import io.lettuce.core.dynamic.annotation.Param; import likelion.MZConnent.domain.review.Review; +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.stereotype.Repository; +import java.util.List; + @Repository public interface ReviewRepository extends JpaRepository { + @Query("SELECT r FROM Review r WHERE r.title LIKE %:keyword%") + Page findByKeyword(@Param("keyword") String keyword, Pageable pageable); } diff --git a/src/main/java/likelion/MZConnent/service/review/ReviewService.java b/src/main/java/likelion/MZConnent/service/review/ReviewService.java new file mode 100644 index 0000000..1aafd07 --- /dev/null +++ b/src/main/java/likelion/MZConnent/service/review/ReviewService.java @@ -0,0 +1,46 @@ +package likelion.MZConnent.service.review; + +import likelion.MZConnent.domain.review.Review; +import likelion.MZConnent.dto.paging.response.PageContentResponse; +import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse; +import likelion.MZConnent.repository.review.ReviewRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Service +@Slf4j +@RequiredArgsConstructor +public class ReviewService { + private final ReviewRepository reviewRepository; + private final int PAGE_SIZE = 6; + + public PageContentResponse getReviewsSimpleList(String keyword, int page) { + Pageable pageable = PageRequest.of(page, PAGE_SIZE, Sort.by("createdDate").descending()); + + log.info(keyword); + + Page reviews = reviewRepository.findByKeyword(keyword, pageable); + + List reviewsResponse = reviews.stream().map(review -> ReviewsSimpleResponse.builder() + .review(review).build() + ).collect(Collectors.toList()); + + return PageContentResponse.builder() + .content(reviewsResponse) + .totalPages(reviews.getTotalPages()) + .totalElements(reviews.getTotalElements()) + .size(pageable.getPageSize()) + .build(); + + } +}