diff --git a/src/main/java/com/fullcar/carpool/application/CarpoolMapper.java b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolMapper.java similarity index 89% rename from src/main/java/com/fullcar/carpool/application/CarpoolMapper.java rename to src/main/java/com/fullcar/carpool/application/carpool/CarpoolMapper.java index 7cea456..59e48eb 100644 --- a/src/main/java/com/fullcar/carpool/application/CarpoolMapper.java +++ b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolMapper.java @@ -1,11 +1,11 @@ -package com.fullcar.carpool.application; +package com.fullcar.carpool.application.carpool; -import com.fullcar.carpool.domain.Carpool; -import com.fullcar.carpool.domain.Cost; -import com.fullcar.carpool.domain.Driver; +import com.fullcar.carpool.domain.carpool.Carpool; +import com.fullcar.carpool.domain.carpool.Cost; +import com.fullcar.carpool.domain.carpool.Driver; import com.fullcar.carpool.domain.service.CarpoolIdService; -import com.fullcar.carpool.presentation.dto.request.CarpoolRequestDto; -import com.fullcar.carpool.presentation.dto.response.CarpoolResponseDto; +import com.fullcar.carpool.presentation.carpool.dto.request.CarpoolRequestDto; +import com.fullcar.carpool.presentation.carpool.dto.response.CarpoolResponseDto; import com.fullcar.member.domain.car.Car; import com.fullcar.member.domain.member.Member; diff --git a/src/main/java/com/fullcar/carpool/application/CarpoolService.java b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolService.java similarity index 83% rename from src/main/java/com/fullcar/carpool/application/CarpoolService.java rename to src/main/java/com/fullcar/carpool/application/carpool/CarpoolService.java index 44fb8bc..708b308 100644 --- a/src/main/java/com/fullcar/carpool/application/CarpoolService.java +++ b/src/main/java/com/fullcar/carpool/application/carpool/CarpoolService.java @@ -1,10 +1,10 @@ -package com.fullcar.carpool.application; +package com.fullcar.carpool.application.carpool; -import com.fullcar.carpool.domain.Carpool; -import com.fullcar.carpool.domain.CarpoolId; -import com.fullcar.carpool.domain.CarpoolRepository; -import com.fullcar.carpool.presentation.dto.request.CarpoolRequestDto; -import com.fullcar.carpool.presentation.dto.response.CarpoolResponseDto; +import com.fullcar.carpool.domain.carpool.Carpool; +import com.fullcar.carpool.domain.carpool.CarpoolId; +import com.fullcar.carpool.domain.carpool.CarpoolRepository; +import com.fullcar.carpool.presentation.carpool.dto.request.CarpoolRequestDto; +import com.fullcar.carpool.presentation.carpool.dto.response.CarpoolResponseDto; import com.fullcar.core.exception.CustomException; import com.fullcar.core.response.ErrorCode; import com.fullcar.member.domain.car.Car; @@ -26,7 +26,7 @@ @RequiredArgsConstructor(access = AccessLevel.PROTECTED) public class CarpoolService { private final CarpoolRepository carpoolRepository; - private final CarRepository carRepository; + private final CarRepository carRepository; //:TODO Event 기반으로 변경 private final CarpoolMapper carpoolMapper; @Transactional diff --git a/src/main/java/com/fullcar/carpool/application/form/FormMapper.java b/src/main/java/com/fullcar/carpool/application/form/FormMapper.java new file mode 100644 index 0000000..adbbba5 --- /dev/null +++ b/src/main/java/com/fullcar/carpool/application/form/FormMapper.java @@ -0,0 +1,53 @@ +package com.fullcar.carpool.application.form; + +import com.fullcar.carpool.domain.carpool.CarpoolId; +import com.fullcar.carpool.domain.form.Cost; +import com.fullcar.carpool.domain.form.Form; +import com.fullcar.carpool.domain.form.Passenger; +import com.fullcar.carpool.domain.service.FormIdService; +import com.fullcar.carpool.presentation.form.dto.request.FormRequestDto; +import com.fullcar.carpool.presentation.form.dto.response.FormResponseDto; +import com.fullcar.member.domain.member.Member; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor(access = AccessLevel.PROTECTED) +public class FormMapper { + private final FormIdService formIdService; + + public FormResponseDto toDto(Form form, Member member) { + return FormResponseDto.builder() + .id(form.getFormId().getId()) + .pickupLocation(form.getPickupLocation()) + .periodType(form.getCost().getPeriodType()) + .money(form.getCost().getMoney()) + .content(form.getContent()) + .formState(form.getFormState()) + .companyName(member.getCompany().getCompanyName()) + .nickname(member.getNickname()) + .createdAt(form.getCreatedAt()) + .build(); + } + + public Form toEntity(Member member, CarpoolId carpoolId, FormRequestDto formRequestDto) { + return Form.builder() + .formId(formIdService.nextId()) + .content(formRequestDto.getContent()) + .pickupLocation(formRequestDto.getPickupLocation()) + .cost( + Cost.builder() + .periodType(formRequestDto.getPeriodType()) + .money(formRequestDto.getMoney()) + .build() + ) + .passenger( + Passenger.builder() + .memberId(member.getId()) + .build() + ) + .carpoolId(carpoolId) + .build(); + } +} diff --git a/src/main/java/com/fullcar/carpool/application/form/FormService.java b/src/main/java/com/fullcar/carpool/application/form/FormService.java new file mode 100644 index 0000000..9ca0621 --- /dev/null +++ b/src/main/java/com/fullcar/carpool/application/form/FormService.java @@ -0,0 +1,40 @@ +package com.fullcar.carpool.application.form; + +import com.fullcar.carpool.domain.carpool.Carpool; +import com.fullcar.carpool.domain.carpool.CarpoolId; +import com.fullcar.carpool.domain.carpool.CarpoolRepository; +import com.fullcar.carpool.domain.form.Form; +import com.fullcar.carpool.domain.form.FormRepository; +import com.fullcar.carpool.presentation.form.dto.request.FormRequestDto; +import com.fullcar.carpool.presentation.form.dto.response.FormResponseDto; +import com.fullcar.core.exception.CustomException; +import com.fullcar.core.response.ErrorCode; +import com.fullcar.member.domain.member.Member; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.annotation.Validated; + +@Validated +@Service +@RequiredArgsConstructor(access = AccessLevel.PROTECTED) +public class FormService { + private final FormRepository formRepository; + private final CarpoolRepository carpoolRepository; + private final FormMapper formMapper; + + @Transactional + public FormResponseDto requestForm(Member member, CarpoolId carpoolId, FormRequestDto formRequestDto) { + Carpool carpool = carpoolRepository.findByCarpoolIdAndIsDeleted(carpoolId, false) + .orElseThrow(() -> new CustomException(ErrorCode.NOT_EXIST_CARPOOL)); + + Form form = formMapper.toEntity(member, carpoolId, formRequestDto); + + return formMapper.toDto( + formRepository.saveAndFlush(form), + member + ); + } + +} diff --git a/src/main/java/com/fullcar/carpool/domain/Carpool.java b/src/main/java/com/fullcar/carpool/domain/carpool/Carpool.java similarity index 96% rename from src/main/java/com/fullcar/carpool/domain/Carpool.java rename to src/main/java/com/fullcar/carpool/domain/carpool/Carpool.java index d0fc364..4f26d7f 100644 --- a/src/main/java/com/fullcar/carpool/domain/Carpool.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/Carpool.java @@ -1,4 +1,4 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; import jakarta.persistence.*; import lombok.*; diff --git a/src/main/java/com/fullcar/carpool/domain/CarpoolId.java b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolId.java similarity index 90% rename from src/main/java/com/fullcar/carpool/domain/CarpoolId.java rename to src/main/java/com/fullcar/carpool/domain/carpool/CarpoolId.java index 59d9960..0b8903c 100644 --- a/src/main/java/com/fullcar/carpool/domain/CarpoolId.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolId.java @@ -1,4 +1,4 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; diff --git a/src/main/java/com/fullcar/carpool/domain/CarpoolRepository.java b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolRepository.java similarity index 89% rename from src/main/java/com/fullcar/carpool/domain/CarpoolRepository.java rename to src/main/java/com/fullcar/carpool/domain/carpool/CarpoolRepository.java index 4cd733e..933bd25 100644 --- a/src/main/java/com/fullcar/carpool/domain/CarpoolRepository.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolRepository.java @@ -1,11 +1,10 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; import java.util.Optional; @Repository diff --git a/src/main/java/com/fullcar/carpool/domain/CarpoolState.java b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolState.java similarity index 53% rename from src/main/java/com/fullcar/carpool/domain/CarpoolState.java rename to src/main/java/com/fullcar/carpool/domain/carpool/CarpoolState.java index 8901d99..652d28e 100644 --- a/src/main/java/com/fullcar/carpool/domain/CarpoolState.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/CarpoolState.java @@ -1,4 +1,4 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; public enum CarpoolState { OPEN, diff --git a/src/main/java/com/fullcar/carpool/domain/Cost.java b/src/main/java/com/fullcar/carpool/domain/carpool/Cost.java similarity index 92% rename from src/main/java/com/fullcar/carpool/domain/Cost.java rename to src/main/java/com/fullcar/carpool/domain/carpool/Cost.java index 97f985c..f625610 100644 --- a/src/main/java/com/fullcar/carpool/domain/Cost.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/Cost.java @@ -1,4 +1,4 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; diff --git a/src/main/java/com/fullcar/carpool/domain/Driver.java b/src/main/java/com/fullcar/carpool/domain/carpool/Driver.java similarity index 92% rename from src/main/java/com/fullcar/carpool/domain/Driver.java rename to src/main/java/com/fullcar/carpool/domain/carpool/Driver.java index f18a224..4a357f5 100644 --- a/src/main/java/com/fullcar/carpool/domain/Driver.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/Driver.java @@ -1,4 +1,4 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; import com.fullcar.member.domain.member.MemberId; import jakarta.persistence.*; diff --git a/src/main/java/com/fullcar/carpool/domain/MoodType.java b/src/main/java/com/fullcar/carpool/domain/carpool/MoodType.java similarity index 52% rename from src/main/java/com/fullcar/carpool/domain/MoodType.java rename to src/main/java/com/fullcar/carpool/domain/carpool/MoodType.java index cbe55ff..1fcc89f 100644 --- a/src/main/java/com/fullcar/carpool/domain/MoodType.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/MoodType.java @@ -1,4 +1,4 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; public enum MoodType { CHATTY, diff --git a/src/main/java/com/fullcar/carpool/domain/PeriodType.java b/src/main/java/com/fullcar/carpool/domain/carpool/PeriodType.java similarity index 68% rename from src/main/java/com/fullcar/carpool/domain/PeriodType.java rename to src/main/java/com/fullcar/carpool/domain/carpool/PeriodType.java index 190a6c9..3caa46a 100644 --- a/src/main/java/com/fullcar/carpool/domain/PeriodType.java +++ b/src/main/java/com/fullcar/carpool/domain/carpool/PeriodType.java @@ -1,4 +1,4 @@ -package com.fullcar.carpool.domain; +package com.fullcar.carpool.domain.carpool; public enum PeriodType { ONCE, diff --git a/src/main/java/com/fullcar/carpool/domain/form/Cost.java b/src/main/java/com/fullcar/carpool/domain/form/Cost.java new file mode 100644 index 0000000..f2b3ba8 --- /dev/null +++ b/src/main/java/com/fullcar/carpool/domain/form/Cost.java @@ -0,0 +1,26 @@ +package com.fullcar.carpool.domain.form; + +import com.fullcar.carpool.domain.carpool.PeriodType; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import static lombok.AccessLevel.PROTECTED; + +@Embeddable +@Getter +@Builder +@NoArgsConstructor(access = PROTECTED) +@AllArgsConstructor(access = PROTECTED) +public class Cost { + private Long money; + + @Enumerated(EnumType.STRING) + @Column(name = "period_type") + private PeriodType periodType; +} diff --git a/src/main/java/com/fullcar/carpool/domain/form/Form.java b/src/main/java/com/fullcar/carpool/domain/form/Form.java new file mode 100644 index 0000000..b1c52fd --- /dev/null +++ b/src/main/java/com/fullcar/carpool/domain/form/Form.java @@ -0,0 +1,55 @@ +package com.fullcar.carpool.domain.form; + +import com.fullcar.carpool.domain.carpool.CarpoolId; +import jakarta.persistence.*; +import lombok.*; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; + +@Entity +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@EntityListeners(AuditingEntityListener.class) +@Table(name = "form") +public class Form { + + @EmbeddedId + private FormId formId; + + private String content; + + @Column(name = "pickup_location") + private String pickupLocation; + + @Embedded + private Cost cost; + + @Enumerated(EnumType.STRING) + @Column(name = "form_state") + @Builder.Default + private FormState formState = FormState.REQUEST; + + @Embedded + private Passenger passenger; + + @Embedded + @AttributeOverride(name = "id", column = @Column(name = "carpool_id")) + private CarpoolId carpoolId; + + @Column(name = "is_deleted", nullable = false) + @Builder.Default + private boolean isDeleted = false; + + @Column(name = "created_at") + @CreatedDate + private LocalDateTime createdAt; + + @Column(name = "updated_at") + @LastModifiedDate + private LocalDateTime updatedAt; +} diff --git a/src/main/java/com/fullcar/carpool/domain/form/FormId.java b/src/main/java/com/fullcar/carpool/domain/form/FormId.java new file mode 100644 index 0000000..fe3e73d --- /dev/null +++ b/src/main/java/com/fullcar/carpool/domain/form/FormId.java @@ -0,0 +1,21 @@ +package com.fullcar.carpool.domain.form; + +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +import static lombok.AccessLevel.PROTECTED; + +@Embeddable +@Getter +@NoArgsConstructor(access = PROTECTED) +@AllArgsConstructor +public class FormId implements Serializable { + + @Column(name = "id") + private Long id; +} diff --git a/src/main/java/com/fullcar/carpool/domain/form/FormRepository.java b/src/main/java/com/fullcar/carpool/domain/form/FormRepository.java new file mode 100644 index 0000000..2a7ad6c --- /dev/null +++ b/src/main/java/com/fullcar/carpool/domain/form/FormRepository.java @@ -0,0 +1,8 @@ +package com.fullcar.carpool.domain.form; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface FormRepository extends JpaRepository