Skip to content

Commit

Permalink
[GOORM-39] 기본 질문 생성 API 작성 (#8)
Browse files Browse the repository at this point in the history
* [GOORM-39]-feat: yml 파일 server port 명시 및 ddl-auto 수정

* [GOORM-39]-feat: 기본 질문 생성 API 구현

* [GOORM-39]-refactor: yml 파일 ddl-auto 수정

* [GOORM-39]-refactor: BaseQuestionController URL 수정

* [GOORM-39]-refactor: BaseQuestion의 content 칼럼 nullable = false 제약 추가

* [GOORM-39]-refactor: BaseQuestionResponseList로 반환하도록 수정

* [GOORM-39]-refactor: 코드 컨벤션 통일
  • Loading branch information
mingmingmon authored Sep 10, 2024
1 parent 342bc9c commit bf4e6ec
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<BaseQuestionCreateRequest> requests) {
List<BaseQuestionResponse> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package goorm.kgu.familynote.domain.question.baseQuestion.domain;


public interface BaseQuestionRepository {

BaseQuestion save(BaseQuestion baseQuestion);

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<BaseQuestion, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<BaseQuestionResponseList> createBaseQuestions(
@Valid @RequestBody List<BaseQuestionCreateRequest> requests
) {
BaseQuestionResponseList response = baseQuestionService.saveBaseQuestions(requests);
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -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<BaseQuestionResponse> of(List<BaseQuestion> baseQuestions) {
return baseQuestions.stream()
.map(BaseQuestionResponse::of)
.collect(Collectors.toList());
}

public static BaseQuestionResponse of(BaseQuestion baseQuestion) {
return new BaseQuestionResponse(
baseQuestion.getId(),
baseQuestion.getContent()
);
}
}
Original file line number Diff line number Diff line change
@@ -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<BaseQuestionResponse> contents
) {

public static BaseQuestionResponseList of(List<BaseQuestionResponse> baseQuestionResponseList) {
return new BaseQuestionResponseList(
baseQuestionResponseList
);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring:
jpa:
hibernate:
ddl-auto: update
ddl-auto: create
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
server:
port: 5252

spring:
application:
name: family-note-server
Expand Down

0 comments on commit bf4e6ec

Please sign in to comment.