diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/application/BaseQuestionService.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/application/BaseQuestionService.java new file mode 100644 index 0000000..411d38a --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/application/BaseQuestionService.java @@ -0,0 +1,33 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.application; + +import goorm.kgu.familynote.domain.question.baseQuestion.domain.BaseQuestion; +import goorm.kgu.familynote.domain.question.baseQuestion.domain.BaseQuestionRepository; +import goorm.kgu.familynote.domain.question.baseQuestion.presentation.request.BaseQuestionCreateRequest; +import goorm.kgu.familynote.domain.question.baseQuestion.presentation.response.BaseQuestionResponse; +import goorm.kgu.familynote.domain.question.baseQuestion.presentation.response.BaseQuestionResponseList; +import java.util.List; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class BaseQuestionService { + private final BaseQuestionRepository baseQuestionRepository; + + @Transactional + public BaseQuestionResponseList saveBaseQuestions(List requests) { + List baseQuestionResponseList = requests.stream() + .map(this::saveBaseQuestion) + .collect(Collectors.toList()); + return BaseQuestionResponseList.of(baseQuestionResponseList); + } + + private BaseQuestionResponse saveBaseQuestion(BaseQuestionCreateRequest request) { + BaseQuestion baseQuestion = BaseQuestion.create(request.content()); + baseQuestionRepository.save(baseQuestion); + return BaseQuestionResponse.of(baseQuestion); + } + +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/domain/BaseQuestion.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/domain/BaseQuestion.java new file mode 100644 index 0000000..f1e6b82 --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/domain/BaseQuestion.java @@ -0,0 +1,41 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.domain; + +import goorm.kgu.familynote.common.domain.BaseTimeEntity; +import goorm.kgu.familynote.domain.family.family.domain.Family; +import goorm.kgu.familynote.domain.family.member.domain.FamilyMember; +import goorm.kgu.familynote.domain.user.domain.User; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import static jakarta.persistence.GenerationType.IDENTITY; + +@Getter +@Entity +@Builder +@Table(name = "base_question") +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class BaseQuestion extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = IDENTITY) + private Long id; + + @Column(nullable = false) + private String content; + + public static BaseQuestion create(String content) { + return BaseQuestion.builder() + .content(content) + .build(); + } + +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/domain/BaseQuestionRepository.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/domain/BaseQuestionRepository.java new file mode 100644 index 0000000..10bfba7 --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/domain/BaseQuestionRepository.java @@ -0,0 +1,8 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.domain; + + +public interface BaseQuestionRepository { + + BaseQuestion save(BaseQuestion baseQuestion); + +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/infrastructure/BaseQuestionRepositoryImpl.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/infrastructure/BaseQuestionRepositoryImpl.java new file mode 100644 index 0000000..30f67ee --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/infrastructure/BaseQuestionRepositoryImpl.java @@ -0,0 +1,17 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.infrastructure; + +import goorm.kgu.familynote.domain.question.baseQuestion.domain.BaseQuestion; +import goorm.kgu.familynote.domain.question.baseQuestion.domain.BaseQuestionRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +@Repository +@RequiredArgsConstructor +public class BaseQuestionRepositoryImpl implements BaseQuestionRepository { + private final JpaBaseQuestionRepository jpaBaseQuestionRepository; + + @Override + public BaseQuestion save(BaseQuestion baseQuestion) { + return jpaBaseQuestionRepository.save(baseQuestion); + } +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/infrastructure/JpaBaseQuestionRepository.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/infrastructure/JpaBaseQuestionRepository.java new file mode 100644 index 0000000..1c421c6 --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/infrastructure/JpaBaseQuestionRepository.java @@ -0,0 +1,7 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.infrastructure; + +import goorm.kgu.familynote.domain.question.baseQuestion.domain.BaseQuestion; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface JpaBaseQuestionRepository extends JpaRepository { +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/BaseQuestionController.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/BaseQuestionController.java new file mode 100644 index 0000000..8bfa84e --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/BaseQuestionController.java @@ -0,0 +1,52 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.presentation; + +import goorm.kgu.familynote.common.exception.ExceptionResponse; +import goorm.kgu.familynote.domain.family.family.presentation.response.FamilyPersistResponse; +import goorm.kgu.familynote.domain.family.member.presentation.request.FamilyMemberCreateRequest; +import goorm.kgu.familynote.domain.question.baseQuestion.application.BaseQuestionService; +import goorm.kgu.familynote.domain.question.baseQuestion.presentation.request.BaseQuestionCreateRequest; +import goorm.kgu.familynote.domain.question.baseQuestion.presentation.response.BaseQuestionResponse; +import goorm.kgu.familynote.domain.question.baseQuestion.presentation.response.BaseQuestionResponseList; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +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.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import static org.springframework.http.HttpStatus.CREATED; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/question/base") +@Tag(name = "Base Question", description = "기본 질문 관련 api / 담당자 : 전민주") +public class BaseQuestionController { + private final BaseQuestionService baseQuestionService; + + @Operation(summary = "기본 질문 등록", description = "기본 질문을 등록합니다. 개발자/관리자가 API를 통해서 조작할 수 있습니다.") + @ApiResponses({ + @ApiResponse( + responseCode = "201", + description = "기본 질문 등록 성공", + content = @Content(schema = @Schema(implementation = FamilyPersistResponse.class)) + ) + }) + @ResponseStatus(CREATED) + @PostMapping + public ResponseEntity createBaseQuestions( + @Valid @RequestBody List requests + ) { + BaseQuestionResponseList response = baseQuestionService.saveBaseQuestions(requests); + return ResponseEntity.ok(response); + } + +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/request/BaseQuestionCreateRequest.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/request/BaseQuestionCreateRequest.java new file mode 100644 index 0000000..4c867ee --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/request/BaseQuestionCreateRequest.java @@ -0,0 +1,11 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.presentation.request; + +import io.swagger.v3.oas.annotations.media.Schema; + +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; + +public record BaseQuestionCreateRequest( + @Schema(description = "기본 질문 내용", example = "탕수육 먹을 때 부먹 vs 찍먹", requiredMode = REQUIRED) + String content +) { +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/response/BaseQuestionResponse.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/response/BaseQuestionResponse.java new file mode 100644 index 0000000..4c4757e --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/response/BaseQuestionResponse.java @@ -0,0 +1,30 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.presentation.response; + +import goorm.kgu.familynote.domain.question.baseQuestion.domain.BaseQuestion; +import io.swagger.v3.oas.annotations.media.Schema; +import java.util.List; +import java.util.stream.Collectors; + +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; + +public record BaseQuestionResponse( + @Schema(description = "기본 질문 id", example = "1", requiredMode = REQUIRED) + Long baseQuestionId, + + @Schema(description = "기본 질문 내용", example = "짜장면 vs 짬뽕", requiredMode = REQUIRED) + String content +) { + + public static List of(List baseQuestions) { + return baseQuestions.stream() + .map(BaseQuestionResponse::of) + .collect(Collectors.toList()); + } + + public static BaseQuestionResponse of(BaseQuestion baseQuestion) { + return new BaseQuestionResponse( + baseQuestion.getId(), + baseQuestion.getContent() + ); + } +} diff --git a/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/response/BaseQuestionResponseList.java b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/response/BaseQuestionResponseList.java new file mode 100644 index 0000000..c1a739e --- /dev/null +++ b/src/main/java/goorm/kgu/familynote/domain/question/baseQuestion/presentation/response/BaseQuestionResponseList.java @@ -0,0 +1,25 @@ +package goorm.kgu.familynote.domain.question.baseQuestion.presentation.response; + +import io.swagger.v3.oas.annotations.media.Schema; +import java.util.List; + +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; + +public record BaseQuestionResponseList ( + @Schema( + description = "기본 질문 리스트", + example = + " [{\"content\": \"오늘 하루 중 가장 즐거웠던 일은 무엇이었나요?\"},\n" + + " {\"content\": \"요즘 가장 관심 있는 취미나 활동은 무엇인가요?\"},\n" + + " {\"content\": \"어렸을 때의 꿈은 무엇이었나요? 지금도 그 꿈이 있나요?\"}]", + requiredMode = REQUIRED + ) + List contents +) { + + public static BaseQuestionResponseList of(List baseQuestionResponseList) { + return new BaseQuestionResponseList( + baseQuestionResponseList + ); + } +} diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index d0a6e1f..7212038 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,4 +1,4 @@ spring: jpa: hibernate: - ddl-auto: update + ddl-auto: create diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 22cd54d..0aacd00 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,3 +1,6 @@ +server: + port: 5252 + spring: application: name: family-note-server