From 96a060ecdbb93979ea867792d592b3ac0e73d020 Mon Sep 17 00:00:00 2001 From: Zoonmy Date: Fri, 25 Oct 2024 14:31:41 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20url=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../problem/controller/ProblemController.java | 29 ++++++++++ .../blog/problem/service/ProblemService.java | 57 +++++++++++++++++++ .../controller/ProblemCommentController.java | 13 ----- .../service/ProblemCommentService.java | 24 -------- 4 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 BE/Blog/src/main/java/hyundai/blog/problem/controller/ProblemController.java create mode 100644 BE/Blog/src/main/java/hyundai/blog/problem/service/ProblemService.java diff --git a/BE/Blog/src/main/java/hyundai/blog/problem/controller/ProblemController.java b/BE/Blog/src/main/java/hyundai/blog/problem/controller/ProblemController.java new file mode 100644 index 0000000..ee012aa --- /dev/null +++ b/BE/Blog/src/main/java/hyundai/blog/problem/controller/ProblemController.java @@ -0,0 +1,29 @@ +package hyundai.blog.problem.controller; + +import hyundai.blog.problem.service.ProblemService; +import hyundai.blog.problem_comment.dto.ProblemCommentsPreviewDto; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class ProblemController { + + private final ProblemService problemService; + + @GetMapping("/challenges/{challengeId}/problems/{problemId}") + public ResponseEntity getProblem( + @PathVariable Long challengeId, + @PathVariable Long problemId, + @RequestParam(defaultValue = "0") int page + ) { + ProblemCommentsPreviewDto problemCommentsPreviewDto = problemService.getProblemInfo( + problemId, page); + + return ResponseEntity.ok(problemCommentsPreviewDto); + } +} diff --git a/BE/Blog/src/main/java/hyundai/blog/problem/service/ProblemService.java b/BE/Blog/src/main/java/hyundai/blog/problem/service/ProblemService.java new file mode 100644 index 0000000..be3ed97 --- /dev/null +++ b/BE/Blog/src/main/java/hyundai/blog/problem/service/ProblemService.java @@ -0,0 +1,57 @@ +package hyundai.blog.problem.service; + +import hyundai.blog.member.entity.Member; +import hyundai.blog.member.exception.MemberIdNotFoundException; +import hyundai.blog.member.repository.MemberRepository; +import hyundai.blog.problem.entity.Problem; +import hyundai.blog.problem.exception.ProblemIdNotFoundException; +import hyundai.blog.problem.repository.ProblemRepository; +import hyundai.blog.problem_comment.dto.ProblemComments; +import hyundai.blog.problem_comment.dto.ProblemCommentsPreviewDto; +import hyundai.blog.problem_comment.entity.ProblemComment; +import hyundai.blog.problem_comment.repository.ProblemCommentRepository; +import hyundai.blog.util.MemberResolver; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class ProblemService { + + private static final int SIZE = 10; + + private final ProblemRepository problemRepository; + private final ProblemCommentRepository problemCommentRepository; + private final MemberRepository memberRepository; + + @Transactional + public ProblemCommentsPreviewDto getProblemInfo(Long problemId, int page) { + Pageable pageable = PageRequest.of(page, SIZE, Sort.by(Sort.Direction.DESC, "createdAt")); + + // 2) 페이지 요청에 따른 모든 Question 조회 + Page problemCommentPage = problemCommentRepository.findAll(pageable); + + List problemCommentsDtos = problemCommentPage.stream() + .map(problemComment -> { + Member member = memberRepository.findById(problemComment.getMemberId()) + .orElseThrow(MemberIdNotFoundException::new); + + return ProblemComments.of(member, problemComment); + }).toList(); + + Problem problem = problemRepository.findById(problemId) + .orElseThrow(ProblemIdNotFoundException::new); + + Page problemCommentsList = new PageImpl<>(problemCommentsDtos, pageable, + problemCommentPage.getTotalElements()); + + return ProblemCommentsPreviewDto.of(problem, problemCommentsList); + } +} diff --git a/BE/Blog/src/main/java/hyundai/blog/problem_comment/controller/ProblemCommentController.java b/BE/Blog/src/main/java/hyundai/blog/problem_comment/controller/ProblemCommentController.java index 1edffb3..6f7d6ce 100644 --- a/BE/Blog/src/main/java/hyundai/blog/problem_comment/controller/ProblemCommentController.java +++ b/BE/Blog/src/main/java/hyundai/blog/problem_comment/controller/ProblemCommentController.java @@ -43,17 +43,4 @@ public ResponseEntity deleteQuestion( return ResponseEntity.ok(response); } - @GetMapping("/problems/{problemId}/problem_comments") - public ResponseEntity getProblemComments( - @PathVariable Long problemId, - @RequestParam(defaultValue = "0") int page - ) { - System.out.println(problemId); - - ProblemCommentsPreviewDto questionsPreviewDtos = problemCommentService.getQuestions(problemId, page); - - return ResponseEntity.ok(questionsPreviewDtos); - } - - } diff --git a/BE/Blog/src/main/java/hyundai/blog/problem_comment/service/ProblemCommentService.java b/BE/Blog/src/main/java/hyundai/blog/problem_comment/service/ProblemCommentService.java index 52c0be8..61e7f55 100644 --- a/BE/Blog/src/main/java/hyundai/blog/problem_comment/service/ProblemCommentService.java +++ b/BE/Blog/src/main/java/hyundai/blog/problem_comment/service/ProblemCommentService.java @@ -90,30 +90,6 @@ public ProblemCommentDeleteResponse deleteProblemComment(Long problemCommentId) return ProblemCommentDeleteResponse.of(problemComment, "problem comment 삭제 완료"); } - @Transactional - public ProblemCommentsPreviewDto getQuestions(Long problemId, int page) { - Pageable pageable = PageRequest.of(page, SIZE, Sort.by(Sort.Direction.DESC, "createdAt")); - - // 2) 페이지 요청에 따른 모든 Question 조회 - Page problemCommentPage = problemCommentRepository.findAll(pageable); - - List problemCommentsDtos = problemCommentPage.stream() - .map(problemComment -> { - Member member = memberRepository.findById(problemComment.getMemberId()) - .orElseThrow(MemberIdNotFoundException::new); - - return ProblemComments.of(member, problemComment); - }).toList(); - - Problem problem = problemRepository.findById(problemId) - .orElseThrow(ProblemIdNotFoundException::new); - - Page problemCommentsList = new PageImpl<>(problemCommentsDtos, pageable, - problemCommentPage.getTotalElements()); - - return ProblemCommentsPreviewDto.of(problem, problemCommentsList); - } - private void validateProblemIdExists(Long problemId) { if (!problemRepository.existsById(problemId)) { throw new ProblemIdNotFoundException();