Skip to content

Commit

Permalink
feat: Carpool close API
Browse files Browse the repository at this point in the history
  • Loading branch information
Park-Young-Hun committed Feb 14, 2024
1 parent 9ebd88e commit 18d8bcb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.fullcar.carpool.domain.carpool.Driver;
import com.fullcar.carpool.domain.form.Form;
import com.fullcar.carpool.domain.form.FormRepository;
import com.fullcar.carpool.domain.service.CarpoolCloseService;
import com.fullcar.carpool.domain.form.FormState;
import com.fullcar.carpool.presentation.carpool.dto.request.CarpoolRequestDto;
import com.fullcar.carpool.presentation.carpool.dto.response.CarpoolResponseDto;
import com.fullcar.carpool.presentation.carpool.dto.response.MyCarpoolDto;
Expand All @@ -16,6 +16,7 @@
import com.fullcar.member.domain.car.CarRepository;
import com.fullcar.member.domain.member.Member;

import com.fullcar.member.domain.member.MemberRepository;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.*;
Expand All @@ -32,9 +33,9 @@
public class CarpoolService {
private final CarpoolRepository carpoolRepository;
private final CarRepository carRepository; //:TODO Event 기반으로 변경
private final MemberRepository memberRepository; //:TODO Event 기반으로 변경
private final FormRepository formRepository;
private final CarpoolMapper carpoolMapper;
private final CarpoolCloseService carpoolCloseService;

@Transactional
public CarpoolResponseDto registerCarpool(Member member, CarpoolRequestDto carpoolRequestDto) {
Expand Down Expand Up @@ -86,17 +87,32 @@ public List<MyCarpoolDto> getMyCarpoolList(Member member) {
}

@Transactional
public CarpoolResponseDto closeCarpool(Member member, CarpoolId carpoolId) {
public CarpoolResponseDto.CarpoolDetailDtO closeCarpool(Member member, CarpoolId carpoolId) {
Carpool carpool = carpoolRepository.findByCarpoolIdAndIsDeletedOrThrow(carpoolId, false);
Car car = carRepository.findByCarIdAndIsDeletedOrThrow(member.getCarId(), false);

if (!carpool.isMyCarpool(member.getId())) {
throw new CustomException(ErrorCode.CANNOT_CLOSE_CARPOOL);
}
List<Form> forms = formRepository.findAllByCarpoolIdAndIsDeleted(carpoolId, false);

carpool = carpoolCloseService.closeCarpool(carpool);
forms = carpoolCloseService.rejectForms(forms);
carpool.close();

return carpoolMapper.toDto(carpool, member);
for (Form form: forms) {
if (form.getFormState() == FormState.REQUEST) {
form.reject(
memberRepository.findByIdAndIsDeletedOrThrow(
form.getPassenger().getMemberId(),
false
)
);
}

} // TODO: N+1 문제 개선 필요.

carpoolRepository.save(carpool);
formRepository.saveAllAndFlush(forms);

return carpoolMapper.toDetailDto(carpool, member, car);
}
}
32 changes: 16 additions & 16 deletions src/main/java/com/fullcar/carpool/application/form/FormService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import com.fullcar.carpool.domain.carpool.CarpoolRepository;
import com.fullcar.carpool.domain.carpool.CarpoolState;
import com.fullcar.carpool.domain.form.*;
import com.fullcar.carpool.domain.form.event.FormStateChangedEvent;
import com.fullcar.carpool.domain.service.NotificationService;
import com.fullcar.carpool.infra.NotificationClient;
import com.fullcar.carpool.infra.dto.NotificationDto;
import com.fullcar.carpool.presentation.form.dto.request.FormRequestDto;
import com.fullcar.carpool.presentation.form.dto.request.FormUpdateDto;
import com.fullcar.carpool.presentation.form.dto.response.FormResponseDto;
Expand All @@ -16,6 +17,7 @@
import com.fullcar.member.domain.member.MemberRepository;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
Expand All @@ -32,6 +34,7 @@ public class FormService {
private final MemberRepository memberRepository; //TODO: Event 기반으로 변경 필요.
private final FormMapper formMapper;
private final NotificationService notificationService;
private final ApplicationEventPublisher eventPublisher;

@Transactional
public FormResponseDto requestForm(Member member, CarpoolId carpoolId, FormRequestDto formRequestDto) {
Expand Down Expand Up @@ -59,7 +62,17 @@ public FormResponseDto requestForm(Member member, CarpoolId carpoolId, FormReque
}

Form form = formMapper.toEntity(member, carpoolId, formRequestDto);
notificationService.sendNotification(driver.getNickname(), driver.getDeviceToken(), "탑승 요청이 들어왔어요!", "탑승자 정보를 확인하고 승인해 주세요🚘");

eventPublisher.publishEvent(
new FormStateChangedEvent(
NotificationDto.builder()
.nickName(driver.getNickname())
.deviceToken(driver.getDeviceToken())
.title(FormMessage.REQUEST_TITLE.getMessage())
.body(FormMessage.REQUEST_BODY.getMessage())
.build()
)
);

return formMapper.toDto(
formRepository.saveAndFlush(form),
Expand Down Expand Up @@ -101,27 +114,14 @@ public List<FormResponseDto> readReceivedFormList(Member member) {
@Transactional
public FormResponseDto.FormDetailDto updateForm(Member member, FormId formId, FormUpdateDto formUpdateDto) {
Form form = formRepository.findByFormIdAndIsDeletedOrThrow(formId, false);
form.changeFormState(formUpdateDto);

Member passenger = memberRepository.findByIdAndIsDeletedOrThrow(form.getPassenger().getMemberId(), false);
Carpool carpool = carpoolRepository.findByCarpoolIdAndIsDeletedOrThrow(form.getCarpoolId(), false);

if (!carpool.isMyCarpool(member.getId())) {
throw new CustomException(ErrorCode.CANNOT_CHANGE_FORM_STATE);
}

System.out.println(passenger.getDeviceToken());

if (formUpdateDto.getFormState() == FormState.ACCEPT) {
String title = "카풀 매칭에 성공했어요!";
String body = "운전자 정보를 확인해 주세요🚘";
notificationService.sendNotification(passenger.getNickname(), passenger.getDeviceToken(), title, body);
}
else if (formUpdateDto.getFormState() == FormState.REJECT) {
String title = "카풀 매칭에 실패했어요.";
String body = "다른 카풀을 찾아볼까요?💁🏻‍♀️";
notificationService.sendNotification(passenger.getNickname(), passenger.getDeviceToken(), title, body);
}
form.changeFormState(formUpdateDto, passenger);

return formMapper.toDetailDto(
formRepository.saveAndFlush(form),
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/com/fullcar/carpool/domain/form/Form.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.fullcar.carpool.domain.form;

import com.fullcar.carpool.domain.carpool.CarpoolId;
import com.fullcar.carpool.domain.form.event.FormStateChangedEvent;
import com.fullcar.carpool.infra.dto.NotificationDto;
import com.fullcar.carpool.presentation.form.dto.request.FormUpdateDto;
import com.fullcar.core.exception.CustomException;
import com.fullcar.core.response.ErrorCode;
import com.fullcar.member.domain.member.Member;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
Expand All @@ -19,8 +23,7 @@
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@Table(name = "form")
public class Form {
private static final String REJECT_MESSAGE = "카풀 매칭에 실패했어요. 다른 카풀을 찾아보세요!";
public class Form extends AbstractAggregateRoot<Form> {

@EmbeddedId
private FormId formId;
Expand Down Expand Up @@ -60,32 +63,50 @@ public class Form {
@LastModifiedDate
private LocalDateTime updatedAt;

public void changeFormState(FormUpdateDto formUpdateDto) {
public void changeFormState(FormUpdateDto formUpdateDto, Member passenger) {
FormState formState = formUpdateDto.getFormState();

if (formState == FormState.ACCEPT) {
this.accept(formUpdateDto.getContact(), formUpdateDto.getToPassenger());
this.accept(formUpdateDto.getContact(), formUpdateDto.getToPassenger(), passenger);
}
else if (formState == FormState.REJECT) {
this.reject();
this.reject(passenger);
}
else {
throw new CustomException(ErrorCode.INVALID_FORM_STATE);
}
}

public void accept(String contact, String toPassenger) {
public void accept(String contact, String toPassenger, Member passenger) {
this.formState = FormState.ACCEPT;
this.resultMessage = ResultMessage.builder()
.contact(contact)
.toPassenger(toPassenger)
.build();

registerEvent(new FormStateChangedEvent(
NotificationDto.builder()
.nickName(passenger.getNickname())
.deviceToken(passenger.getDeviceToken())
.title(FormMessage.ACCEPT_TITLE.getMessage())
.body(FormMessage.ACCEPT_BODY.getMessage())
.build()
));
}

public void reject() {
public void reject(Member passenger) {
this.formState = FormState.REJECT;
this.resultMessage = ResultMessage.builder()
.toPassenger(REJECT_MESSAGE)
.toPassenger(FormMessage.REJECT_MESSAGE.getMessage())
.build();

registerEvent(new FormStateChangedEvent(
NotificationDto.builder()
.nickName(passenger.getNickname())
.deviceToken(passenger.getDeviceToken())
.title(FormMessage.REJECT_TITLE.getMessage())
.body(FormMessage.REJECT_BODY.getMessage())
.build()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fullcar.carpool.domain.form.event;

import com.fullcar.carpool.infra.dto.NotificationDto;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class FormStateChangedEvent {
private NotificationDto notificationDto;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,20 @@ public ApiResponse<List<MyCarpoolDto>> getMyCarpools(
carpoolService.getMyCarpoolList(member)
);
}

@Operation(summary = "카풀 마감 API")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "수정 성공")
})
@PatchMapping("/carpools/{carpoolId}")
public ApiResponse<CarpoolResponseDto.CarpoolDetailDtO> patchCarpool(
@Parameter(hidden = true)
@CurrentMember Member member,
@PathVariable Long carpoolId
) {
return ApiResponse.success(
SuccessCode.UPDATE_SUCCESS,
carpoolService.closeCarpool(member, new CarpoolId(carpoolId))
);
}
}

0 comments on commit 18d8bcb

Please sign in to comment.