Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Form request API #41

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/fullcar/carpool/application/form/FormMapper.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CarpoolService에도 findByCarpoolIdAndIsDeleted ν•¨μˆ˜κ°€ λ°˜λ³΅λΌμ„œ μ‚¬μš©λ˜κ³  μžˆλŠ”λ°, CarpoolRepositoryμ—μ„œ default method둜 μ’€ 더 κΉ”λ”ν•˜κ²Œ μ‚¬μš©ν•˜λŠ” 건 μ–΄λ–¨κΉŒμš”??

ex)
default Member findByIdAndIsDeletedOrThrow(MemberId id, boolean isDeleted) {
return findByIdAndIsDeleted(id, isDeleted)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_EXIST_USER));
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쒋은 리뷰 κ°μ‚¬ν•©λ‹ˆλ‹€! λ°˜μ˜ν–ˆμŠ΅λ‹ˆλ‹€~


Form form = formMapper.toEntity(member, carpoolId, formRequestDto);

return formMapper.toDto(
formRepository.saveAndFlush(form),
member
);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fullcar.carpool.domain;
package com.fullcar.carpool.domain.carpool;

import jakarta.persistence.*;
import lombok.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fullcar.carpool.domain;
package com.fullcar.carpool.domain.carpool;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fullcar.carpool.domain;
package com.fullcar.carpool.domain.carpool;

public enum CarpoolState {
OPEN,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fullcar.carpool.domain;
package com.fullcar.carpool.domain.carpool;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fullcar.carpool.domain;
package com.fullcar.carpool.domain.carpool;

public enum MoodType {
CHATTY,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fullcar.carpool.domain;
package com.fullcar.carpool.domain.carpool;

public enum PeriodType {
ONCE,
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/fullcar/carpool/domain/form/Cost.java
Original file line number Diff line number Diff line change
@@ -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;
}
55 changes: 55 additions & 0 deletions src/main/java/com/fullcar/carpool/domain/form/Form.java
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions src/main/java/com/fullcar/carpool/domain/form/FormId.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Form, FormId> {
}
7 changes: 7 additions & 0 deletions src/main/java/com/fullcar/carpool/domain/form/FormState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.fullcar.carpool.domain.form;

public enum FormState {
REQUEST,
ACCEPT,
REJECT
}
16 changes: 16 additions & 0 deletions src/main/java/com/fullcar/carpool/domain/form/Passenger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fullcar.carpool.domain.form;

import com.fullcar.member.domain.member.MemberId;
import jakarta.persistence.*;
import lombok.*;

@Embeddable
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class Passenger {
@Embedded
@AttributeOverride(name = "id", column = @Column(name = "passenger_id"))
private MemberId memberId;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fullcar.carpool.domain.service;

import com.fullcar.carpool.domain.CarpoolId;
import com.fullcar.carpool.domain.carpool.CarpoolId;
import com.fullcar.core.id.SnowFlake;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fullcar.carpool.domain.service;

import com.fullcar.carpool.domain.form.FormId;
import com.fullcar.core.id.SnowFlake;
import org.springframework.stereotype.Service;

@Service
public class FormIdService {
private final SnowFlake snowFlake;

public FormIdService() {
snowFlake = new SnowFlake(255);
}
public FormId nextId() {
return new FormId(snowFlake.nextId());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.fullcar.carpool.presentation;
package com.fullcar.carpool.presentation.carpool;

import com.fullcar.carpool.application.CarpoolService;
import com.fullcar.carpool.domain.CarpoolId;
import com.fullcar.carpool.presentation.dto.request.CarpoolRequestDto;
import com.fullcar.carpool.presentation.dto.response.CarpoolResponseDto;
import com.fullcar.carpool.application.carpool.CarpoolService;
import com.fullcar.carpool.domain.carpool.CarpoolId;
import com.fullcar.carpool.presentation.carpool.dto.request.CarpoolRequestDto;
import com.fullcar.carpool.presentation.carpool.dto.response.CarpoolResponseDto;
import com.fullcar.core.annotation.CurrentMember;
import com.fullcar.core.response.ApiResponse;
import com.fullcar.core.response.SuccessCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.fullcar.carpool.presentation.dto.request;
package com.fullcar.carpool.presentation.carpool.dto.request;

import com.fullcar.carpool.domain.MoodType;
import com.fullcar.carpool.domain.PeriodType;
import com.fullcar.carpool.domain.carpool.MoodType;
import com.fullcar.carpool.domain.carpool.PeriodType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;


@Getter
Expand Down
Loading
Loading