Skip to content

Commit

Permalink
Merge pull request #81 from Leets-Official/refactor-#80
Browse files Browse the repository at this point in the history
Refactor #80
jj0526 authored Jul 31, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 689d171 + 0097167 commit 2c7c420
Showing 5 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -138,22 +138,25 @@ public CommonResponse<Void> createWeek(@RequestBody @Valid AttendanceDTO.Week dt
public CommonResponse<List<ResponseWeekCode>> getWeekCode(@PathVariable Integer cardinal) {
return CommonResponse.createSuccess(weekService.getWeekCode(cardinal));
}
/*
Penalty 관련 admin api
*/

@Operation(summary = "패널티 부여")
@Operation(summary = "패널티 부여", description = "관리자가 특정 유저에게 패널티를 부여합니다.")
@PostMapping("/penalty")
public CommonResponse<Void> assignPenalty(@RequestBody RequestPenalty requestPenalty) {
penaltyService.assignPenalty(requestPenalty);
return CommonResponse.createSuccess();
}

@Operation(summary = "패널티 삭제")
@Operation(summary = "패널티 삭제", description = "관리자가 특정 패널티를 삭제합니다.")
@DeleteMapping("/penalty")
public CommonResponse<Void> removePenalty(@RequestParam Long penaltyId) {
penaltyService.removePenalty(penaltyId);
return CommonResponse.createSuccess();
}

@Operation(summary = "모든 유저의 패널티 확인")
@Operation(summary = "모든 유저의 패널티 확인", description = "관리자가 모든 패널티를 userId 순서로 조회합니다.")
@GetMapping("/penalty/all")
public CommonResponse<List<ResponsePenalty>> showAllPenalty() {
List<ResponsePenalty> allPenalties = penaltyService.getAllPenaltiesSortedByUserId();
Original file line number Diff line number Diff line change
@@ -37,15 +37,15 @@ public CommonResponse<List<ResponsePostDTO>> findAllPosts(){
return CommonResponse.createSuccess(posts);
}

@Operation(summary = "본인의 게시글 조회")
@Operation(summary = "최근 게시글 15개 조회")
@GetMapping("/load")
public CommonResponse<List<ResponsePostDTO>> loadPosts(@RequestParam(required = false) Long lastPostId) throws InvalidAccessException {
List<ResponsePostDTO> postsLoaded = postService.loadPosts(lastPostId);
return CommonResponse.createSuccess(postsLoaded);
}



@Operation(summary = "본인의 게시글 조회")
@GetMapping("/myPosts")
public CommonResponse<List<ResponsePostDTO>> showMyPost(@CurrentUser Long userId){
List<ResponsePostDTO> myPost = postService.myPosts(userId);
21 changes: 16 additions & 5 deletions src/main/java/leets/weeth/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
@@ -70,12 +70,23 @@ public static Long calculateTotalComments(Post post) {
}

private Long countCommentsRecursively(Comment comment) {
if (comment.getChildren() == null) {
return 1L; // 현재 댓글만 카운트
// 부모 댓글이 삭제된 경우
if(comment.getIsDeleted()){
if (comment.getChildren() == null) {
return 0L; // 0개
}
return comment.getChildren().stream()
.mapToLong(this::countCommentsRecursively)
.sum(); // 자식 댓글만 카운트
}
else{
if (comment.getChildren() == null) {
return 1L; // 현재 댓글만 카운트
}
return 1L + comment.getChildren().stream()
.mapToLong(this::countCommentsRecursively)
.sum(); // 현재 및 자식 댓글 카운트
}
return 1L + comment.getChildren().stream()
.mapToLong(this::countCommentsRecursively)
.sum();
}

@PrePersist
Original file line number Diff line number Diff line change
@@ -23,19 +23,23 @@ public class CommentService {
public void createComment(Long userId, Long postId, RequestCommentDTO requestCommentDTO) {
User user = userRepository.findById(userId)
.orElseThrow(UserNotFoundException::new);
// 해당 유저가 없는 경우
Post currentPost = postRepository.findById(postId).orElseThrow(PostNotFoundException::new);
// 해당 게시물이 없는 경우

Comment newComment = Comment.createComment(requestCommentDTO, currentPost, user);
Comment parentComment;
commentRepository.save(newComment);
// child인 경우(부모가 있는 경우)
// 댓글이 child인 경우(부모가 있는 경우)
if(requestCommentDTO.getParentCommentId()!=null){
parentComment = commentRepository.findById(requestCommentDTO.getParentCommentId())
.orElseThrow(CommentNotFoundException::new);
parentComment.addChild(newComment);
// 자식 추가
commentRepository.save(parentComment);
}
else{
// 댓글이 부모인 경우
currentPost.addComment(newComment);
postRepository.save(currentPost);
}
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Slf4j
@@ -36,7 +35,7 @@ public List<ResponsePostDTO> findAllPosts() {
List<Post> posts = postRepository.findAll(Sort.by(Sort.Direction.ASC, "id"));
return posts.stream()
.map(ResponsePostDTO::createResponsePostDTO)
.collect(Collectors.toList());
.toList();
}

// 특정 postId의 게시물만 조회
@@ -55,7 +54,7 @@ public List<ResponsePostDTO> myPosts(Long userId){
// Post 리스트를 ResponsePostDTO 리스트로 변환
return myPosts.stream()
.map(ResponsePostDTO::createResponsePostDTO) // Post -> ResponsePostDTO 변환
.collect(Collectors.toList());
.toList();
}

@Transactional

0 comments on commit 2c7c420

Please sign in to comment.