From e9f42bfe5c92e2550f05880be43387f4cd25ac70 Mon Sep 17 00:00:00 2001 From: hojeong2747 Date: Sun, 17 Sep 2023 17:05:28 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix(#34):=20=EC=9E=90=EC=A3=BC=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EC=A7=88=EB=AC=B8/=EB=8B=B5=EB=B3=80=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=95=20=EC=B2=98=EB=A6=AC=20=EC=97=86=EA=B2=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hdmedi/controller/qna/QnaController.java | 18 +++++++-- .../hdmedi/repository/qna/QnaRepository.java | 5 +++ .../hdmedi/service/qna/QnaService.java | 37 ++++++++++++------- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java b/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java index ce381ec..efe8e55 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 = "특정 자주하는 질문/답변 조회") 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..cc532a9 100644 --- a/src/main/java/kusitms/hdmedi/service/qna/QnaService.java +++ b/src/main/java/kusitms/hdmedi/service/qna/QnaService.java @@ -12,28 +12,37 @@ import org.springframework.stereotype.Service; 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) { From f77213ab3ea170611f7e09fc55655da77e4d0283 Mon Sep 17 00:00:00 2001 From: hojeong2747 Date: Sun, 17 Sep 2023 17:12:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat(#34):=20=EC=9E=90=EC=A3=BC=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EC=A7=88=EB=AC=B8/=EB=8B=B5=EB=B3=80=20CRUD=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hdmedi/controller/qna/QnaController.java | 14 ++++++++++++++ .../kusitms/hdmedi/service/qna/QnaService.java | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java b/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java index efe8e55..2e5db55 100644 --- a/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java +++ b/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java @@ -63,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/service/qna/QnaService.java b/src/main/java/kusitms/hdmedi/service/qna/QnaService.java index cc532a9..63140e6 100644 --- a/src/main/java/kusitms/hdmedi/service/qna/QnaService.java +++ b/src/main/java/kusitms/hdmedi/service/qna/QnaService.java @@ -10,6 +10,7 @@ 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; @@ -61,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); + } + }