Skip to content

Commit

Permalink
Merge pull request #8 from 9oormthon-univ/feature/#7
Browse files Browse the repository at this point in the history
[FEAT] 내가 쓴 리뷰 조회 API 구현
  • Loading branch information
haeun1107 authored Nov 18, 2024
2 parents 522f053 + c2b4c13 commit 8994fd4
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package danpoong.soenter.domain.review.controller;

import danpoong.soenter.base.ApiResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.MyReviewsWrapperResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.GetMyReviewResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.PostReviewResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest;
import danpoong.soenter.domain.review.service.ReviewService;
Expand All @@ -9,10 +11,9 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/reviews")
Expand All @@ -30,4 +31,13 @@ public ApiResponse<PostReviewResponse> createReview(@RequestBody @Valid PostRevi
PostReviewResponse response = reviewService.createReview(postReviewRequest, authentication.getName());
return ApiResponse.onSuccess(response);
}

@GetMapping()
@Operation(summary = "내가 쓴 리뷰 조회 API", description = "사용자가 작성한 모든 리뷰를 조회합니다.")
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON400", description = "잘못된 요청입니다.")
public ApiResponse<MyReviewsWrapperResponse> getMyReviews(Authentication authentication) {
MyReviewsWrapperResponse response = reviewService.getMyReviews(authentication.getName());
return ApiResponse.onSuccess(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import danpoong.soenter.domain.enterprise.entity.Enterprise;
import danpoong.soenter.domain.enterprise.entity.Visit;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.GetMyReviewResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.PostReviewResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest;
import danpoong.soenter.domain.review.entity.Review;
import danpoong.soenter.domain.review.entity.TagList;
import danpoong.soenter.domain.user.entity.User;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

public class ReviewConverter {
public static Visit toVisit(User user, Enterprise enterprise, LocalDate visitDate) {
Expand Down Expand Up @@ -36,12 +39,28 @@ public static TagList toTagList(Review review, Integer tagNum) {
.build();
}

public static PostReviewResponse toReviewResponseDto(Review review) {
public static PostReviewResponse toReviewResponse(Review review) {
return PostReviewResponse.builder()
.reviewId(review.getReviewId())
.title(review.getTitle())
.content(review.getContent())
.createAt(review.getCreateAt())
.build();
}

public static GetMyReviewResponse toMyReviewsResponse(Review review, List<TagList> tagList) {
List<Integer> tagNumbers = tagList.stream()
.map(TagList::getTagNum)
.collect(Collectors.toList());

return GetMyReviewResponse.builder()
.reviewId(review.getReviewId())
.enterpriseName(review.getEnterprise().getName())
.enterpriseAddress(review.getEnterprise().getCity() + ", " + review.getEnterprise().getDistrict())
.content(review.getContent())
.createAt(review.getCreateAt())
.tagCount(tagList.size())
.tagNumbers(tagNumbers)
.build();
}
}
20 changes: 20 additions & 0 deletions src/main/java/danpoong/soenter/domain/review/dto/ReviewDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,25 @@ public static class PostReviewResponse {
private String content;
private LocalDate createAt;
}

@Getter
@Builder
public static class GetMyReviewResponse {
private Long reviewId;
private String enterpriseName;
private String enterpriseAddress;
private String content;
private LocalDate createAt;
private Integer tagCount;
private List<Integer> tagNumbers;
}

@Getter
@Builder
public static class MyReviewsWrapperResponse {
private String userName;
private Integer totalReviewCount;
private List<GetMyReviewResponse> reviews;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package danpoong.soenter.domain.review.repository;

import danpoong.soenter.domain.review.entity.Review;
import danpoong.soenter.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {

List<Review> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package danpoong.soenter.domain.review.repository;

import danpoong.soenter.domain.review.entity.Review;
import danpoong.soenter.domain.review.entity.TagList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TagListRepository extends JpaRepository<TagList, Long> {
List<TagList> findByReview(Review review);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package danpoong.soenter.domain.review.service;
import danpoong.soenter.domain.review.dto.ReviewDTO;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.MyReviewsWrapperResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.PostReviewResponse;

import java.util.List;

public interface ReviewService {
public ReviewDTO.ReviewResponse.PostReviewResponse createReview(PostReviewRequest postReviewRequest, String userId);
public PostReviewResponse createReview(PostReviewRequest postReviewRequest, String userId);
public MyReviewsWrapperResponse getMyReviews(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import danpoong.soenter.domain.enterprise.repository.EnterpriseRepository;
import danpoong.soenter.domain.enterprise.repository.VisitRepository;
import danpoong.soenter.domain.review.converter.ReviewConverter;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.MyReviewsWrapperResponse;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.GetMyReviewResponse;
import danpoong.soenter.domain.review.entity.Review;
import danpoong.soenter.domain.review.entity.TagList;
import danpoong.soenter.domain.review.repository.ReviewRepository;
Expand All @@ -17,6 +19,9 @@
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest;
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.PostReviewResponse;

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

@Service
@RequiredArgsConstructor
public class ReviewServiceImpl implements ReviewService {
Expand Down Expand Up @@ -53,6 +58,24 @@ public PostReviewResponse createReview(PostReviewRequest postReviewRequest, Stri
review.updateTagNum(tagCount);
reviewRepository.save(review);

return ReviewConverter.toReviewResponseDto(review);
return ReviewConverter.toReviewResponse(review);
}

@Transactional(readOnly = true)
public MyReviewsWrapperResponse getMyReviews(String userId) {
User user = userRepository.findById(Long.valueOf(userId))
.orElseThrow(() -> new RuntimeException("존재하지 않는 사용자입니다."));

List<Review> reviews = reviewRepository.findByUser(user);

List<GetMyReviewResponse> reviewResponses = reviews.stream()
.map(review -> ReviewConverter.toMyReviewsResponse(review, tagListRepository.findByReview(review)))
.collect(Collectors.toList());

return MyReviewsWrapperResponse.builder()
.userName(user.getName())
.totalReviewCount(reviews.size())
.reviews(reviewResponses)
.build();
}
}

0 comments on commit 8994fd4

Please sign in to comment.