Skip to content

Commit

Permalink
Merge pull request #39 from hyundai-autoever-first-project-team3/refa…
Browse files Browse the repository at this point in the history
…ctor/problem

refactor: url 변경
  • Loading branch information
pjm2571 authored Oct 25, 2024
2 parents 6807399 + 96a060e commit 505bd13
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 37 deletions.
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
}

private void validateProblemIdExists(Long problemId) {
if (!problemRepository.existsById(problemId)) {
throw new ProblemIdNotFoundException();
Expand Down

0 comments on commit 505bd13

Please sign in to comment.