Skip to content

Commit

Permalink
feat: 질문 추가 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JaeHongDev committed Jul 6, 2024
1 parent 101d04c commit d6d1acb
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package in.backend.core.question.application;

import in.backend.global.entity.ActionType;

public record QuestionSaveCommand(
ActionType action,
Long questionId,
Long questionSetId,
Integer sequence,
String question
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package in.backend.core.question.application;


import in.backend.core.question.application.QuestionWriter.QuestionInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class QuestionService {
private final QuestionWriter questionWriter;

public Long save(QuestionSaveCommand command) {
return switch (command.action()) {
case CREATE -> questionWriter.write(new QuestionInfo(command));
case UPDATE -> questionWriter.write(command.questionId(), new QuestionInfo(command));
case DELETE -> throw new UnsupportedOperationException();
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ public class QuestionWriter {
private final QuestionReader questionReader;


public Long execute(QuestionInfo questionInfo) {
return questionRepository.save(questionInfo.toEntity()).getId();
public Long write(QuestionInfo questionInfo) {
return questionRepository.save(questionInfo.toEntity())
.getId();
}

public void update(Long questionId, QuestionInfo questionInfo) {
public Long write(Long questionId, QuestionInfo questionInfo) {
var question = questionReader.read(questionId);

question.update(questionInfo);

return question.getId();
}

public record QuestionInfo(
String content,
String referenceLinks,
int sequence
) {

public QuestionInfo(QuestionSaveCommand command) {
this(command.question(), command.sequence());
}

public QuestionEntity toEntity() {
return QuestionEntity.builder()
.content(content)
.referenceLinks(referenceLinks)
.sequence(sequence)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public QuestionEntity(

public void update(QuestionInfo questionInfo) {
applyIfPresent(questionInfo.content(), value -> this.content = value);
applyIfPresent(questionInfo.referenceLinks(), value -> this.referenceLinks = value);
applyIfPresent(questionInfo.sequence(), value -> this.sequence = value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package in.backend.core.question.presentation;


import in.backend.core.auth.domain.Visitor;
import in.backend.core.auth.domain.attributes.Auth;
import in.backend.core.question.presentation.payload.QuestionSaveRequest;
import lombok.RequiredArgsConstructor;
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;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/questions")
public class QuestionApi {


@PostMapping
public void save(
@RequestBody QuestionSaveRequest questionSaveRequest,
@Auth Visitor visitor
) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package in.backend.core.question.presentation.payload;

public record QuestionDetailResponse(
Long questionId,
Long questionSetId,
String question,
Integer sequence
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package in.backend.core.question.presentation.payload;

import in.backend.global.entity.ActionType;

public record QuestionSaveRequest(
ActionType action,
Long questionId,
Long questionSetId,
Integer sequence,
String question
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package in.backend.global.entity;

public enum ActionType {
CREATE,
UPDATE,
DELETE
}

0 comments on commit d6d1acb

Please sign in to comment.