Skip to content

Commit

Permalink
Merge pull request #21 from Team-Umbba/feat/#19-answer_today_question
Browse files Browse the repository at this point in the history
[FEAT] 일일문답 페이지 질문 답변 API
  • Loading branch information
ddongseop authored Jul 10, 2023
2 parents 76254fc + bd693c3 commit e82fd08
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import sopt.org.umbbaServer.domain.qna.controller.dto.request.TodayAnswerRequestDto;
import sopt.org.umbbaServer.domain.qna.controller.dto.response.TodayQnAResponseDto;
import sopt.org.umbbaServer.domain.qna.service.QnAService;
import sopt.org.umbbaServer.global.common.dto.ApiResponse;
import sopt.org.umbbaServer.global.config.jwt.JwtProvider;
import sopt.org.umbbaServer.global.exception.SuccessType;

import javax.validation.Valid;
import java.security.Principal;

@RestController
Expand All @@ -20,7 +21,7 @@ public class QnAController {

@GetMapping("/qna")
@ResponseStatus(HttpStatus.OK)
public ApiResponse getTodayQna(Principal principal) {
public ApiResponse<TodayQnAResponseDto> getTodayQna(Principal principal) {

return ApiResponse.success(SuccessType.GET_TODAY_QNA_SUCCESS, qnAService.getTodayQnA(JwtProvider.getUserFromPrincial(principal)));
}
Expand All @@ -32,4 +33,15 @@ public ApiResponse dummy() {

return ApiResponse.success(SuccessType.GET_TODAY_QNA_SUCCESS);
}

@PostMapping("/qna")
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse answerTodayQuestion(
Principal principal,
@Valid @RequestBody TodayAnswerRequestDto request) {

qnAService.answerTodayQuestion(request, JwtProvider.getUserFromPrincial(principal));

return ApiResponse.success(SuccessType.ANSWER_TODAY_QUESTION_SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sopt.org.umbbaServer.domain.qna.controller.dto.request;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;

@Getter
public class TodayAnswerRequestDto {

@NotBlank // null, "", " "을 모두 허용하지 X
String answer;
}
10 changes: 10 additions & 0 deletions src/main/java/sopt/org/umbbaServer/domain/qna/model/QnA.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ public boolean isParentAnswer() {
public boolean isChildAnswer() {
return isChildAnswer;
}

public void saveParentAnswer(String answer) {
this.parentAnswer = answer;
this.isParentAnswer = true;
}

public void saveChildAnswer(String answer) {
this.childAnswer = answer;
this.isChildAnswer = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.transaction.annotation.Transactional;
import sopt.org.umbbaServer.domain.parentchild.model.Parentchild;
import sopt.org.umbbaServer.domain.parentchild.repository.ParentchildRepository;
import sopt.org.umbbaServer.domain.qna.controller.dto.request.TodayAnswerRequestDto;
import sopt.org.umbbaServer.domain.qna.controller.dto.response.TodayQnAResponseDto;
import sopt.org.umbbaServer.domain.qna.model.QnA;
import sopt.org.umbbaServer.domain.qna.model.Question;
Expand Down Expand Up @@ -41,6 +42,22 @@ public TodayQnAResponseDto getTodayQnA(Long userId) {
return TodayQnAResponseDto.of(myUser, opponentUser, todayQnA, todayQuestion, isMeChild);
}

@Transactional
public void answerTodayQuestion(TodayAnswerRequestDto request, Long userId) {
User myUser = getUserById(userId);
Parentchild parentchild = getParentchildByUser(myUser);
QnA todayQnA = getQnAByParentchild(parentchild);
User opponentUser = getOpponentByParentchild(parentchild, userId);

boolean isMeChild = myUser.getBornYear() >= opponentUser.getBornYear();

if (isMeChild) {
todayQnA.saveChildAnswer(request.getAnswer());
} else {
todayQnA.saveParentAnswer(request.getAnswer());
}
}

@Transactional
public void createQnA() {
QnA newQnA = QnA.builder()
Expand All @@ -56,6 +73,8 @@ public void createQnA() {
parentchild.addQnA(newQnA);
}

// 리팩토링을 위해 아래로 뺀 메서드들

private User getUserById(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorType.INVALID_USER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum SuccessType {
* 201 CREATED
*/
CREATE_PARENT_CHILD_SUCCESS(HttpStatus.CREATED, "온보딩 정보를 입력받아 부모자식 관계를 생성하는 데 성공했습니다."),

ANSWER_TODAY_QUESTION_SUCCESS(HttpStatus.CREATED, "오늘의 일일문답에 답변을 완료하였습니다."),

;

Expand Down

0 comments on commit e82fd08

Please sign in to comment.