-
Notifications
You must be signed in to change notification settings - Fork 0
feat: QnA 기능 구현 #25
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
Merged
Merged
feat: QnA 기능 구현 #25
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
70a4c3a
docs: 사용자 qna swagger 작성
brothergiven 2dc5e84
docs: 관리자 qna swagger 작성
brothergiven f1b14f1
feat: QnA Sort type 정의(시간순)
brothergiven ed7780c
feat: isSovled 필드 추가
brothergiven 1dc386c
feat: dto 작성
brothergiven 8c0660c
feat: QnA 조회 기능 구현
brothergiven acb6bc0
feat: QnA 단건 조회 기능 구현
brothergiven fc60c04
feat: QnA 생성 기능 구현
brothergiven ce1aa5d
feat: QnA 수정 기능 구현
brothergiven b7c5dc8
feat: QnA 삭제 기능 구현
brothergiven 6645186
feat: QnA 답변 생성 기능 구현
brothergiven 42e4fad
docs: swagger Reponse 예시 수정
brothergiven a1bb539
feat: User-Answer 연관관계 연결 설정
brothergiven 07136f2
feat: 이미 작성된 답변 작성 못하도록 구현
brothergiven 68dd4d0
refact: 불필요 애노테이션 삭제
brothergiven 411da85
docs: Req DTO Swagger 설명 추가
brothergiven cd62d45
refact: Solved 검증 위치 수정
brothergiven 9613626
refact: InquiryService의 단일책임을 위한 리팩터링
brothergiven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/Gotcha/domain/inquiry/api/AdminInquiryApi.java
This file contains hidden or 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,61 @@ | ||
| package Gotcha.domain.inquiry.api; | ||
|
|
||
| import Gotcha.common.jwt.auth.SecurityUserDetails; | ||
| import Gotcha.domain.inquiry.dto.AnswerReq; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.media.Content; | ||
| import io.swagger.v3.oas.annotations.media.ExampleObject; | ||
| import io.swagger.v3.oas.annotations.parameters.RequestBody; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
|
|
||
| @Tag(name = "[관리자 QnA API]", description = "관리자 QnA 처리 관련 API") | ||
| public interface AdminInquiryApi { | ||
|
|
||
| @Operation(summary = "QnA 답변 생성", description = "QnA 답변 생성 API") | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "QnA 답변 생성 성공", | ||
| content = @Content(mediaType = "application/json", examples = { | ||
| @ExampleObject(value = """ | ||
| { | ||
| "status": "OK", | ||
| "message": "QnA 답변 생성에 성공했습니다." | ||
| } | ||
| """) | ||
| }) | ||
| ), | ||
| @ApiResponse(responseCode = "400", description = "필드 검증 오류", | ||
| content = @Content(mediaType = "application/json", examples = { | ||
| @ExampleObject(value = """ | ||
| { | ||
| "status": "BAD_REQUEST", | ||
| "message": "필드 검증 오류입니다.", | ||
| "fields": { | ||
| "content": "내용은 필수 입력 사항입니다." | ||
| } | ||
| } | ||
| """) | ||
| }) | ||
| ), | ||
| @ApiResponse(responseCode = "403", description = "권한 없음", | ||
| content = @Content(mediaType = "application/json", examples = { | ||
| @ExampleObject(value = """ | ||
| { | ||
| "status": "FORBIDDEN", | ||
| "message": "권한이 없습니다." | ||
| } | ||
| """) | ||
| }) | ||
| ) | ||
| }) | ||
| ResponseEntity<?> createAnswer(@Valid @RequestBody AnswerReq answerReq, @PathVariable(value = "inquiryId")Long inquiryId, | ||
| @AuthenticationPrincipal SecurityUserDetails userDetails); | ||
|
|
||
|
|
||
|
|
||
| } |
304 changes: 304 additions & 0 deletions
304
src/main/java/Gotcha/domain/inquiry/api/InquiryApi.java
Large diffs are not rendered by default.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
src/main/java/Gotcha/domain/inquiry/controller/AdminInquiryController.java
This file contains hidden or 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,38 @@ | ||
| package Gotcha.domain.inquiry.controller; | ||
|
|
||
| import Gotcha.common.api.SuccessRes; | ||
| import Gotcha.common.jwt.auth.SecurityUserDetails; | ||
| import Gotcha.domain.inquiry.api.AdminInquiryApi; | ||
| import Gotcha.domain.inquiry.dto.AnswerReq; | ||
| import Gotcha.domain.inquiry.entity.Inquiry; | ||
| import Gotcha.domain.inquiry.service.AnswerService; | ||
| import Gotcha.domain.inquiry.service.InquiryService; | ||
| import Gotcha.domain.user.entity.User; | ||
| import Gotcha.domain.user.service.UserService; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/admin/qnas/") | ||
| @RequiredArgsConstructor | ||
| public class AdminInquiryController implements AdminInquiryApi { | ||
|
|
||
| private final AnswerService answerService; | ||
|
|
||
| private final InquiryService inquiryService; | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| @Override | ||
| @PostMapping("/{inquiryId}/answer") | ||
| public ResponseEntity<?> createAnswer(@Valid @RequestBody AnswerReq answerReq, @PathVariable(value = "inquiryId") Long inquiryId, | ||
| @AuthenticationPrincipal SecurityUserDetails securityUserDetails) { | ||
| Inquiry inquiry = inquiryService.findInquiryById(inquiryId); | ||
| User writer = userService.findUserByUserId(securityUserDetails.getId()); | ||
| answerService.createAnswer(answerReq, inquiry, writer); | ||
| return ResponseEntity.ok(SuccessRes.from("QnA 답변 생성에 성공했습니다.")); | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/main/java/Gotcha/domain/inquiry/controller/InquiryController.java
This file contains hidden or 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,86 @@ | ||
| package Gotcha.domain.inquiry.controller; | ||
|
|
||
| import Gotcha.common.api.SuccessRes; | ||
| import Gotcha.common.jwt.auth.SecurityUserDetails; | ||
| import Gotcha.domain.inquiry.api.InquiryApi; | ||
| import Gotcha.domain.inquiry.dto.InquiryReq; | ||
| import Gotcha.domain.inquiry.dto.InquiryRes; | ||
| import Gotcha.domain.inquiry.dto.InquirySortType; | ||
| import Gotcha.domain.inquiry.dto.InquirySummaryRes; | ||
| import Gotcha.domain.inquiry.service.InquiryService; | ||
| import Gotcha.domain.user.entity.User; | ||
| import Gotcha.domain.user.service.UserService; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.Min; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/qnas") | ||
| @RequiredArgsConstructor | ||
| public class InquiryController implements InquiryApi { | ||
|
|
||
| private final InquiryService inquiryService; | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| @Override | ||
| @GetMapping | ||
| public ResponseEntity<?> getInquiries(@RequestParam(value = "keyword", required = false) String keyword, | ||
| @RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page, | ||
| @RequestParam(value = "sort", defaultValue = "DATE_DESC")InquirySortType sort, | ||
| @RequestParam(value = "isSolved", required = false) Boolean isSolved) { | ||
| Page<InquirySummaryRes> inquiries = inquiryService.getInquiries(keyword, page, sort, isSolved); | ||
|
|
||
| return ResponseEntity.status(HttpStatus.OK).body(inquiries); | ||
| } | ||
|
|
||
| @Override | ||
| @GetMapping("/mine") | ||
| public ResponseEntity<?> getMyInquiries(@RequestParam(value = "keyword", required = false) String keyword, | ||
| @RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page, | ||
| @RequestParam(value = "sort", defaultValue = "DATE_DESC") InquirySortType sort, | ||
| @RequestParam(value = "isSolved", required = false) Boolean isSolved, | ||
| @AuthenticationPrincipal SecurityUserDetails userDetails) { | ||
| Page<InquirySummaryRes> myInquiries = inquiryService.getMyInquiries(keyword, page, sort, isSolved, userDetails.getId()); | ||
|
|
||
| return ResponseEntity.status(HttpStatus.OK).body(myInquiries); | ||
| } | ||
|
|
||
| @Override | ||
| @GetMapping("/{inquiryId}") | ||
| public ResponseEntity<?> getInquiryById(@PathVariable(value = "inquiryId") Long inquiryId) { | ||
| InquiryRes inquiryRes = inquiryService.getInquiryById(inquiryId); | ||
| return ResponseEntity.status(HttpStatus.OK).body(inquiryRes); | ||
| } | ||
|
|
||
| @Override | ||
| @PostMapping | ||
| public ResponseEntity<?> createInquiry(@Valid @RequestBody InquiryReq inquiryReq, | ||
| @AuthenticationPrincipal SecurityUserDetails userDetails) { | ||
| User writer = userService.findUserByUserId(userDetails.getId()); | ||
| inquiryService.createInquiry(inquiryReq, writer); | ||
| return ResponseEntity.ok(SuccessRes.from("QnA 생성에 성공했습니다.")); | ||
| } | ||
|
|
||
| @Override | ||
| @PutMapping("/{inquiryId}") | ||
| public ResponseEntity<?> updateInquiry(@PathVariable(value = "inquiryId") Long inquiryId, | ||
| @Valid @RequestBody InquiryReq inquiryReq, | ||
| @AuthenticationPrincipal SecurityUserDetails userDetails) { | ||
| inquiryService.updateInquiry(inquiryReq, inquiryId, userDetails.getId()); | ||
| return ResponseEntity.ok(SuccessRes.from("QnA 수정에 성공했습니다.")); | ||
| } | ||
|
|
||
| @Override | ||
| @DeleteMapping("/{inquiryId}") | ||
| public ResponseEntity<?> deleteInquiry(@PathVariable(value = "inquiryId") Long inquiryId, | ||
| @AuthenticationPrincipal SecurityUserDetails userDetails) { | ||
| inquiryService.deleteInquiry(inquiryId, userDetails.getId()); | ||
| return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| package Gotcha.domain.inquiry.dto; | ||
|
|
||
| import Gotcha.domain.inquiry.entity.Answer; | ||
| import Gotcha.domain.user.entity.User; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public record AnswerReq( | ||
| @Schema(description = "답변 내용", example = "감사합니묘! 더 발전하는 갓챠가 되겠습니묘!") | ||
| @NotBlank(message = "답변 내용은 필수 입력 사항입니다.") | ||
| String content | ||
| ) { | ||
| public Answer toEntity(User writer){ | ||
| return Answer.builder(). | ||
| content(content) | ||
| .writer(writer) | ||
| .build(); | ||
| } | ||
| } | ||
This file contains hidden or 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 Gotcha.domain.inquiry.dto; | ||
|
|
||
| import Gotcha.domain.inquiry.entity.Answer; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record AnswerRes( | ||
| String writer, | ||
| String content, | ||
| LocalDateTime createdAt | ||
| ) { | ||
| public static AnswerRes fromEntity(Answer answer){ | ||
| if (answer == null) return null; | ||
| return new AnswerRes( | ||
| answer.getWriter().getNickname(), | ||
| answer.getContent(), | ||
| answer.getCreatedAt() | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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,33 @@ | ||
| package Gotcha.domain.inquiry.dto; | ||
|
|
||
| import Gotcha.domain.inquiry.entity.Inquiry; | ||
| import Gotcha.domain.user.entity.User; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public record InquiryReq( | ||
| @Schema(description = "QnA 제목", example = "게임이 너무 재밌는 것 같아요!") | ||
| @NotBlank(message = "제목은 필수 입력 사항입니다.") | ||
| String title, | ||
|
|
||
| @Schema(description = "QnA 질문 내용", example = "이 편지는 영국에서 최초로 시작되어 일년에 한바퀴를 돌면서 받는 사람에게 행운을 주었고 지금은 당신에게로 옮겨진 이 편지는 4일 안에 당신 곁을 ...더보기") | ||
| @NotBlank(message = "내용은 필수 입력 사항입니다.") | ||
| String content, | ||
|
|
||
| @Schema(description = "비공개 여부", example = "false", defaultValue = "false") | ||
| Boolean isPrivate | ||
| ) { | ||
| public InquiryReq { | ||
| if (isPrivate == null) { | ||
| isPrivate = false; // default value | ||
| } | ||
| } | ||
| public Inquiry toEntity(User writer){ | ||
| return Inquiry.builder(). | ||
| title(title). | ||
| content(content). | ||
| writer(writer). | ||
| isSecret(isPrivate). | ||
| build(); | ||
| } | ||
| } |
This file contains hidden or 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,29 @@ | ||
| package Gotcha.domain.inquiry.dto; | ||
|
|
||
| import Gotcha.domain.inquiry.entity.Inquiry; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record InquiryRes( | ||
| Long inquiryId, | ||
| String title, | ||
| String writer, | ||
| String content, | ||
| Boolean isPrivate, | ||
| LocalDateTime createdAt, | ||
| Boolean isSolved, | ||
| AnswerRes answer | ||
| ) { | ||
| public static InquiryRes fromEntity(Inquiry inquiry){ | ||
| return new InquiryRes( | ||
| inquiry.getId(), | ||
| inquiry.getTitle(), | ||
| inquiry.getWriter().getNickname(), | ||
| inquiry.getContent(), | ||
| inquiry.getIsSecret(), | ||
| inquiry.getCreatedAt(), | ||
| inquiry.getIsSolved(), | ||
| AnswerRes.fromEntity(inquiry.getAnswer()) | ||
| ); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/Gotcha/domain/inquiry/dto/InquirySortType.java
This file contains hidden or 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,21 @@ | ||
| package Gotcha.domain.inquiry.dto; | ||
|
|
||
| import org.springframework.data.domain.Sort; | ||
|
|
||
| public enum InquirySortType { | ||
| DATE_DESC("createdAt", Sort.Direction.DESC), | ||
| DATE_ASC("createdAt", Sort.Direction.ASC); | ||
|
|
||
| private final String type; | ||
| private final Sort.Direction direction; | ||
|
|
||
| InquirySortType(String type, Sort.Direction direction){ | ||
| this.type = type; | ||
| this.direction = direction; | ||
| } | ||
|
|
||
| public Sort getSort(){ | ||
| return Sort.by(direction, type); | ||
| } | ||
|
|
||
| } |
25 changes: 25 additions & 0 deletions
25
src/main/java/Gotcha/domain/inquiry/dto/InquirySummaryRes.java
This file contains hidden or 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,25 @@ | ||
| package Gotcha.domain.inquiry.dto; | ||
|
|
||
| import Gotcha.domain.inquiry.entity.Inquiry; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record InquirySummaryRes( | ||
| Long inquiryId, | ||
| String title, | ||
| String writer, | ||
| Boolean isPrivate, | ||
| LocalDateTime createdAt, | ||
| Boolean isSolved | ||
| ) { | ||
| public static InquirySummaryRes fromEntity(Inquiry inquiry){ | ||
| return new InquirySummaryRes( | ||
| inquiry.getId(), | ||
| inquiry.getTitle(), | ||
| inquiry.getWriter().getNickname(), | ||
| inquiry.getIsSecret(), | ||
| inquiry.getCreatedAt(), | ||
| inquiry.getIsSolved() | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 큰 부분은 아니지만 클라이언트가 요청에 사용하는 DTO에는
@Schema어노테이션으로 Swagger 문서를 잘 정의해두면 프론트 입장에서 훨씬 개발이 수월해진다고 합니다.백엔드 입장에서는 직관적으로 이해할 수 있을 거라 생각하지만 의외로 잘 파악하지 못하는 경우도 있는 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수정해두겠습니다! 공지사항 Req DTO에도
@Schema어노테이션 추가해두었습니다.