Skip to content

Commit

Permalink
Merge pull request #190 from Funssion-SWM/qna
Browse files Browse the repository at this point in the history
Qna
  • Loading branch information
goathoon authored Nov 18, 2023
2 parents 8295611 + 5f744c8 commit 92ac09d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import Funssion.Inforum.common.dto.IsSuccessResponseDto;
import Funssion.Inforum.common.exception.etc.UnAuthorizedException;
import Funssion.Inforum.common.utils.SecurityContextUtils;
import Funssion.Inforum.domain.post.memo.dto.response.MemoListDto;
import Funssion.Inforum.domain.post.qna.Constant;
import Funssion.Inforum.domain.post.qna.domain.Question;
import Funssion.Inforum.domain.post.qna.dto.request.QuestionSaveDto;
import Funssion.Inforum.domain.post.qna.dto.response.CreateQuestionDto;
import Funssion.Inforum.domain.post.qna.dto.response.QuestionDto;
import Funssion.Inforum.domain.post.qna.service.QuestionService;
import Funssion.Inforum.domain.post.utils.AuthUtils;
Expand All @@ -33,10 +33,9 @@ public class QuestionController {
private final QuestionService questionService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public IsSuccessResponseDto createQuestion(@RequestBody @Validated QuestionSaveDto questionSaveDto,@RequestParam(required = false, defaultValue=Constant.NONE_MEMO_QUESTION) Long memoId){
public CreateQuestionDto createQuestion(@RequestBody @Validated QuestionSaveDto questionSaveDto,@RequestParam(required = false, defaultValue=Constant.NONE_MEMO_QUESTION) Long memoId){
Long authorId = AuthUtils.getUserId(CRUDType.CREATE);
questionService.createQuestion(questionSaveDto, authorId, memoId);
return new IsSuccessResponseDto(true,"성공적으로 질문이 등록되었습니다.");
return new CreateQuestionDto(questionService.createQuestion(questionSaveDto, authorId, memoId).getId(),"성공적으로 질문이 등록 되었습니다." );
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Funssion.Inforum.domain.post.qna.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CreateQuestionDto {
private Long questionId;
private String message;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package Funssion.Inforum.domain.notification;

import Funssion.Inforum.common.constant.NotificationType;
import Funssion.Inforum.common.constant.OrderType;
import Funssion.Inforum.common.constant.PostType;
import Funssion.Inforum.domain.follow.repository.FollowRepository;
import Funssion.Inforum.domain.member.constant.LoginType;
import Funssion.Inforum.domain.member.dto.response.SaveMemberResponseDto;
import Funssion.Inforum.domain.member.entity.Member;
import Funssion.Inforum.domain.member.entity.NonSocialMember;
import Funssion.Inforum.domain.member.entity.SocialMember;
import Funssion.Inforum.domain.member.repository.MemberRepository;
Expand All @@ -17,13 +12,10 @@
import Funssion.Inforum.domain.post.comment.repository.CommentRepository;
import Funssion.Inforum.domain.post.memo.domain.Memo;
import Funssion.Inforum.domain.post.memo.repository.MemoRepository;
import Funssion.Inforum.domain.post.qna.Constant;
import Funssion.Inforum.domain.post.qna.domain.Answer;
import Funssion.Inforum.domain.post.qna.domain.Question;
import Funssion.Inforum.domain.post.qna.repository.AnswerRepository;
import Funssion.Inforum.domain.post.qna.repository.QuestionRepository;
import Funssion.Inforum.domain.score.Rank;
import org.assertj.core.api.Assertions;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -34,24 +26,22 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;

import static Funssion.Inforum.common.constant.NotificationType.*;
import static Funssion.Inforum.common.constant.OrderType.*;
import static Funssion.Inforum.common.constant.OrderType.NEW;
import static Funssion.Inforum.common.constant.PostType.*;
import static Funssion.Inforum.domain.post.qna.Constant.*;
import static Funssion.Inforum.domain.score.Rank.*;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@Transactional
Expand Down Expand Up @@ -672,8 +662,7 @@ void newQuestionInMemo() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.param("memoId", createdMemo.getId().toString())
.with(user(testUser2Id.toString())))
.andExpect(status().isCreated())
.andExpect(content().string(containsString("\"isSuccess\":true")));
.andExpect(status().isCreated());

Question savedQuestion = questionRepository.getQuestionsOfMemo(testUser2Id, createdMemo.getId()).get(0);
assertThat(savedQuestion.getMemoId()).isEqualTo(createdMemo.getId());
Expand Down Expand Up @@ -891,8 +880,7 @@ void manyFollowingUserWriteNewQuestion() throws Exception {
.content(questionSaveForm)
.contentType(MediaType.APPLICATION_JSON)
.with(user(testUser1Id.toString())))
.andExpect(status().isCreated())
.andExpect(content().string(containsString("\"isSuccess\":true")));
.andExpect(status().isCreated());

Question savedQuestion = questionRepository.getMyQuestions(testUser1Id, NEW, DEFAULT_PAGE_NUM, DEFAULT_RESULT_SIZE_PER_PAGE).get(0);
assertThat(savedQuestion.getMemoId()).isEqualTo(Long.valueOf(NONE_MEMO_QUESTION));
Expand Down Expand Up @@ -999,8 +987,7 @@ void followingUserWriteNewQuestionInMyMemo() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.param("memoId", createdMemo.getId().toString())
.with(user(testUser2Id.toString())))
.andExpect(status().isCreated())
.andExpect(content().string(containsString("\"isSuccess\":true")));
.andExpect(status().isCreated());

Question savedQuestion = questionRepository.getMyQuestions(testUser2Id, NEW, DEFAULT_PAGE_NUM, DEFAULT_RESULT_SIZE_PER_PAGE).get(0);
assertThat(savedQuestion.getMemoId()).isEqualTo(createdMemo.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Funssion.Inforum.domain.post.qna.controller;

import Funssion.Inforum.common.utils.SecurityContextUtils;
import Funssion.Inforum.domain.post.qna.domain.Question;
import Funssion.Inforum.domain.post.qna.dto.request.QuestionSaveDto;
import Funssion.Inforum.domain.post.qna.dto.response.QuestionDto;
import Funssion.Inforum.domain.post.qna.service.QuestionService;
Expand All @@ -20,6 +21,7 @@
import java.time.LocalDateTime;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
Expand Down Expand Up @@ -69,6 +71,8 @@ class createQuestion{
@WithMockUser(username = AUTHORIZED_USER)
@DisplayName("로그인한 유저가 올바른 질문을 생성")
void createQuestionByAuthUser() throws Exception {
when(questionService.createQuestion(any(QuestionSaveDto.class),any(Long.class),any(Long.class)))
.thenReturn(Question.builder().id(1L).build());
mvc.perform(post("/questions")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -79,6 +83,8 @@ void createQuestionByAuthUser() throws Exception {
@WithMockUser(username = AUTHORIZED_USER)
@DisplayName("로그인한 유저가 특정 메모랑 연관된 올바른 질문을 생성")
void createQuestionInMemoByAuthUser() throws Exception {
when(questionService.createQuestion(any(QuestionSaveDto.class),any(Long.class),any(Long.class)))
.thenReturn(Question.builder().id(1L).build());
mvc.perform(post("/questions")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -102,6 +108,8 @@ void createInvalidQuestionByAuthUser() throws Exception {
@WithMockUser(username = SecurityContextUtils.ANONYMOUS_USER_ID_STRING)
@DisplayName("로그인하지 않은 유저가 올바르지 않은 질문을 생성")
void createQuestionByNonAuthUser() throws Exception {
when(questionService.createQuestion(any(QuestionSaveDto.class),any(Long.class),any(Long.class)))
.thenReturn(Question.builder().id(1L).build());
mvc.perform(post("/questions")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit 92ac09d

Please sign in to comment.