Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: 사용자 마이페이지 리뷰 조회 기능 구현 #433

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/main/java/com/funeat/common/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public WebConfig(final CustomPageableHandlerMethodArgumentResolver customPageabl
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(authHandlerInterceptor)
.addPathPatterns("/api/products/**/reviews/**")
.addPathPatterns("/api/members");
.addPathPatterns("/api/members/**");
registry.addInterceptor(recipeHandlerInterceptor)
.addPathPatterns("/api/recipes");
}
Expand Down
58 changes: 58 additions & 0 deletions backend/src/main/java/com/funeat/member/dto/MemberReviewDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.funeat.member.dto;

import com.funeat.review.domain.Review;

public class MemberReviewDto {

private final Long reviewId;
private final Long productId;
private final String productName;
private final String content;
private final Long rating;
private final Long favoriteCount;

private MemberReviewDto(final Long reviewId, final Long productId, final String productName, final String content,
final Long rating, final Long favoriteCount) {
this.reviewId = reviewId;
this.productId = productId;
this.productName = productName;
this.content = content;
this.rating = rating;
this.favoriteCount = favoriteCount;
}

public static MemberReviewDto toDto(final Review review) {
return new MemberReviewDto(
review.getId(),
review.getProduct().getId(),
review.getProduct().getName(),
review.getContent(),
review.getRating(),
review.getFavoriteCount()
);
}

public Long getReviewId() {
return reviewId;
}

public Long getProductId() {
return productId;
}

public String getProductName() {
return productName;
}

public String getContent() {
return content;
}

public Long getRating() {
return rating;
}

public Long getFavoriteCount() {
return favoriteCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.funeat.member.dto;

import com.funeat.review.presentation.dto.SortingReviewsPageDto;
import java.util.List;

public class MemberReviewsResponse {

private final SortingReviewsPageDto page;
private final List<MemberReviewDto> reviews;

private MemberReviewsResponse(final SortingReviewsPageDto page, final List<MemberReviewDto> reviews) {
this.page = page;
this.reviews = reviews;
}

public static MemberReviewsResponse toResponse(final SortingReviewsPageDto page,
final List<MemberReviewDto> reviews) {
return new MemberReviewsResponse(page, reviews);
}

public SortingReviewsPageDto getPage() {
return page;
}

public List<MemberReviewDto> getReviews() {
return reviews;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@
import com.funeat.member.application.MemberService;
import com.funeat.member.dto.MemberProfileResponse;
import com.funeat.member.dto.MemberRequest;
import com.funeat.member.dto.MemberReviewsResponse;
import com.funeat.review.application.ReviewService;
import javax.validation.Valid;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/members")
public class MemberApiController implements MemberController {

private final MemberService memberService;
private final ReviewService reviewService;

public MemberApiController(final MemberService memberService) {
public MemberApiController(final MemberService memberService, final ReviewService reviewService) {
this.memberService = memberService;
this.reviewService = reviewService;
}

@GetMapping("/api/members")
@GetMapping
public ResponseEntity<MemberProfileResponse> getMemberProfile(
@AuthenticationPrincipal final LoginInfo loginInfo) {
final Long memberId = loginInfo.getId();
Expand All @@ -31,7 +38,7 @@ public ResponseEntity<MemberProfileResponse> getMemberProfile(
return ResponseEntity.ok(response);
}

@PutMapping("/api/members")
@PutMapping
public ResponseEntity<Void> putMemberProfile(@AuthenticationPrincipal final LoginInfo loginInfo,
@RequestBody @Valid final MemberRequest request) {
final Long memberId = loginInfo.getId();
Expand All @@ -40,4 +47,11 @@ public ResponseEntity<Void> putMemberProfile(@AuthenticationPrincipal final Logi

return ResponseEntity.ok().build();
}

@GetMapping("/reviews")
public ResponseEntity<MemberReviewsResponse> getMemberReview(final LoginInfo loginInfo, final Pageable pageable) {
wugawuga marked this conversation as resolved.
Show resolved Hide resolved
final MemberReviewsResponse response = reviewService.findReviewByMember(loginInfo.getId(), pageable);

return ResponseEntity.ok().body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.funeat.auth.util.AuthenticationPrincipal;
import com.funeat.member.dto.MemberProfileResponse;
import com.funeat.member.dto.MemberRequest;
import com.funeat.member.dto.MemberReviewsResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -31,4 +34,13 @@ public interface MemberController {
@PutMapping
ResponseEntity<Void> putMemberProfile(@AuthenticationPrincipal LoginInfo loginInfo,
@RequestBody MemberRequest request);

@Operation(summary = "사용자 리뷰 조회", description = "사용자가 작성한 리뷰를 조회한다.")
@ApiResponse(
responseCode = "200",
description = "사용자 리뷰 조회 성공."
)
@GetMapping
ResponseEntity<MemberReviewsResponse> getMemberReview(@AuthenticationPrincipal LoginInfo loginInfo,
@PageableDefault Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.funeat.common.ImageService;
import com.funeat.member.domain.Member;
import com.funeat.member.domain.favorite.ReviewFavorite;
import com.funeat.member.dto.MemberReviewDto;
import com.funeat.member.dto.MemberReviewsResponse;
import com.funeat.member.exception.MemberException.MemberNotFoundException;
import com.funeat.member.persistence.MemberRepository;
import com.funeat.member.persistence.ReviewFavoriteRepository;
Expand Down Expand Up @@ -139,4 +141,18 @@ public RankingReviewsResponse getTopReviews() {

return RankingReviewsResponse.toResponse(dtos);
}

public MemberReviewsResponse findReviewByMember(final Long memberId, final Pageable pageable) {
final Member findMember = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException(MEMBER_NOT_FOUND, memberId));

final Page<Review> sortedReviewPages = reviewRepository.findReviewsByMember(findMember, pageable);
final SortingReviewsPageDto pageDto = SortingReviewsPageDto.toDto(sortedReviewPages);

final List<MemberReviewDto> dtos = sortedReviewPages.stream()
.map(MemberReviewDto::toDto)
.collect(Collectors.toList());

return MemberReviewsResponse.toResponse(pageDto, dtos);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.funeat.review.persistence;

import com.funeat.member.domain.Member;
import com.funeat.product.domain.Product;
import com.funeat.review.domain.Review;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ReviewRepository extends JpaRepository<Review, Long> {

Page<Review> findReviewsByProduct(final Pageable pageable, final Product product);

List<Review> findTop3ByOrderByFavoriteCountDesc();

Long countByProduct(final Product product);

Page<Review> findReviewsByMember(final Member findMember, final Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ public class RankingReviewDto {
private final Long rating;
private final Long favoriteCount;

public RankingReviewDto(final Long reviewId,
final Long productId,
final String productName,
final String content,
final Long rating,
final Long favoriteCount) {
private RankingReviewDto(final Long reviewId, final Long productId, final String productName, final String content,
final Long rating, final Long favoriteCount) {
this.reviewId = reviewId;
this.productId = productId;
this.productName = productName;
Expand Down
Loading
Loading