-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from hyundai-autoever-first-project-team3/refa…
…ctor/problem refactor: url 변경
- Loading branch information
Showing
4 changed files
with
86 additions
and
37 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
BE/Blog/src/main/java/hyundai/blog/problem/controller/ProblemController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
BE/Blog/src/main/java/hyundai/blog/problem/service/ProblemService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ProblemComment> problemCommentPage = problemCommentRepository.findAll(pageable); | ||
|
||
List<ProblemComments> 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<ProblemComments> problemCommentsList = new PageImpl<>(problemCommentsDtos, pageable, | ||
problemCommentPage.getTotalElements()); | ||
|
||
return ProblemCommentsPreviewDto.of(problem, problemCommentsList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters