Skip to content

Commit 0c5d491

Browse files
authored
Merge pull request #230 from imad-project/master
#228 적용 내용 배포
2 parents 6f6701d + b16388c commit 0c5d491

9 files changed

Lines changed: 79 additions & 91 deletions

File tree

src/main/java/com/ncookie/imad/domain/posting/controller/PostingController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class PostingController {
2424
public ApiResponse<PostingDetailsResponse> postingDetails(@RequestHeader("Authorization") String accessToken,
2525
@PathVariable Long id) {
2626
return ApiResponse.createSuccess(ResponseCode.POSTING_GET_DETAILS_SUCCESS,
27-
postingService.getPosting(accessToken, id, false));
27+
postingService.getPostingById(accessToken, id, false));
2828
}
2929

3030
@Description("게시글 등록")

src/main/java/com/ncookie/imad/domain/posting/dto/response/PostingDetailsResponse.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public class PostingDetailsResponse {
5656
private Long scrapId;
5757
private boolean scrapStatus;
5858

59+
// 신고 여부
60+
private boolean isReported;
61+
5962

6063
public static PostingDetailsResponse toDTO(Posting posting, CommentListResponse commentList) {
6164
return PostingDetailsResponse.builder()

src/main/java/com/ncookie/imad/domain/posting/service/PostingService.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,19 @@ public class PostingService {
5454
private final UserActivityService userActivityService;
5555

5656

57-
public PostingDetailsResponse getPosting(String accessToken, Long postingId, boolean isPopularPosting) {
57+
public PostingDetailsResponse getPostingById(String accessToken, Long postingId, boolean isPopularPosting) {
5858
Posting posting = getPostingEntityById(postingId);
59+
return getPosting(accessToken, posting, isPopularPosting);
60+
}
61+
62+
public PostingDetailsResponse getPosting(String accessToken, Posting posting, boolean isPopularPosting) {
5963
UserAccount user = userRetrievalService.getUserFromAccessToken(accessToken);
6064

6165
// 댓글 조회
6266
// 게시글 조회 시 댓글 조회는 항상 날짜 기준/오름차순으로 첫 페이지의 데이터만 조회함
6367
CommentListResponse commentList = commentService.getCommentListByPosting(
6468
accessToken,
65-
postingId,
69+
posting.getPostingId(),
6670
0,
6771
null,
6872
0,
@@ -89,7 +93,7 @@ public PostingDetailsResponse getPosting(String accessToken, Long postingId, boo
8993
// 인기 게시글 조회 시 사용하는 경우가 아닐 때만 조회수 업데이트
9094
if (!isPopularPosting) {
9195
// 조회수 갱신
92-
postingRepository.updateViewCount(postingId, posting.getViewCnt() + 1);
96+
postingRepository.updateViewCount(posting.getPostingId(), posting.getViewCnt() + 1);
9397
todayPopularScoreService.addPopularPostingScore(posting, TodayPopularScoreService.POPULAR_POSTING_VIEW_CNT_SCORE);
9498
}
9599

@@ -101,6 +105,11 @@ public PostingDetailsResponse getPosting(String accessToken, Long postingId, boo
101105
postingDetailsResponse.setScrapId(scrap != null ? scrap.getId() : null);
102106
postingDetailsResponse.setScrapStatus(scrap != null);
103107

108+
// 신고 여부 조회 및 설정
109+
boolean isReported = reportService.isPostingReported(user, posting)
110+
|| reportService.isUserReported(user, posting.getUser());
111+
postingDetailsResponse.setReported(isReported);
112+
104113
return postingDetailsResponse;
105114
}
106115

@@ -234,11 +243,11 @@ public PostingDetailsResponse getMostLikePosting(String accessToken) {
234243
// 좋아요를 가장 많이 받은 게시글이 2개 이상일 때
235244
if (mostLikePostingList.size() > 1) {
236245
int randomNum = Utils.getRandomNum(mostLikePostingList.size());
237-
return getPosting(accessToken, mostLikePostingList.get(randomNum).getPostingId(), true);
246+
return getPosting(accessToken, mostLikePostingList.get(randomNum), true);
238247
}
239248

240249
// 좋아요를 가장 많이 받은 게시글이 1개일 때
241-
return getPosting(accessToken, mostLikePostingList.get(0).getPostingId(), true);
250+
return getPosting(accessToken, mostLikePostingList.get(0), true);
242251
}
243252

244253
public PostingIdResponse addPosting(String accessToken, AddPostingRequest addPostingRequest) {
Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.ncookie.imad.domain.ranking.controller;
22

33
import com.ncookie.imad.domain.posting.dto.response.PostingDetailsResponse;
4-
import com.ncookie.imad.domain.posting.service.PostingService;
54
import com.ncookie.imad.domain.ranking.service.TodayPopularPostingService;
65
import com.ncookie.imad.domain.ranking.service.TodayPopularReviewService;
76
import com.ncookie.imad.domain.review.dto.response.ReviewDetailsResponse;
8-
import com.ncookie.imad.domain.review.service.ReviewService;
97
import com.ncookie.imad.global.dto.response.ApiResponse;
108
import com.ncookie.imad.global.dto.response.ResponseCode;
119
import jdk.jfr.Description;
@@ -22,54 +20,22 @@
2220
@RestController
2321
@RequestMapping("/api/popular")
2422
public class TodayPopularController {
23+
2524
private final TodayPopularReviewService todayPopularReviewService;
2625
private final TodayPopularPostingService todayPopularPostingService;
2726

28-
private final ReviewService reviewService;
29-
private final PostingService postingService;
30-
3127

3228
@Description("오늘의 리뷰 조회")
3329
@GetMapping("/review")
34-
public ApiResponse<ReviewDetailsResponse> getPopularReview() {
35-
ReviewDetailsResponse todayPopularReview = todayPopularReviewService.getTodayPopularReview();
36-
37-
// 현재 인기 리뷰 데이터가 없는 상태이므로 좋아요가 가장 많은 리뷰 데이터 반환
38-
if (todayPopularReview == null) {
39-
log.info("인기 리뷰 데이터가 없으므로 좋아요가 가장 많은 리뷰를 조회합니다...");
40-
ReviewDetailsResponse popularReview = reviewService.getMostLikeReview();
41-
42-
// 리뷰 좋아요 데이터도 없는 경우
43-
if (popularReview == null) {
44-
log.info("리뷰의 좋아요 데이터도 존재하지 않으므로 이에 맞는 응답을 반환합니다.");
45-
return ApiResponse.createSuccess(ResponseCode.POPULAR_REVIEW_ALL_NULL, null);
46-
}
47-
return ApiResponse.createSuccess(ResponseCode.POPULAR_REVIEW_NULL_AND_GET_REVIEW, popularReview);
48-
}
49-
50-
// 오늘의 리뷰 데이터 반환
30+
public ApiResponse<ReviewDetailsResponse> getPopularReview(@RequestHeader("Authorization") String accessToken) {
31+
ReviewDetailsResponse todayPopularReview = todayPopularReviewService.getTodayPopularReview(accessToken);
5132
return ApiResponse.createSuccess(ResponseCode.POPULAR_REVIEW_GET_SUCCESS, todayPopularReview);
5233
}
5334

5435
@Description("인기 게시글 조회")
5536
@GetMapping("/posting")
5637
public ApiResponse<PostingDetailsResponse> getPopularPosting(@RequestHeader("Authorization") String accessToken) {
5738
PostingDetailsResponse todayPopularPosting = todayPopularPostingService.getTodayPopularPosting(accessToken);
58-
59-
// 현재 인기 게시글 데이터가 없는 상태이므로 좋아요가 가장 많은 게시글 데이터 반환
60-
if (todayPopularPosting == null) {
61-
log.info("인기 리뷰 데이터가 없으므로 좋아요가 가장 많은 리뷰를 조회합니다...");
62-
PostingDetailsResponse mostLikePosting = postingService.getMostLikePosting(accessToken);
63-
64-
// 리뷰 좋아요 데이터도 없는 경우
65-
if (mostLikePosting == null) {
66-
log.info("리뷰의 좋아요 데이터도 존재하지 않으므로 이에 맞는 응답을 반환합니다.");
67-
return ApiResponse.createSuccess(ResponseCode.POPULAR_POSTING_ALL_NULL, null);
68-
}
69-
return ApiResponse.createSuccess(ResponseCode.POPULAR_POSTING_NULL_AND_GET_POSTING, mostLikePosting);
70-
}
71-
72-
// 오늘의 리뷰 데이터 반환
7339
return ApiResponse.createSuccess(ResponseCode.POPULAR_POSTING_GET_SUCCESS, todayPopularPosting);
7440
}
7541
}

src/main/java/com/ncookie/imad/domain/ranking/service/TodayPopularPostingService.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,23 @@ public class TodayPopularPostingService {
2424

2525
public PostingDetailsResponse getTodayPopularPosting(String accessToken) {
2626
List<TodayPopularPosting> popularPostingList = todayPopularPostingsRepository.findTopByPopularScore();
27+
PostingDetailsResponse todayPopularPosting;
2728

28-
// 인기 게시글 데이터가 존재하지 않으면 null 반환
2929
if (popularPostingList.isEmpty()) {
30-
log.info("오늘의 게시글 데이터가 존재하지 않습니다.");
31-
return null;
32-
}
33-
34-
// 인기 점수가 가장 높은 게시글이 2개 이상일 때 랜덤으로 반환
35-
if (popularPostingList.size() > 1) {
30+
// 인기 게시글 데이터가 존재하지 않으면 null 반환
31+
log.info("오늘의 게시글 데이터가 존재하지 않아 좋아요를 가장 많이 받은 게시글을 조회합니다.");
32+
todayPopularPosting = postingService.getMostLikePosting(accessToken);
33+
} else if (popularPostingList.size() > 1) {
34+
// 인기 점수가 가장 높은 게시글이 2개 이상일 때 랜덤으로 반환
3635
int randomNum = Utils.getRandomNum(popularPostingList.size());
3736

3837
log.info("인기 점수가 가장 높은 게시글이 2개 이상이므로 이 중 랜덤으로 반환합니다");
39-
return postingService.getPosting(accessToken, popularPostingList.get(randomNum).getPosting().getPostingId(), true);
38+
todayPopularPosting = postingService.getPosting(accessToken, popularPostingList.get(randomNum).getPosting(), true);
39+
} else {
40+
todayPopularPosting = postingService.getPosting(accessToken, popularPostingList.get(0).getPosting(), true);
4041
}
41-
42+
4243
log.info("오늘의 게시글 데이터를 반환합니다");
43-
return postingService.getPosting(accessToken, popularPostingList.get(0).getPosting().getPostingId(), true);
44+
return todayPopularPosting;
4445
}
4546
}

src/main/java/com/ncookie/imad/domain/ranking/service/TodayPopularReviewService.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.ncookie.imad.domain.ranking.entity.TodayPopularReview;
44
import com.ncookie.imad.domain.ranking.repository.TodayPopularReviewsRepository;
55
import com.ncookie.imad.domain.review.dto.response.ReviewDetailsResponse;
6+
import com.ncookie.imad.domain.review.service.ReviewService;
67
import com.ncookie.imad.global.Utils;
78
import jakarta.transaction.Transactional;
89
import lombok.RequiredArgsConstructor;
@@ -17,25 +18,29 @@
1718
@Transactional
1819
@Service
1920
public class TodayPopularReviewService {
21+
private final ReviewService reviewService;
22+
2023
private final TodayPopularReviewsRepository todayPopularReviewsRepository;
2124

22-
public ReviewDetailsResponse getTodayPopularReview() {
25+
public ReviewDetailsResponse getTodayPopularReview(String accessToken) {
2326
List<TodayPopularReview> popularReviewList = todayPopularReviewsRepository.findTopByPopularScore();
24-
// 인기 리뷰 데이터가 존재하지 않으면 null 반환
25-
if (popularReviewList.isEmpty()) {
26-
log.info("오늘의 리뷰 데이터가 존재하지 않습니다.");
27-
return null;
28-
}
27+
ReviewDetailsResponse todayPopularReview;
2928

30-
// 인기 리뷰 점수가 가장 높은 리뷰가 2개 이상일 때 랜덤으로 반환
31-
if (popularReviewList.size() > 1) {
29+
if (popularReviewList.isEmpty()) {
30+
// 인기 리뷰 데이터가 존재하지 않으면 좋아요를 가장 많이 받은 리뷰 반환
31+
log.info("오늘의 리뷰 데이터가 존재하지 않아 좋아요가 가장 많은 리뷰를 반환합니다.");
32+
todayPopularReview = reviewService.getMostLikeReview(accessToken);
33+
} else if (popularReviewList.size() > 1) {
34+
// 인기 리뷰 점수가 가장 높은 리뷰가 2개 이상일 때 랜덤으로 반환
3235
int randomNum = Utils.getRandomNum(popularReviewList.size());
3336

3437
log.info("인기 점수가 가장 높은 리뷰가 2개 이상이므로 이 중 랜덤으로 반환합니다");
35-
return ReviewDetailsResponse.toDTO(popularReviewList.get(randomNum).getReview());
38+
todayPopularReview = reviewService.getReview(accessToken, popularReviewList.get(randomNum).getReview());
39+
} else {
40+
todayPopularReview = reviewService.getReview(accessToken, popularReviewList.get(0).getReview());
3641
}
3742

3843
log.info("오늘의 리뷰 데이터를 반환합니다");
39-
return ReviewDetailsResponse.toDTO(popularReviewList.get(0).getReview());
44+
return todayPopularReview;
4045
}
4146
}

src/main/java/com/ncookie/imad/domain/review/controller/ReviewController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ReviewController {
2121
@GetMapping("/{id}")
2222
public ApiResponse<ReviewDetailsResponse> reviewDetails(@RequestHeader("Authorization") String accessToken,
2323
@PathVariable("id") Long id) {
24-
return ApiResponse.createSuccess(ResponseCode.REVIEW_GET_DETAILS_SUCCESS, reviewService.getReview(accessToken, id));
24+
return ApiResponse.createSuccess(ResponseCode.REVIEW_GET_DETAILS_SUCCESS, reviewService.getReviewById(accessToken, id));
2525
}
2626

2727
@GetMapping("/list")

src/main/java/com/ncookie/imad/domain/review/service/ReviewService.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,40 @@ public class ReviewService {
5353
private final TodayPopularScoreService todayPopularScoreService;
5454
private final UserActivityService userActivityService;
5555

56-
public ReviewDetailsResponse getReview(String accessToken, Long reviewId) {
56+
public ReviewDetailsResponse getReviewById(String accessToken, Long reviewId) {
5757
Optional<Review> optional = reviewRepository.findById(reviewId);
58-
UserAccount user = userRetrievalService.getUserFromAccessToken(accessToken);
5958

6059
if (optional.isPresent()) {
61-
Review review = optional.get();
62-
63-
// 본인 작성 여부
64-
boolean isAuthor;
65-
if (user == null) {
66-
isAuthor = false;
67-
} else {
68-
isAuthor = review.getUserAccount().getId().equals(user.getId());
69-
}
70-
71-
ReviewLike reviewLike = reviewLikeService.findByUserAccountAndE(user, review);
72-
int likeStatus = reviewLike == null ? 0 : reviewLike.getLikeStatus();
60+
return getReview(accessToken, optional.get());
61+
} else {
62+
throw new BadRequestException(ResponseCode.REVIEW_NOT_FOUND);
63+
}
64+
}
7365

74-
ReviewDetailsResponse reviewDetailsResponse = ReviewDetailsResponse.toDTO(review);
75-
reviewDetailsResponse.setAuthor(isAuthor);
76-
reviewDetailsResponse.setLikeStatus(likeStatus);
66+
public ReviewDetailsResponse getReview(String accessToken, Review review) {
67+
UserAccount user = userRetrievalService.getUserFromAccessToken(accessToken);
7768

78-
return reviewDetailsResponse;
69+
// 본인 작성 여부
70+
boolean isAuthor;
71+
if (user == null) {
72+
isAuthor = false;
7973
} else {
80-
throw new BadRequestException(ResponseCode.REVIEW_NOT_FOUND);
74+
isAuthor = review.getUserAccount().getId().equals(user.getId());
8175
}
76+
77+
ReviewLike reviewLike = reviewLikeService.findByUserAccountAndE(user, review);
78+
int likeStatus = reviewLike == null ? 0 : reviewLike.getLikeStatus();
79+
80+
ReviewDetailsResponse reviewDetailsResponse = ReviewDetailsResponse.toDTO(review);
81+
reviewDetailsResponse.setAuthor(isAuthor);
82+
reviewDetailsResponse.setLikeStatus(likeStatus);
83+
84+
// 신고 여부
85+
boolean isReported = reportService.isReviewReported(user, review)
86+
|| reportService.isUserReported(user, review.getUserAccount());
87+
reviewDetailsResponse.setReported(isReported);
88+
89+
return reviewDetailsResponse;
8290
}
8391

8492
public ReviewListResponse getReviewList(String accessToken, Long contentsId, int pageNumber, String sortString, int order) {
@@ -124,7 +132,7 @@ public ReviewListResponse getLikedReviewListByUser(UserAccount user, int pageNum
124132
);
125133
}
126134

127-
public ReviewDetailsResponse getMostLikeReview() {
135+
public ReviewDetailsResponse getMostLikeReview(String accessToken) {
128136
List<Review> mostLikeReviewList = reviewRepository.findMostLikeReview();
129137

130138
if (mostLikeReviewList.isEmpty()) {
@@ -133,9 +141,10 @@ public ReviewDetailsResponse getMostLikeReview() {
133141

134142
if (mostLikeReviewList.size() > 1) {
135143
int randomNum = Utils.getRandomNum(mostLikeReviewList.size());
136-
return ReviewDetailsResponse.toDTO(mostLikeReviewList.get(randomNum));
144+
return getReview(accessToken, mostLikeReviewList.get(randomNum));
137145
}
138-
return ReviewDetailsResponse.toDTO(mostLikeReviewList.get(0));
146+
147+
return getReview(accessToken, mostLikeReviewList.get(0));
139148
}
140149

141150
public int getWrittenReviewCount(UserAccount user) {

src/main/java/com/ncookie/imad/global/dto/response/ResponseCode.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,7 @@ public enum ResponseCode {
122122
RANKING_WRONG_PERIOD(400, "옯바르지 않은 랭킹 기간입니다."),
123123

124124
POPULAR_REVIEW_GET_SUCCESS(200, "정상적으로 오늘의 리뷰를 조회했습니다."),
125-
POPULAR_REVIEW_NULL_AND_GET_REVIEW(200, "오늘의 리뷰 데이터가 없으므로 좋아요가 가장 많은 리뷰를 조회했습니다."),
126-
POPULAR_REVIEW_ALL_NULL(200, "오늘의 리뷰와 리뷰의 좋아요 데이터 모두 존재하지 않습니다."),
127-
128125
POPULAR_POSTING_GET_SUCCESS(200, "정상적으로 오늘의 게시글을 조회했습니다."),
129-
POPULAR_POSTING_NULL_AND_GET_POSTING(200, "오늘의 게시글 데이터가 없으므로 좋아요가 가장 많은 게시글을 조회했습니다."),
130-
POPULAR_POSTING_ALL_NULL(200, "오늘의 게시글과 게시글의 좋아요 데이터 모두 존재하지 않습니다."),
131126

132127
// 추천
133128
RECOMMEND_GET_SUCCESS(200, "정상적으로 작품 추천 데이터를 조회했습니다."),

0 commit comments

Comments
 (0)