Skip to content

Commit

Permalink
Merge pull request TAVE-balak#73 from songhee1/discussion
Browse files Browse the repository at this point in the history
Bug : @PathParam으로 잘못 사용된 어노테이션 변경(TAVE-balak#72)
  • Loading branch information
songhee1 authored Jan 16, 2024
2 parents 32bc29e + eb4496b commit 6b66a7e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.swagger.annotations.ApiResponse;
import java.io.IOException;
import javax.validation.Valid;
import javax.websocket.server.PathParam;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand All @@ -19,6 +18,7 @@
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tavebalak.OTTify.common.BaseResponse;
import tavebalak.OTTify.community.dto.request.CommunitySubjectCreateDTO;
Expand Down Expand Up @@ -75,7 +75,7 @@ public BaseResponse<String> deleteSubject(@PathVariable Long subjectId)
@ApiImplicitParam(name = "subjectId", dataType = "long", value = "토론 주제 id", required = true, paramType = "path")
@ApiResponse(code = 200, message = "성공적으로 토론 게시글 공감 해제가 적용되었습니다.")
@PostMapping("/like")
public BaseResponse<String> likeSubject(@PathParam("subjectId") Long subjectId) {
public BaseResponse<String> likeSubject(@RequestParam("subjectId") Long subjectId) {
boolean hasLiked = communityService.likeSubject(subjectId);
if (hasLiked) {
return BaseResponse.success("성공적으로 토론 게시글 공감이 적용이 되었습니다.");
Expand All @@ -91,8 +91,8 @@ public BaseResponse<String> likeSubject(@PathParam("subjectId") Long subjectId)
@ApiResponse(code = 200, message = "성공적으로 토론 댓글 공감이 적용/해제되었습니다.")
@PostMapping("/like/comment")
public BaseResponse<String> likeComment(
@PathParam("subjectId") Long subjectId,
@PathParam("commentId") Long commentId) {
@RequestParam("subjectId") Long subjectId,
@RequestParam("commentId") Long commentId) {
boolean hasLiked = communityService.likeComment(subjectId, commentId);
if (hasLiked) {
return BaseResponse.success("성공적으로 토론 댓글 공감 적용이 되었습니다.");
Expand Down Expand Up @@ -123,15 +123,16 @@ public BaseResponse<CommunitySubjectsDTO> getTotalProgramsSubjects(
@ApiImplicitParam(name = "page", dataType = "int", value = "페이지 번호(0부터 시작)", required = false, paramType = "path"),
@ApiImplicitParam(name = "direction", dataType = "String", value = "내림차순과 오름차순", required = false, paramType = "path"),
@ApiImplicitParam(name = "sort", dataType = "String", value = "정렬기준(createdAt, updatedAt)", required = false, paramType = "path"),
@ApiImplicitParam(name = "size", dataType = "int", value = "페이지당 아이템 갯수", required = false, paramType = "path")
@ApiImplicitParam(name = "size", dataType = "int", value = "페이지당 아이템 갯수", required = false, paramType = "path"),
@ApiImplicitParam(name = "programId", dataType = "long", value = "프로그램 id", required = true, paramType = "path")
})
@GetMapping("/program")
public BaseResponse<CommunitySubjectsDTO> getTotalProgramSubjects(
@PageableDefault(size = 10,
sort = "createdAt",
direction = Sort.Direction.DESC,
page = 0) Pageable pageable,
@PathParam("programId") Long programId) {
@RequestParam("programId") Long programId) {

CommunitySubjectsDTO page = communityService.findSingleProgramSubjects(pageable, programId);
return BaseResponse.success(page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import java.util.List;
import javax.websocket.server.PathParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tavebalak.OTTify.common.BaseResponse;
import tavebalak.OTTify.review.dto.response.LatestReviewsDTO;
Expand All @@ -34,7 +34,7 @@ public BaseResponse<List<LatestReviewsDTO>> getLatestReviews() {
@ApiImplicitParam(name = "id", dataType = "long", value = "공감하려고 하는 리뷰의 id", required = true, paramType = "path")
@ApiResponse(code = 200, message = "id = ? 인 아이디 값을 가진 리뷰에 공감이 적용/해제되었습니다.")
@PostMapping("/latestReviews/like")
public BaseResponse<String> likeReview(@PathParam("id") Long id) {
public BaseResponse<String> likeReview(@RequestParam("id") Long id) {
boolean hasSaved = reviewService.likeReview(id);
if (hasSaved) {
return BaseResponse.success("id = " + id + "인 아이디 값을 가진 리뷰에 공감이 적용되었습니다.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package tavebalak.OTTify.review.service;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tavebalak.OTTify.error.ErrorCode;
import tavebalak.OTTify.error.exception.NotFoundException;
import tavebalak.OTTify.oauth.jwt.SecurityUtil;
import tavebalak.OTTify.review.dto.response.LatestReviewsDTO;
import tavebalak.OTTify.review.entity.Review;
Expand All @@ -13,92 +21,96 @@
import tavebalak.OTTify.user.repository.LikedReviewRepository;
import tavebalak.OTTify.user.repository.UserRepository;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
public class ReviewServiceImpl implements ReviewService{
public class ReviewServiceImpl implements ReviewService {

private final ReviewRepository reviewRepository;
private final LikedReviewRepository likedReviewRepository;
private final UserRepository userRepository;
private final int TOP8_SIZE = 8;

public List<LatestReviewsDTO> getLatestReviews() {
List<Review> reviewList = reviewRepository.findAll(Sort.by(Sort.Direction.DESC, "createdAt"));
List<Review> reviewList = reviewRepository.findAll(
Sort.by(Sort.Direction.DESC, "createdAt"));
List<Review> top8ReviewList = new ArrayList<>();
if(reviewList.size()< TOP8_SIZE){
if (reviewList.size() < TOP8_SIZE) {
top8ReviewList.addAll(reviewList);
}else{
} else {
top8ReviewList.addAll(reviewList.subList(0, TOP8_SIZE));
}
return top8ReviewList.stream().map(
listOne -> LatestReviewsDTO.builder()
.reviewId(listOne.getId())
.nickName(listOne.getUser().getNickName())
.content(listOne.getContent())
.programTitle(listOne.getProgram().getTitle())
.userRating(listOne.getRating())
.profilePhoto(listOne.getUser().getProfilePhoto())
.likeCount(getLikeSum(listOne.getId()))
.build()
listOne -> LatestReviewsDTO.builder()
.reviewId(listOne.getId())
.nickName(listOne.getUser().getNickName())
.content(listOne.getContent())
.programTitle(listOne.getProgram().getTitle())
.userRating(listOne.getRating())
.profilePhoto(listOne.getUser().getProfilePhoto())
.likeCount(getLikeSum(listOne.getId()))
.build()
).collect(Collectors.toList());

}

private Integer getLikeSum(Long reviewId) {
Optional<List<LikedReview>> likedReviews = likedReviewRepository.findByReviewId(reviewId);
return likedReviews
.map(List::size)
.orElse(0);
.map(List::size)
.orElse(0);
}

public List<LatestReviewsDTO> getLatestReviewsTest() {
List<Review> reviewList = reviewRepository.findAll(Sort.by(Sort.Direction.DESC, "createdAt"));
List<Review> reviewList = reviewRepository.findAll(
Sort.by(Sort.Direction.DESC, "createdAt"));
List<Review> top8ReviewList = new ArrayList<>();
if(reviewList.size()< TOP8_SIZE){
if (reviewList.size() < TOP8_SIZE) {
top8ReviewList.addAll(reviewList);
}else{
} else {
top8ReviewList.addAll(reviewList.subList(0, TOP8_SIZE));
}
return top8ReviewList.stream().map(
listOne -> LatestReviewsDTO.builder()
.reviewId(listOne.getId())
.nickName(listOne.getUser().getNickName())
.content(listOne.getContent())
.programTitle(listOne.getProgram().getTitle())
.userRating(listOne.getRating())
.profilePhoto(listOne.getUser().getProfilePhoto())
.likeCount(0)
.build()
listOne -> LatestReviewsDTO.builder()
.reviewId(listOne.getId())
.nickName(listOne.getUser().getNickName())
.content(listOne.getContent())
.programTitle(listOne.getProgram().getTitle())
.userRating(listOne.getRating())
.profilePhoto(listOne.getUser().getProfilePhoto())
.likeCount(0)
.build()
).collect(Collectors.toList());

}


public void save(Review review){
public void save(Review review) {
reviewRepository.save(review);
}

@Override
public boolean likeReview(Long id) {
AtomicBoolean flag = new AtomicBoolean(false);
String userEmail = SecurityUtil.getCurrentEmail().get();
User savedUser = userRepository.findByEmail(userEmail).orElseThrow(NoSuchElementException::new);
Review review = reviewRepository.findById(id).orElseThrow(NoSuchElementException::new);
likedReviewRepository.findByUserIdAndReviewId(savedUser.getId(), review.getId()).ifPresentOrElse(
User savedUser = userRepository.findByEmail(userEmail)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND));
Review review = reviewRepository.findById(id).orElseThrow(() -> new NotFoundException(
ErrorCode.REVIEW_NOT_FOUND));
likedReviewRepository.findByUserIdAndReviewId(savedUser.getId(), review.getId())
.ifPresentOrElse(
likedReviewRepository::delete,
() -> {
likedReviewRepository.save(
LikedReview.builder()
.user(savedUser)
.review(reviewRepository.findById(id).orElseThrow(NoSuchElementException::new))
.build()
LikedReview.builder()
.user(savedUser)
.review(reviewRepository.findById(id)
.orElseThrow(NoSuchElementException::new))
.build()
);
flag.set(true);
}
);
);

return flag.get();
}
Expand Down

0 comments on commit 6b66a7e

Please sign in to comment.