-
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 #35 from Kusitms-28th-HDmedi-B/feat/qna
feat(#34): 자주하는 질문/답변 CRUD API 구현
- Loading branch information
Showing
7 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/kusitms/hdmedi/controller/qna/QnaController.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 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); | ||
} | ||
|
||
|
||
} |
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,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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/kusitms/hdmedi/dto/request/qna/QnaRequest.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,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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/kusitms/hdmedi/dto/response/qna/QnaListResponse.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,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<QnaResponse> data; | ||
|
||
@Builder | ||
public QnaListResponse(Long maxpage, List<QnaResponse> data) { | ||
this.maxpage = maxpage; | ||
this.data = data; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/kusitms/hdmedi/dto/response/qna/QnaResponse.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,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")); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/kusitms/hdmedi/repository/qna/QnaRepository.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,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<Qna, Long> { | ||
Page<Qna> findAll(Pageable pageable); | ||
} |
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,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<QnaResponse> 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; | ||
} | ||
} |