Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/qna #39

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}