Skip to content

Commit

Permalink
Merge pull request #39 from Kusitms-28th-HDmedi-B/feat/qna
Browse files Browse the repository at this point in the history
Feat/qna
  • Loading branch information
hojeong2747 authored Sep 17, 2023
2 parents a0ef5d5 + f77213a commit c04a907
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
32 changes: 28 additions & 4 deletions src/main/java/kusitms/hdmedi/controller/qna/QnaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<QnaResponse> getAll() {
return qnaService.getAll();
}

@Operation(description = "특정 자주하는 질문/답변 조회하기", summary = "특정 자주하는 질문/답변 조회")
Expand All @@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Qna, Long> {
List<Qna> findAllByOrderByCreatedAtDesc();
Page<Qna> findAll(Pageable pageable);

}
52 changes: 38 additions & 14 deletions src/main/java/kusitms/hdmedi/service/qna/QnaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<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();
// 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;
// }

return qnaListResponse;
public List<QnaResponse> getAll() {
List<QnaResponse> qnaResponses = qnaRepository.findAllByOrderByCreatedAtDesc().stream()
.map(QnaResponse::new)
.collect(Collectors.toList());
return qnaResponses;
}

public void create(QnaRequest qnaRequest) {
Expand All @@ -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);
}

}

0 comments on commit c04a907

Please sign in to comment.