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] 질문글 생성 API #64

Merged
merged 2 commits into from
Jul 24, 2022
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,4 +29,9 @@ public ApiResponse<AnnouncementCreateResponse> createAnnouncement(@Valid @Reques
public ApiResponse<PostResponse> createAnnouncementPost(@RequestBody VotePostCreateRequest request) {
return ApiResponse.ok(postApiService.createPost(request));
}

@PostMapping("/question")
public ApiResponse<PostResponse> createQuestionPost(@Valid @RequestBody QuestionPostCreateRequest request) {
return ApiResponse.ok(postApiService.createQuestionPost(request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void create_vote_post() throws Exception {
}

@Test
@DisplayName("둥지 - 공지 생성 API")
@DisplayName("둥지 - 공지글 생성 API")
void announcement_create_test() throws Exception {

String token = "[유저의 JWT]";
Expand Down Expand Up @@ -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()
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()));
}

}