From d08d54f4bf9ccf979f80347ce19413ec0417d363 Mon Sep 17 00:00:00 2001 From: heoboseong7 Date: Sun, 24 Jul 2022 18:07:12 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[Feat]=20=EC=A7=88=EB=AC=B8=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ccc/keeweapi/api/nest/PostController.java | 10 ++++++---- .../java/ccc/keeweapi/dto/nest/PostAssembler.java | 5 +++++ .../keeweapi/dto/nest/QuestionPostCreateRequest.java | 11 +++++++++++ .../ccc/keeweapi/service/nest/PostApiService.java | 6 ++++++ .../ccc/keewedomain/domain/nest/QuestionPost.java | 8 ++++++++ .../ccc/keewedomain/dto/nest/QuestionPostDto.java | 12 ++++++++++++ .../keewedomain/service/nest/PostDomainService.java | 11 +++++++---- 7 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 keewe-api/src/main/java/ccc/keeweapi/dto/nest/QuestionPostCreateRequest.java create mode 100644 keewe-domain/src/main/java/ccc/keewedomain/dto/nest/QuestionPostDto.java diff --git a/keewe-api/src/main/java/ccc/keeweapi/api/nest/PostController.java b/keewe-api/src/main/java/ccc/keeweapi/api/nest/PostController.java index 9f53f0b8..acd59fd1 100644 --- a/keewe-api/src/main/java/ccc/keeweapi/api/nest/PostController.java +++ b/keewe-api/src/main/java/ccc/keeweapi/api/nest/PostController.java @@ -1,10 +1,7 @@ package ccc.keeweapi.api.nest; import ccc.keeweapi.dto.ApiResponse; -import ccc.keeweapi.dto.nest.AnnouncementCreateRequest; -import ccc.keeweapi.dto.nest.AnnouncementCreateResponse; -import ccc.keeweapi.dto.nest.PostResponse; -import ccc.keeweapi.dto.nest.VotePostCreateRequest; +import ccc.keeweapi.dto.nest.*; import ccc.keeweapi.service.nest.PostApiService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.PostMapping; @@ -32,4 +29,9 @@ public ApiResponse createAnnouncement(@Valid @Reques public ApiResponse createAnnouncementPost(@RequestBody VotePostCreateRequest request) { return ApiResponse.ok(postApiService.createPost(request)); } + + @PostMapping("/question") + public ApiResponse createQuestionPost(@Valid @RequestBody QuestionPostCreateRequest request) { + return ApiResponse.ok(postApiService.createQuestionPost(request)); + } } diff --git a/keewe-api/src/main/java/ccc/keeweapi/dto/nest/PostAssembler.java b/keewe-api/src/main/java/ccc/keeweapi/dto/nest/PostAssembler.java index e7ca2f26..5aab8ed1 100644 --- a/keewe-api/src/main/java/ccc/keeweapi/dto/nest/PostAssembler.java +++ b/keewe-api/src/main/java/ccc/keeweapi/dto/nest/PostAssembler.java @@ -3,6 +3,7 @@ import ccc.keeweapi.utils.SecurityUtil; import ccc.keewedomain.domain.nest.AnnouncementPost; import ccc.keewedomain.dto.nest.AnnouncementCreateDto; +import ccc.keewedomain.dto.nest.QuestionPostDto; import ccc.keewedomain.dto.nest.VotePostDto; import org.springframework.stereotype.Component; @@ -23,4 +24,8 @@ public VotePostDto toVotePostDto(VotePostCreateRequest request) { public PostResponse toPostResponse(Long postId) { return new PostResponse(postId); } + + public QuestionPostDto toQuestionCreateDto(QuestionPostCreateRequest request) { + return QuestionPostDto.of(request.getProfileId(), SecurityUtil.getUserId(), request.getContent()); + } } diff --git a/keewe-api/src/main/java/ccc/keeweapi/dto/nest/QuestionPostCreateRequest.java b/keewe-api/src/main/java/ccc/keeweapi/dto/nest/QuestionPostCreateRequest.java new file mode 100644 index 00000000..7c120a14 --- /dev/null +++ b/keewe-api/src/main/java/ccc/keeweapi/dto/nest/QuestionPostCreateRequest.java @@ -0,0 +1,11 @@ +package ccc.keeweapi.dto.nest; + +import ccc.keeweapi.validator.nest.PostContent; +import lombok.Data; + +@Data +public class QuestionPostCreateRequest { + private Long profileId; + @PostContent + private String content; +} diff --git a/keewe-api/src/main/java/ccc/keeweapi/service/nest/PostApiService.java b/keewe-api/src/main/java/ccc/keeweapi/service/nest/PostApiService.java index baf85b8d..b9035750 100644 --- a/keewe-api/src/main/java/ccc/keeweapi/service/nest/PostApiService.java +++ b/keewe-api/src/main/java/ccc/keeweapi/service/nest/PostApiService.java @@ -25,4 +25,10 @@ public AnnouncementCreateResponse createAnnouncementPost(AnnouncementCreateReque Long postId = postDomainService.createAnnouncementPost(postAssembler.toAnnouncementCreateDto(request)); return postAssembler.toAnnouncementCreateResponse(postId); } + + @Transactional + public PostResponse createQuestionPost(QuestionPostCreateRequest request) { + Long postId = postDomainService.createQuestionPost(postAssembler.toQuestionCreateDto(request)); + return postAssembler.toPostResponse(postId); + } } diff --git a/keewe-domain/src/main/java/ccc/keewedomain/domain/nest/QuestionPost.java b/keewe-domain/src/main/java/ccc/keewedomain/domain/nest/QuestionPost.java index cba7bef2..77751b2f 100644 --- a/keewe-domain/src/main/java/ccc/keewedomain/domain/nest/QuestionPost.java +++ b/keewe-domain/src/main/java/ccc/keewedomain/domain/nest/QuestionPost.java @@ -8,4 +8,12 @@ @Entity @DiscriminatorValue("QUESTION") public class QuestionPost extends Post { + + protected QuestionPost(Nest nest, Profile writer, String content) { + super(nest, writer, content); + } + + public static QuestionPost of(Profile profile, String content) { + return new QuestionPost(profile.getNest(), profile, content); + } } diff --git a/keewe-domain/src/main/java/ccc/keewedomain/dto/nest/QuestionPostDto.java b/keewe-domain/src/main/java/ccc/keewedomain/dto/nest/QuestionPostDto.java new file mode 100644 index 00000000..bd7bc337 --- /dev/null +++ b/keewe-domain/src/main/java/ccc/keewedomain/dto/nest/QuestionPostDto.java @@ -0,0 +1,12 @@ +package ccc.keewedomain.dto.nest; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor(staticName = "of") +public class QuestionPostDto { + private Long profileId; + private Long userId; + private String content; +} diff --git a/keewe-domain/src/main/java/ccc/keewedomain/service/nest/PostDomainService.java b/keewe-domain/src/main/java/ccc/keewedomain/service/nest/PostDomainService.java index 0e85889b..489858f4 100644 --- a/keewe-domain/src/main/java/ccc/keewedomain/service/nest/PostDomainService.java +++ b/keewe-domain/src/main/java/ccc/keewedomain/service/nest/PostDomainService.java @@ -1,10 +1,8 @@ package ccc.keewedomain.service.nest; -import ccc.keewedomain.domain.nest.AnnouncementPost; -import ccc.keewedomain.domain.nest.Candidate; -import ccc.keewedomain.domain.nest.Post; -import ccc.keewedomain.domain.nest.VotePost; +import ccc.keewedomain.domain.nest.*; import ccc.keewedomain.domain.user.Profile; +import ccc.keewedomain.dto.nest.QuestionPostDto; import ccc.keewedomain.dto.nest.VotePostDto; import ccc.keewedomain.dto.nest.AnnouncementCreateDto; import ccc.keewedomain.repository.nest.PostRepository; @@ -49,4 +47,9 @@ public Long createAnnouncementPost(AnnouncementCreateDto dto) { return this.save(AnnouncementPost.of(profile, dto.getContent())); } + public Long createQuestionPost(QuestionPostDto dto) { + Profile profile = profileDomainService.getAndVerifyOwnerOrElseThrow(dto.getProfileId(), dto.getUserId()); + return this.save(QuestionPost.of(profile, dto.getContent())); + } + } From 2f91f5a8f98c76cdf9020748d3b8f91b589c79e8 Mon Sep 17 00:00:00 2001 From: heoboseong7 Date: Sun, 24 Jul 2022 18:21:50 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[Test]=20=EC=A7=88=EB=AC=B8=EA=B8=80=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20API=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../keeweapi/api/nest/PostControllerTest.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/keewe-api/src/test/java/ccc/keeweapi/api/nest/PostControllerTest.java b/keewe-api/src/test/java/ccc/keeweapi/api/nest/PostControllerTest.java index 485f2374..c9239086 100644 --- a/keewe-api/src/test/java/ccc/keeweapi/api/nest/PostControllerTest.java +++ b/keewe-api/src/test/java/ccc/keeweapi/api/nest/PostControllerTest.java @@ -75,7 +75,7 @@ void create_vote_post() throws Exception { } @Test - @DisplayName("둥지 - 공지 생성 API") + @DisplayName("둥지 - 공지글 생성 API") void announcement_create_test() throws Exception { String token = "[유저의 JWT]"; @@ -111,4 +111,42 @@ void announcement_create_test() throws Exception { .build() ))); } + + @Test + @DisplayName("둥지 - 질문글 생성 API") + void question_create_test() throws Exception { + + String token = "[유저의 JWT]"; + AnnouncementCreateRequest request = new AnnouncementCreateRequest(); + request.setProfileId(1L); + request.setContent("봄날의 햇살 유승훈."); + + when(postApiService.createQuestionPost(any())) + .thenReturn(new PostResponse(1L)); + + mockMvc.perform( + post("/api/v1/nest/question") + .header(HttpHeaders.AUTHORIZATION, "Bearer " + token) + .content(objectMapper.writeValueAsString(request)) + .contentType(MediaType.APPLICATION_JSON) + ).andExpect(status().isOk()) + .andDo(restDocs.document( + resource( + ResourceSnippetParameters.builder() + .description("질문글 생성 API 입니다.") + .summary("질문글 생성 API 입니다.") + .requestHeaders( + headerWithName("Authorization").description("유저의 JWT")) + .requestFields( + fieldWithPath("profileId").description("대상 프로필의 id"), + fieldWithPath("content").description("글의 내용")) + .responseFields( + fieldWithPath("message").description("요청 결과 메세지"), + fieldWithPath("code").description("결과 코드"), + fieldWithPath("data.postId").description("작성된 질문글의 ID") + ) + .tag("Nest") + .build() + ))); + } }