-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add: dto 추가 * feat: 회고 작성 로직 구현 * feat: 변환 로직 구현 * feat: 질문 관련 일급컬렉션 구현 * add: 예외 추가 * del: 컨버터 삭제 * del: 컨버터 삭제 * chore: Answers 로직 수정 * del: 컨버터 삭제 * chore: 예외 추가 * chore: 검증로직 추가
- Loading branch information
Showing
23 changed files
with
316 additions
and
37 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
layer-api/src/main/java/org/layer/domain/answer/controller/AnswerApi.java
This file contains 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 org.layer.domain.answer.controller; | ||
|
||
import org.layer.common.annotation.MemberId; | ||
import org.layer.domain.answer.controller.dto.request.AnswerListCreateRequest; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
|
||
@Tag(name = "회고 작성", description = "회고 작성 관련 API") | ||
public interface AnswerApi { | ||
|
||
@Operation(summary = "회고 작성", description = "") | ||
ResponseEntity<Void> createAnswer(@PathVariable("spaceId") Long spaceId, @PathVariable("retrospectId") Long retrospectId, | ||
@RequestBody @Valid AnswerListCreateRequest request, @MemberId Long memberId); | ||
} |
43 changes: 43 additions & 0 deletions
43
layer-api/src/main/java/org/layer/domain/answer/controller/AnswerController.java
This file contains 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,43 @@ | ||
package org.layer.domain.answer.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.layer.common.annotation.MemberId; | ||
import org.layer.domain.answer.controller.dto.request.AnswerListCreateRequest; | ||
import org.layer.domain.answer.service.AnswerService; | ||
import org.layer.domain.answer.service.dto.request.AnswerCreateServiceRequest; | ||
import org.layer.domain.answer.service.dto.request.AnswerListCreateServiceRequest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/space/{spaceId}/retrospect/{retrospectId}/answer") | ||
public class AnswerController implements AnswerApi { | ||
private final AnswerService answerService; | ||
|
||
@Override | ||
@PostMapping | ||
@PreAuthorize("isAuthenticated()") | ||
public ResponseEntity<Void> createAnswer(@PathVariable("spaceId") Long spaceId, | ||
@PathVariable("retrospectId") Long retrospectId, | ||
@RequestBody @Valid AnswerListCreateRequest request, @MemberId Long memberId) { | ||
|
||
List<AnswerCreateServiceRequest> requests = request.requests().stream() | ||
.map(r -> AnswerCreateServiceRequest.of(r.questionId(), r.questionType(), r.answer())) | ||
.toList(); | ||
|
||
answerService.create(AnswerListCreateServiceRequest.of(requests), spaceId, retrospectId, memberId); | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...api/src/main/java/org/layer/domain/answer/controller/dto/request/AnswerCreateRequest.java
This file contains 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,9 @@ | ||
package org.layer.domain.answer.controller.dto.request; | ||
|
||
public record AnswerCreateRequest( | ||
Long questionId, | ||
String questionType, | ||
String answer | ||
|
||
) { | ||
} |
8 changes: 8 additions & 0 deletions
8
...src/main/java/org/layer/domain/answer/controller/dto/request/AnswerListCreateRequest.java
This file contains 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,8 @@ | ||
package org.layer.domain.answer.controller.dto.request; | ||
|
||
import java.util.List; | ||
|
||
public record AnswerListCreateRequest( | ||
List<AnswerCreateRequest> requests | ||
) { | ||
} |
69 changes: 69 additions & 0 deletions
69
layer-api/src/main/java/org/layer/domain/answer/service/AnswerService.java
This file contains 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,69 @@ | ||
package org.layer.domain.answer.service; | ||
|
||
import static org.layer.common.exception.MemberSpaceRelationExceptionType.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.layer.domain.answer.entity.Answer; | ||
import org.layer.domain.answer.entity.Answers; | ||
import org.layer.domain.answer.repository.AnswerRepository; | ||
import org.layer.domain.answer.service.dto.request.AnswerCreateServiceRequest; | ||
import org.layer.domain.answer.service.dto.request.AnswerListCreateServiceRequest; | ||
import org.layer.domain.question.entity.Questions; | ||
import org.layer.domain.question.enums.QuestionType; | ||
import org.layer.domain.question.repository.QuestionRepository; | ||
import org.layer.domain.retrospect.repository.RetrospectRepository; | ||
import org.layer.domain.space.entity.MemberSpaceRelation; | ||
import org.layer.domain.space.exception.MemberSpaceRelationException; | ||
import org.layer.domain.space.repository.MemberSpaceRelationRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class AnswerService { | ||
private final AnswerRepository answerRepository; | ||
private final MemberSpaceRelationRepository memberSpaceRelationRepository; | ||
private final RetrospectRepository retrospectRepository; | ||
private final QuestionRepository questionRepository; | ||
|
||
public void create(AnswerListCreateServiceRequest request, Long spaceId, Long retrospectId, Long memberId) { | ||
// 스페이스 팀원인지 검증 | ||
Optional<MemberSpaceRelation> team = memberSpaceRelationRepository.findBySpaceIdAndMemberId( | ||
spaceId, memberId); | ||
if (team.isEmpty()) { | ||
throw new MemberSpaceRelationException(NOT_FOUND_MEMBER_SPACE_RELATION); | ||
} | ||
|
||
// 회고 존재 검증 | ||
retrospectRepository.findByIdOrThrow(retrospectId); | ||
|
||
// 회고 질문 유효성 검사 - 해당 회고에 속해있는 질문인지 | ||
List<Long> questionIds = request.requests().stream() | ||
.map(AnswerCreateServiceRequest::questionId) | ||
.toList(); | ||
Questions questions = new Questions(questionRepository.findAllByIdIn(questionIds)); | ||
|
||
questions.validateQuestionSize(questionIds.size()); | ||
|
||
// 회고 질문 유효성 검사 - 이미 응답을 하지 않았는지 | ||
Answers answers = new Answers( | ||
answerRepository.findByRetrospectIdAndMemberIdAndQuestionIdIn(retrospectId, memberId, questionIds)); | ||
answers.validateNoAnswer(); | ||
|
||
request.requests().forEach( | ||
r -> { | ||
// 회고 질문 유효성 검사 - 각각의 질문들이 유효한지 | ||
questions.validateIdAndQuestionType(r.questionId(), QuestionType.stringToEnum(r.questionType())); | ||
Answer answer = new Answer(retrospectId, r.questionId(), memberId, r.answer()); | ||
answerRepository.save(answer); | ||
}); | ||
|
||
|
||
|
||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...src/main/java/org/layer/domain/answer/service/dto/request/AnswerCreateServiceRequest.java
This file contains 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,11 @@ | ||
package org.layer.domain.answer.service.dto.request; | ||
|
||
public record AnswerCreateServiceRequest( | ||
Long questionId, | ||
String questionType, | ||
String answer | ||
) { | ||
public static AnswerCreateServiceRequest of(Long questionId, String questionType, String answer){ | ||
return new AnswerCreateServiceRequest(questionId, questionType, answer); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...main/java/org/layer/domain/answer/service/dto/request/AnswerListCreateServiceRequest.java
This file contains 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,11 @@ | ||
package org.layer.domain.answer.service.dto.request; | ||
|
||
import java.util.List; | ||
|
||
public record AnswerListCreateServiceRequest( | ||
List<AnswerCreateServiceRequest> requests | ||
) { | ||
public static AnswerListCreateServiceRequest of(List<AnswerCreateServiceRequest> requests){ | ||
return new AnswerListCreateServiceRequest(requests); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
23 changes: 23 additions & 0 deletions
23
layer-common/src/main/java/org/layer/common/exception/AnswerExceptionType.java
This file contains 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,23 @@ | ||
package org.layer.common.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public enum AnswerExceptionType implements ExceptionType{ | ||
ALREADY_ANSWERED(HttpStatus.BAD_REQUEST, "이미 응답한 질문이 있습니다."); | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
layer-common/src/main/java/org/layer/common/exception/QuestionExceptionType.java
This file contains 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 org.layer.common.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public enum QuestionExceptionType implements ExceptionType{ | ||
INVALID_QUESTION(HttpStatus.BAD_REQUEST, "유효하지 않은 질문 입니다."), | ||
INVALID_QUESTION_TYPE(HttpStatus.BAD_REQUEST, "유효하지 않은 질문 타입입니다."), | ||
INVALID_QUESTION_SIZE(HttpStatus.BAD_REQUEST, "요청한 질문 응답 수가 유효하지 않습니다."); | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
} |
This file contains 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
21 changes: 18 additions & 3 deletions
21
layer-domain/src/main/java/org/layer/domain/answer/entity/Answers.java
This file contains 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 |
---|---|---|
@@ -1,19 +1,34 @@ | ||
package org.layer.domain.answer.entity; | ||
|
||
import static org.layer.common.exception.AnswerExceptionType.*; | ||
|
||
import java.util.List; | ||
|
||
import org.layer.domain.answer.exception.AnswerException; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class Answers { | ||
private static final int ZERO = 0; | ||
|
||
private final List<Answer> answers; | ||
|
||
public boolean hasRetrospectAnswer(Long memberId) { | ||
public boolean hasRetrospectAnswer(Long memberId, Long retrospectId) { | ||
return answers.stream() | ||
.filter(answer -> answer.getRetrospectId().equals(retrospectId)) | ||
.anyMatch(answer -> answer.getMemberId().equals(memberId)); | ||
} | ||
|
||
public int getWriteCount() { | ||
return answers.size(); | ||
public long getWriteCount(Long retrospectId) { | ||
return answers.stream() | ||
.filter(answer -> answer.getRetrospectId().equals(retrospectId)) | ||
.count(); | ||
} | ||
|
||
public void validateNoAnswer(){ | ||
if(answers.size() != ZERO){ | ||
throw new AnswerException(ALREADY_ANSWERED); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
layer-domain/src/main/java/org/layer/domain/answer/exception/AnswerException.java
This file contains 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,10 @@ | ||
package org.layer.domain.answer.exception; | ||
|
||
import org.layer.common.exception.BaseCustomException; | ||
import org.layer.common.exception.ExceptionType; | ||
|
||
public class AnswerException extends BaseCustomException { | ||
public AnswerException(ExceptionType exceptionType) { | ||
super(exceptionType); | ||
} | ||
} |
This file contains 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
25 changes: 0 additions & 25 deletions
25
layer-domain/src/main/java/org/layer/domain/question/converter/QuestionTypeConverter.java
This file was deleted.
Oops, something went wrong.
This file contains 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.