Skip to content

Commit

Permalink
refactor: Qna Controller and Service [#311]
Browse files Browse the repository at this point in the history
  • Loading branch information
kho903 committed Feb 13, 2022
1 parent a39d537 commit a0ea1e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.bancow.bancowback.domain.common.dto.ServiceResult;
import com.bancow.bancowback.domain.main.qna.dto.QnaReplyDto;
import com.bancow.bancowback.domain.main.qna.dto.QnaRequestDto;
import com.bancow.bancowback.domain.main.qna.dto.QnaResponseDto;
import com.bancow.bancowback.domain.main.qna.entity.Qna;
import com.bancow.bancowback.domain.main.qna.service.QnaService;

Expand All @@ -31,14 +32,14 @@ public class QnaController {
private final QnaService qnaService;

@GetMapping("/{id}")
public Response<Qna> getQna(@PathVariable Long id, @RequestHeader("TOKEN") String token) {
Qna qna = qnaService.getQna(token, id);
return new Response<>(qna, HttpStatus.OK);
public Response<QnaResponseDto> getQna(@PathVariable Long id, @RequestHeader("TOKEN") String token) {
QnaResponseDto dto = qnaService.getQna(token, id);
return new Response<>(dto, HttpStatus.OK);
}

@GetMapping
public Response<Page<Qna>> getQnaPaging(@RequestParam int page, @RequestHeader("TOKEN") String token) {
Page<Qna> pagingQna = qnaService.getQnaPaging(page, token);
public Response<Page<QnaResponseDto>> getQnaPaging(@RequestParam int page, @RequestHeader("TOKEN") String token) {
Page<QnaResponseDto> pagingQna = qnaService.getQnaPaging(page, token);
return new Response<>(pagingQna, HttpStatus.OK);
}

Expand All @@ -49,8 +50,8 @@ public Response<?> deleteQna(@PathVariable Long id, @RequestHeader("TOKEN") Stri
}

@PostMapping("/add")
public Response<Qna> addQna(@RequestBody @Valid QnaRequestDto qnaRequestDto) {
Qna result = qnaService.addQna(qnaRequestDto);
public Response<ServiceResult> addQna(@RequestBody @Valid QnaRequestDto qnaRequestDto) {
ServiceResult result = qnaService.addQna(qnaRequestDto);
return new Response<>(result, HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.bancow.bancowback.domain.common.util.token.service.TokenService;
import com.bancow.bancowback.domain.main.qna.dto.QnaReplyDto;
import com.bancow.bancowback.domain.main.qna.dto.QnaRequestDto;
import com.bancow.bancowback.domain.main.qna.dto.QnaResponseDto;
import com.bancow.bancowback.domain.main.qna.entity.Qna;
import com.bancow.bancowback.domain.main.qna.entity.QnaReply;
import com.bancow.bancowback.domain.main.qna.mapper.QnaMapper;
Expand All @@ -36,20 +37,20 @@ public class QnaService {
private final QnaReplyRepository qnaReplyRepository;
private final QnaReplyMapper qnaReplyMapper;

public Qna getQna(String token, Long qnaId) {
public QnaResponseDto getQna(String token, Long qnaId) {
tokenService.validTokenAuthority(token);
Qna qna = qnaRepository.findById(qnaId)
.orElseThrow(() -> new QnaException(NOT_Found_QNA, "해당 ID의 QnA를 찾을 수 없습니다."));

return qnaRepository.save(qna);
return qnaMapper.toResponse(qna);
}

public Page<Qna> getQnaPaging(int page, String token) {
public Page<QnaResponseDto> getQnaPaging(int page, String token) {
tokenService.validTokenAuthority(token);

return qnaRepository.findAll(
Page<Qna> qnaList = qnaRepository.findAll(
PageRequest.of(page, 10, Sort.by(Sort.Direction.DESC, "id"))
);
return qnaList.map(qnaMapper::toResponse);
}

public ServiceResult deleteQna(String token, Long id) {
Expand All @@ -61,9 +62,10 @@ public ServiceResult deleteQna(String token, Long id) {
return ServiceResult.success("QnA를 성공적으로 삭제하였습니다.");
}

public Qna addQna(QnaRequestDto qnaRequestDto) {
public ServiceResult addQna(QnaRequestDto qnaRequestDto) {
Qna qna = qnaMapper.toEntity(qnaRequestDto);
return qnaRepository.save(qna);
qnaRepository.save(qna);
return ServiceResult.success("QnA를 성공적으로 등록하였습니다.");
}

public ServiceResult replyQna(QnaReplyDto dto, String token, Long id) {
Expand Down

0 comments on commit a0ea1e7

Please sign in to comment.