diff --git a/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java b/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java index ce381ec..2e5db55 100644 --- a/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java +++ b/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java @@ -21,6 +21,8 @@ import org.springframework.data.web.PageableDefault; import org.springframework.web.bind.annotation.*; +import java.util.List; + @Tag(name = "qna", description = "자주하는 질문 API") @RestController @RequiredArgsConstructor @@ -29,13 +31,21 @@ public class QnaController { private final QnaService qnaService; - @Operation(description = "자주하는 질문 목록 조회하기", summary = "자주하는 질문 목록 조회") +// @Operation(description = "자주하는 질문 목록 조회하기(페이징)", summary = "자주하는 질문 목록 조회(페이징)") +// @ApiResponse(responseCode = "200", description = "OK", +// content = @Content(schema = @Schema(implementation = QnaListResponse.class))) +// @GetMapping("") +// @Parameter(name = "page", description = "페이지 번호", in = ParameterIn.QUERY) +// public QnaListResponse getAll(@Parameter(hidden = true) @PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) { +// return qnaService.getAll(pageable); +// } + + @Operation(description = "자주하는 질문/답변 목록 조회하기", summary = "자주하는 질문/답변 목록 조회") @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = QnaListResponse.class))) @GetMapping("") - @Parameter(name = "page", description = "페이지 번호", in = ParameterIn.QUERY) - public QnaListResponse getAll(@Parameter(hidden = true) @PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) { - return qnaService.getAll(pageable); + public List getAll() { + return qnaService.getAll(); } @Operation(description = "특정 자주하는 질문/답변 조회하기", summary = "특정 자주하는 질문/답변 조회") @@ -53,5 +63,19 @@ public void create(@RequestBody QnaRequest qnaRequest) { qnaService.create(qnaRequest); } + @Operation(description = "자주하는 질문/답변 수정하기", summary = "자주하는 질문/답변 수정") + @Parameter(name = "qnaId", description = "자주하는 질문/답변 ID") + @PutMapping("/{qnaId}") + public void delete(@PathVariable Long qnaId, @RequestBody QnaRequest qnaRequest) { + qnaService.update(qnaId, qnaRequest); + } + + @Operation(description = "자주하는 질문/답변 삭제하기", summary = "자주하는 질문/답변 삭제") + @Parameter(name = "qnaId", description = "자주하는 질문/답변 ID") + @DeleteMapping("/{qnaId}") + public void delete(@PathVariable Long qnaId) { + qnaService.delete(qnaId); + } + } diff --git a/src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java b/src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java index 6b9e0e8..50fd6b0 100644 --- a/src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java +++ b/src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java @@ -2,10 +2,15 @@ import kusitms.hdmedi.domain.announcement.Announcement; import kusitms.hdmedi.domain.qna.Qna; +import kusitms.hdmedi.dto.response.qna.QnaResponse; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface QnaRepository extends JpaRepository { + List findAllByOrderByCreatedAtDesc(); Page findAll(Pageable pageable); + } diff --git a/src/main/java/kusitms/hdmedi/service/qna/QnaService.java b/src/main/java/kusitms/hdmedi/service/qna/QnaService.java index 9631956..63140e6 100644 --- a/src/main/java/kusitms/hdmedi/service/qna/QnaService.java +++ b/src/main/java/kusitms/hdmedi/service/qna/QnaService.java @@ -10,30 +10,40 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class QnaService { private final QnaRepository qnaRepository; - public QnaListResponse getAll(Pageable pageable) { - List qnaResponses = qnaRepository.findAll(pageable) - .map(QnaResponse::new) - .getContent(); - - long maxpage = 0, cnt = qnaRepository.count(); - if (cnt > 0) { - maxpage = (cnt - 1) / pageable.getPageSize(); - } - QnaListResponse qnaListResponse = QnaListResponse.builder() - .maxpage(maxpage) - .data(qnaResponses) - .build(); +// public QnaListResponse getAll(Pageable pageable) { +// List qnaResponses = qnaRepository.findAll(pageable) +// .map(QnaResponse::new) +// .getContent(); +// +// long maxpage = 0, cnt = qnaRepository.count(); +// if (cnt > 0) { +// maxpage = (cnt - 1) / pageable.getPageSize(); +// } +// +// QnaListResponse qnaListResponse = QnaListResponse.builder() +// .maxpage(maxpage) +// .data(qnaResponses) +// .build(); +// +// return qnaListResponse; +// } - return qnaListResponse; + public List getAll() { + List qnaResponses = qnaRepository.findAllByOrderByCreatedAtDesc().stream() + .map(QnaResponse::new) + .collect(Collectors.toList()); + return qnaResponses; } public void create(QnaRequest qnaRequest) { @@ -52,4 +62,18 @@ public QnaResponse get(Long qnaId) { QnaResponse qnaResponse = new QnaResponse(qna); return qnaResponse; } + + @Transactional + public void update(Long qnaId, QnaRequest qnaRequest) { + Qna qna = qnaRepository.findById(qnaId) + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 질문/답변입니다")); + qna.update(qnaRequest.getQuestion(), qnaRequest.getAnswer()); + } + + public void delete(Long qnaId) { + Qna qna = qnaRepository.findById(qnaId) + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 질문/답변입니다")); + qnaRepository.delete(qna); + } + }