From 92d6999bbc9cf05d372bd74e6a420649c022ea1b Mon Sep 17 00:00:00 2001 From: hojeong2747 Date: Sat, 16 Sep 2023 21:18:03 +0900 Subject: [PATCH] =?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?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hdmedi/controller/qna/QnaController.java | 57 +++++++++++++++++++ .../java/kusitms/hdmedi/domain/qna/Qna.java | 44 ++++++++++++++ .../hdmedi/dto/request/qna/QnaRequest.java | 20 +++++++ .../dto/response/qna/QnaListResponse.java | 22 +++++++ .../hdmedi/dto/response/qna/QnaResponse.java | 27 +++++++++ .../hdmedi/repository/qna/QnaRepository.java | 11 ++++ .../hdmedi/service/qna/QnaService.java | 55 ++++++++++++++++++ 7 files changed, 236 insertions(+) create mode 100644 src/main/java/kusitms/hdmedi/controller/qna/QnaController.java create mode 100644 src/main/java/kusitms/hdmedi/domain/qna/Qna.java create mode 100644 src/main/java/kusitms/hdmedi/dto/request/qna/QnaRequest.java create mode 100644 src/main/java/kusitms/hdmedi/dto/response/qna/QnaListResponse.java create mode 100644 src/main/java/kusitms/hdmedi/dto/response/qna/QnaResponse.java create mode 100644 src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java create mode 100644 src/main/java/kusitms/hdmedi/service/qna/QnaService.java diff --git a/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java b/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java new file mode 100644 index 0000000..ce381ec --- /dev/null +++ b/src/main/java/kusitms/hdmedi/controller/qna/QnaController.java @@ -0,0 +1,57 @@ +package kusitms.hdmedi.controller.qna; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.enums.ParameterIn; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import kusitms.hdmedi.dto.request.announcement.AnnouncementRequest; +import kusitms.hdmedi.dto.request.qna.QnaRequest; +import kusitms.hdmedi.dto.response.announcement.AnnouncementListResponse; +import kusitms.hdmedi.dto.response.announcement.AnnouncementResponse; +import kusitms.hdmedi.dto.response.news.NewsListResponse; +import kusitms.hdmedi.dto.response.qna.QnaListResponse; +import kusitms.hdmedi.dto.response.qna.QnaResponse; +import kusitms.hdmedi.service.qna.QnaService; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.web.bind.annotation.*; + +@Tag(name = "qna", description = "자주하는 질문 API") +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/qna") +public class QnaController { + + private final QnaService qnaService; + + @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 = QnaResponse.class))) + @Parameter(name = "qnaId", description = "질문/답변 ID") + @GetMapping("/{qnaId}") + public QnaResponse get(@PathVariable Long qnaId) { + return qnaService.get(qnaId); + } + + @Operation(description = "자주하는 질문/답변 작성하기", summary = "자주하는 질문/답변 작성") + @PostMapping("") + public void create(@RequestBody QnaRequest qnaRequest) { + qnaService.create(qnaRequest); + } + + +} diff --git a/src/main/java/kusitms/hdmedi/domain/qna/Qna.java b/src/main/java/kusitms/hdmedi/domain/qna/Qna.java new file mode 100644 index 0000000..aeb5ccf --- /dev/null +++ b/src/main/java/kusitms/hdmedi/domain/qna/Qna.java @@ -0,0 +1,44 @@ +package kusitms.hdmedi.domain.qna; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import java.time.LocalDateTime; + +@Entity +@Getter +@NoArgsConstructor +public class Qna { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String question; + + private String answer; + + @CreationTimestamp + private LocalDateTime createdAt; + + @UpdateTimestamp + private LocalDateTime updatedAt; + + @Builder + public Qna(String question, String answer) { + this.question = question; + this.answer = answer; + } + + public void update(String title, String content) { + this.question = question; + this.answer = answer; + } +} diff --git a/src/main/java/kusitms/hdmedi/dto/request/qna/QnaRequest.java b/src/main/java/kusitms/hdmedi/dto/request/qna/QnaRequest.java new file mode 100644 index 0000000..b14129f --- /dev/null +++ b/src/main/java/kusitms/hdmedi/dto/request/qna/QnaRequest.java @@ -0,0 +1,20 @@ +package kusitms.hdmedi.dto.request.qna; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Getter +public class QnaRequest { + + private String question; + + private String answer; + + @Builder + public QnaRequest(String question, String answer) { + this.question = question; + this.answer = answer; + } +} diff --git a/src/main/java/kusitms/hdmedi/dto/response/qna/QnaListResponse.java b/src/main/java/kusitms/hdmedi/dto/response/qna/QnaListResponse.java new file mode 100644 index 0000000..a8bfd70 --- /dev/null +++ b/src/main/java/kusitms/hdmedi/dto/response/qna/QnaListResponse.java @@ -0,0 +1,22 @@ +package kusitms.hdmedi.dto.response.qna; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@NoArgsConstructor +@Getter +public class QnaListResponse { + + private Long maxpage; + + private List data; + + @Builder + public QnaListResponse(Long maxpage, List data) { + this.maxpage = maxpage; + this.data = data; + } +} diff --git a/src/main/java/kusitms/hdmedi/dto/response/qna/QnaResponse.java b/src/main/java/kusitms/hdmedi/dto/response/qna/QnaResponse.java new file mode 100644 index 0000000..d20c973 --- /dev/null +++ b/src/main/java/kusitms/hdmedi/dto/response/qna/QnaResponse.java @@ -0,0 +1,27 @@ +package kusitms.hdmedi.dto.response.qna; + +import kusitms.hdmedi.domain.qna.Qna; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.format.DateTimeFormatter; + +@NoArgsConstructor +@Getter +public class QnaResponse { + + private Long id; + + private String question; + + private String answer; + + private String createdAt; + + public QnaResponse(Qna qna) { + id = qna.getId(); + question = qna.getQuestion(); + answer = qna.getAnswer(); + createdAt = qna.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy.mm.dd")); + } +} diff --git a/src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java b/src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java new file mode 100644 index 0000000..6b9e0e8 --- /dev/null +++ b/src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.java @@ -0,0 +1,11 @@ +package kusitms.hdmedi.repository.qna; + +import kusitms.hdmedi.domain.announcement.Announcement; +import kusitms.hdmedi.domain.qna.Qna; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface QnaRepository extends JpaRepository { + 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 new file mode 100644 index 0000000..9631956 --- /dev/null +++ b/src/main/java/kusitms/hdmedi/service/qna/QnaService.java @@ -0,0 +1,55 @@ +package kusitms.hdmedi.service.qna; + +import kusitms.hdmedi.domain.announcement.Announcement; +import kusitms.hdmedi.domain.qna.Qna; +import kusitms.hdmedi.dto.request.qna.QnaRequest; +import kusitms.hdmedi.dto.response.announcement.AnnouncementResponse; +import kusitms.hdmedi.dto.response.qna.QnaListResponse; +import kusitms.hdmedi.dto.response.qna.QnaResponse; +import kusitms.hdmedi.repository.qna.QnaRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@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(); + + return qnaListResponse; + } + + public void create(QnaRequest qnaRequest) { + Qna qna = Qna.builder() + .question(qnaRequest.getQuestion()) + .answer(qnaRequest.getAnswer()) + .build(); + + qnaRepository.save(qna); + + } + + public QnaResponse get(Long qnaId) { + Qna qna = qnaRepository.findById(qnaId) + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 질문/답변입니다")); + QnaResponse qnaResponse = new QnaResponse(qna); + return qnaResponse; + } +}