Skip to content

Commit

Permalink
[DDING-86] 동아리 지원 폼지 수정 API 구현 (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
KoSeonJe authored Feb 3, 2025
1 parent 38896cf commit a0cd6a8
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public Club getByUserId(Long userId) {
@Override
public Club getByUserIdWithFetch(Long userId) {
return clubRepository.findEntityGraphByUserId(userId)
.orElseThrow(() -> new ResourceNotFound("Club(userId=" + userId + ")를 찾을 수 없습니다.")); }
.orElseThrow(() -> new ResourceNotFound("Club(userId=" + userId + ")를 찾을 수 없습니다."));
}

@Override
public List<Club> findAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import ddingdong.ddingdongBE.auth.PrincipalDetails;
import ddingdong.ddingdongBE.domain.form.controller.dto.request.CreateFormRequest;
import ddingdong.ddingdongBE.domain.form.controller.dto.request.UpdateFormRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -26,4 +29,15 @@ void createForm(
@RequestBody CreateFormRequest createFormRequest,
@AuthenticationPrincipal PrincipalDetails principalDetails
);

@Operation(summary = "동아리 지원 폼지 수정 API")
@ApiResponse(responseCode = "204", description = "동아리 지원 폼지 수정 성공")
@ResponseStatus(HttpStatus.NO_CONTENT)
@SecurityRequirement(name = "AccessToken")
@PutMapping("/my/forms/{formId}")
void updateForm(
@RequestBody UpdateFormRequest updateFormRequest,
@PathVariable("formId") Long formId,
@AuthenticationPrincipal PrincipalDetails principalDetails
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ddingdong.ddingdongBE.auth.PrincipalDetails;
import ddingdong.ddingdongBE.domain.form.api.CentralFormApi;
import ddingdong.ddingdongBE.domain.form.controller.dto.request.CreateFormRequest;
import ddingdong.ddingdongBE.domain.form.controller.dto.request.UpdateFormRequest;
import ddingdong.ddingdongBE.domain.form.service.FacadeCentralFormService;
import ddingdong.ddingdongBE.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,4 +23,13 @@ public void createForm(
User user = principalDetails.getUser();
facadeCentralFormService.createForm(createFormRequest.toCommand(user));
}

@Override
public void updateForm(
UpdateFormRequest updateFormRequest,
Long formId,
PrincipalDetails principalDetails
) {
facadeCentralFormService.updateForm(updateFormRequest.toCommand(formId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package ddingdong.ddingdongBE.domain.form.controller.dto.request;

import ddingdong.ddingdongBE.domain.form.entity.FieldType;
import ddingdong.ddingdongBE.domain.form.service.dto.command.UpdateFormCommand;
import ddingdong.ddingdongBE.domain.form.service.dto.command.UpdateFormCommand.UpdateFormFieldCommand;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;

public record UpdateFormRequest(
@Schema(description = "폼지 제목", example = "폼지제목입니다")
@NotNull(message = "폼지 제목은 null이 될 수 없습니다.")
String title,

@Schema(description = "폼지 설명", example = "우리 동아리는 띵동입니다.")
String description,

@Schema(description = "폼지 시작일자", example = "2001-01-01")
@NotNull(message = "폼지 시작일자는 null이 될 수 없습니다.")
LocalDate startDate,

@Schema(description = "폼지 종료일자", example = "2001-01-02")
@NotNull(message = "폼지 종료일자는 null이 될 수 없습니다.")
LocalDate endDate,

@Schema(description = "면접여부", example = "true")
@NotNull(message = "면접여부는 null이 될 수 없습니다.")
boolean hasInterview,

@ArraySchema(schema = @Schema(implementation = UpdateFormFieldRequest.class))
List<UpdateFormFieldRequest> formFields
) {

record UpdateFormFieldRequest(
@Schema(description = "폼지 질문", example = "우리 동아리 들어올겁니까?")
@NotNull(message = "질문는 null이 될 수 없습니다.")
String question,

@Schema(description = "질문 종류", example = "CHECK_BOX")
@NotNull(message = "질문 종류는 null이 될 수 없습니다.")
FieldType type,

@Schema(description = "질문의 선택리스트", example = "[지문1이다., 지문2이다., 지문3이다.]")
List<String> options,

@Schema(description = "필수여부", example = "true")
@NotNull(message = "필수여부는 null이 될 수 없습니다.")
boolean required,

@Schema(description = "질문 순서", example = "1")
@NotNull(message = "질문 순서는 null이 될 수 없습니다.")
int order,

@Schema(description = "질문 섹션 종류", example = "공통")
@NotNull(message = "질문 섹션종류는 null이 될 수 없습니다.")
String section
) {

public UpdateFormFieldCommand toCommand() {
return UpdateFormFieldCommand.builder()
.question(question)
.type(type)
.options(options)
.required(required)
.order(order)
.section(section)
.build();
}
}

public UpdateFormCommand toCommand(Long formId) {
List<UpdateFormFieldCommand> updateFormFieldCommands = formFields.stream()
.map(UpdateFormFieldRequest::toCommand)
.toList();
return UpdateFormCommand.builder()
.formId(formId)
.title(title)
.description(description)
.startDate(startDate)
.endDate(endDate)
.hasInterview(hasInterview)
.formFieldCommands(updateFormFieldCommands)
.build();
}

}
18 changes: 16 additions & 2 deletions src/main/java/ddingdong/ddingdongBE/domain/form/entity/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@ public class Form extends BaseEntity {
private Club club;

@Builder
private Form(String title, String description, LocalDate startDate, LocalDate endDate, boolean hasInterview,
Club club) {
private Form(
String title,
String description,
LocalDate startDate,
LocalDate endDate,
boolean hasInterview,
Club club
) {
this.title = title;
this.description = description;
this.startDate = startDate;
this.endDate = endDate;
this.hasInterview = hasInterview;
this.club = club;
}

public void update(Form updateForm) {
this.title = updateForm.getTitle();
this.description = updateForm.getDescription();
this.startDate = updateForm.getStartDate();
this.endDate = updateForm.getEndDate();
this.hasInterview = updateForm.isHasInterview();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ public class FormField extends BaseEntity {
private Form form;

@Builder
private FormField(String question, FieldType fieldType, boolean required, int fieldOrder, String section,
List<String> options, Form form) {
private FormField(
String question,
FieldType fieldType,
boolean required,
int fieldOrder,
String section,
List<String> options,
Form form
) {
this.question = question;
this.fieldType = fieldType;
this.required = required;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ddingdong.ddingdongBE.domain.form.repository;

import ddingdong.ddingdongBE.domain.form.entity.Form;
import ddingdong.ddingdongBE.domain.form.entity.FormField;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FormFieldRepository extends JpaRepository<FormField, Long> {

List<FormField> findAllByForm(Form form);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ddingdong.ddingdongBE.domain.form.service;

import ddingdong.ddingdongBE.domain.form.service.dto.command.CreateFormCommand;
import ddingdong.ddingdongBE.domain.form.service.dto.command.UpdateFormCommand;

public interface FacadeCentralFormService {

void createForm(CreateFormCommand command);

void updateForm(UpdateFormCommand command);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import ddingdong.ddingdongBE.domain.form.entity.FormField;
import ddingdong.ddingdongBE.domain.form.service.dto.command.CreateFormCommand;
import ddingdong.ddingdongBE.domain.form.service.dto.command.CreateFormCommand.CreateFormFieldCommand;
import ddingdong.ddingdongBE.domain.form.service.dto.command.UpdateFormCommand;
import ddingdong.ddingdongBE.domain.form.service.dto.command.UpdateFormCommand.UpdateFormFieldCommand;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -27,11 +29,31 @@ public void createForm(CreateFormCommand createFormCommand) {
Form form = createFormCommand.toEntity(club);
Form savedForm = formService.create(form);

List<FormField> formFields = toFormFields(savedForm, createFormCommand.formFieldCommands());
List<FormField> formFields = toCreateFormFields(savedForm, createFormCommand.formFieldCommands());
formFieldService.createAll(formFields);
}

private List<FormField> toFormFields(Form savedForm, List<CreateFormFieldCommand> createFormFieldCommands) {
@Transactional
@Override
public void updateForm(UpdateFormCommand updateFormCommand) {
Form originform = formService.getById(updateFormCommand.formId());
Form updateForm = updateFormCommand.toEntity();
originform.update(updateForm);

List<FormField> originFormFields = formFieldService.findAllByForm(originform);
formFieldService.deleteAll(originFormFields);

List<FormField> updateFormFields = toUpdateFormFields(originform, updateFormCommand.formFieldCommands());
formFieldService.createAll(updateFormFields);
}

private List<FormField> toUpdateFormFields(Form originform, List<UpdateFormFieldCommand> updateFormFieldCommands) {
return updateFormFieldCommands.stream()
.map(formFieldCommand -> formFieldCommand.toEntity(originform))
.toList();
}

private List<FormField> toCreateFormFields(Form savedForm, List<CreateFormFieldCommand> createFormFieldCommands) {
return createFormFieldCommands.stream()
.map(formFieldCommand -> formFieldCommand.toEntity(savedForm))
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package ddingdong.ddingdongBE.domain.form.service;

import ddingdong.ddingdongBE.domain.form.entity.Form;
import ddingdong.ddingdongBE.domain.form.entity.FormField;
import java.util.List;

public interface FormFieldService {

void createAll(List<FormField> formFields);

List<FormField> findAllByForm(Form form);

void deleteAll(List<FormField> originFormFields);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface FormService {

Form create(Form form);

Form getById(Long formId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ddingdong.ddingdongBE.domain.form.service;

import ddingdong.ddingdongBE.domain.form.entity.Form;
import ddingdong.ddingdongBE.domain.form.entity.FormField;
import ddingdong.ddingdongBE.domain.form.repository.FormFieldRepository;
import java.util.List;
Expand All @@ -19,4 +20,15 @@ public class GeneralFormFieldService implements FormFieldService {
public void createAll(List<FormField> formFields) {
formFieldRepository.saveAll(formFields);
}

@Override
public List<FormField> findAllByForm(Form form) {
return formFieldRepository.findAllByForm(form);
}

@Transactional
@Override
public void deleteAll(List<FormField> originFormFields) {
formFieldRepository.deleteAll(originFormFields);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ddingdong.ddingdongBE.domain.form.service;

import ddingdong.ddingdongBE.common.exception.PersistenceException.ResourceNotFound;
import ddingdong.ddingdongBE.domain.form.entity.Form;
import ddingdong.ddingdongBE.domain.form.repository.FormRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -18,4 +19,10 @@ public class GeneralFormService implements FormService{
public Form create(Form form) {
return formRepository.save(form);
}

@Override
public Form getById(Long formId) {
return formRepository.findById(formId)
.orElseThrow(() -> new ResourceNotFound("Form(formId=" + formId + ")를 찾을 수 없습니다."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ddingdong.ddingdongBE.domain.form.service.dto.command;

import ddingdong.ddingdongBE.domain.form.entity.FieldType;
import ddingdong.ddingdongBE.domain.form.entity.Form;
import ddingdong.ddingdongBE.domain.form.entity.FormField;
import java.time.LocalDate;
import java.util.List;
import lombok.Builder;

@Builder
public record UpdateFormCommand(
Long formId,
String title,
String description,
LocalDate startDate,
LocalDate endDate,
boolean hasInterview,
List<UpdateFormFieldCommand> formFieldCommands
) {

@Builder
public record UpdateFormFieldCommand(
String question,
FieldType type,
List<String> options,
boolean required,
int order,
String section
) {

public FormField toEntity(Form form) {
return FormField.builder()
.form(form)
.question(question)
.fieldType(type)
.options(options)
.required(required)
.fieldOrder(order)
.section(section)
.build();
}
}

public Form toEntity() {
return Form.builder()
.title(title)
.description(description)
.startDate(startDate)
.endDate(endDate)
.hasInterview(hasInterview)
.build();
}
}
Loading

0 comments on commit a0cd6a8

Please sign in to comment.