Skip to content

Commit

Permalink
Merge pull request #99 from YAPP-Github/feat/ISSUE-91
Browse files Browse the repository at this point in the history
feat: Carpool delete API
  • Loading branch information
Park-Young-Hun authored Feb 20, 2024
2 parents 96846a4 + 0a62988 commit bfcdbbf
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,26 @@ public CarpoolResponseDto.CarpoolDetailDtO closeCarpool(Member member, CarpoolId
}
List<Form> forms = formRepository.findAllByCarpoolIdAndIsDeleted(carpoolId, false);

carpool.close();
carpool.close(forms);

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

} // TODO: N+1 문제 개선 필요.
return carpoolMapper.toDetailDto(carpool, member, car);
}

carpoolRepository.save(carpool);
formRepository.saveAllAndFlush(forms);
@Transactional
public CarpoolResponseDto deleteCarpool(Member member, CarpoolId carpoolId) {
Carpool carpool = carpoolRepository.findByCarpoolIdAndIsDeletedOrThrow(carpoolId, false);

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

carpool.delete(forms);

carpoolRepository.saveAndFlush(carpool);

return carpoolMapper.toDto(carpool, member);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fullcar.carpool.application.form;


import com.fullcar.carpool.domain.carpool.event.CarpoolClosedEvent;
import com.fullcar.carpool.domain.form.Form;
import com.fullcar.carpool.domain.form.FormRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class CarpoolClosedEventHandler {
private final FormRepository formRepository;

@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void rejectForms(CarpoolClosedEvent carpoolClosedEvent) {
log.info("CarpoolClosedEvent received");
List<Form> forms = carpoolClosedEvent.getForms();

forms.forEach(Form::reject);
formRepository.saveAllAndFlush(forms);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fullcar.carpool.application.form;

import com.fullcar.carpool.domain.carpool.event.CarpoolDeletedEvent;
import com.fullcar.carpool.domain.form.Form;
import com.fullcar.carpool.domain.form.FormRepository;
import com.fullcar.carpool.domain.form.FormState;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class CarpoolDeletedEventHandler {
private final FormRepository formRepository;

@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void rejectForms(CarpoolDeletedEvent carpoolDeletedEvent) {
log.info("CarpoolDeletedEvent received");
List<Form> forms = carpoolDeletedEvent.getForms();

for (Form form: forms) {
if (form.getFormState() == FormState.REQUEST || form.getFormState() == FormState.ACCEPT) {
form.reject();
}
form.disconnectCarpool();
}
formRepository.saveAllAndFlush(forms);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public FormResponseDto toDto(Form form, Member member) {
}

public FormResponseDto.FormDetailDto toDetailDto(Form form, Member member) {
Long carpoolId;

if (form.getCarpoolId() != null) {
carpoolId = form.getCarpoolId().getId();
}
else {
carpoolId = null;
}

return FormResponseDto.FormDetailDto.builder()
.id(form.getFormId().getId())
.pickupLocation(form.getPickupLocation())
Expand All @@ -42,7 +51,7 @@ public FormResponseDto.FormDetailDto toDetailDto(Form form, Member member) {
.createdAt(form.getCreatedAt())
.content(form.getContent())
.resultMessage(form.getResultMessage())
.carpoolId(form.getCarpoolId().getId())
.carpoolId(carpoolId)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ public FormResponseDto requestForm(Member member, CarpoolId carpoolId, FormReque
Form form = formMapper.toEntity(member, carpoolId, formRequestDto);

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

return formMapper.toDto(
Expand Down Expand Up @@ -114,14 +111,13 @@ public List<FormResponseDto> readReceivedFormList(Member member) {
@Transactional
public FormResponseDto.FormDetailDto updateForm(Member member, FormId formId, FormUpdateDto formUpdateDto) {
Form form = formRepository.findByFormIdAndIsDeletedOrThrow(formId, false);
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);
}

form.changeFormState(formUpdateDto, passenger);
form.changeFormState(formUpdateDto);

return formMapper.toDetailDto(
formRepository.saveAndFlush(form),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fullcar.carpool.application.form;

import com.fullcar.carpool.domain.form.event.FormStateChangedEvent;
import com.fullcar.carpool.domain.service.NotificationService;
import com.fullcar.carpool.infra.dto.NotificationDto;
import com.fullcar.member.domain.member.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Service
@RequiredArgsConstructor
public class FormStateChangedEventHandler {
private final NotificationService notificationService;
private final MemberRepository memberRepository;

@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void sendNotification(FormStateChangedEvent formStateChangedEvent) {
log.info("FormStateChangedEvent received");
notificationService.sendNotification(
NotificationDto.builder()
.member(
memberRepository.findByIdAndIsDeletedOrThrow(formStateChangedEvent.getMemberId(), false)
)
.title(formStateChangedEvent.getTitle())
.body(formStateChangedEvent.getBody())
.build()
);
log.info("Notification send");
}
}
33 changes: 31 additions & 2 deletions src/main/java/com/fullcar/carpool/domain/carpool/Carpool.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.fullcar.carpool.domain.carpool;

import com.fullcar.carpool.domain.carpool.event.CarpoolClosedEvent;
import com.fullcar.carpool.domain.carpool.event.CarpoolDeletedEvent;
import com.fullcar.carpool.domain.form.Form;
import com.fullcar.carpool.domain.form.FormState;
import com.fullcar.core.exception.CustomException;
import com.fullcar.core.response.ErrorCode;
import com.fullcar.member.domain.member.MemberId;
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;
import java.util.List;

import static lombok.AccessLevel.PROTECTED;

Expand All @@ -18,7 +26,7 @@
@AllArgsConstructor(access = PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@Table(name = "carpool")
public class Carpool {
public class Carpool extends AbstractAggregateRoot<Carpool> {

@EmbeddedId
private CarpoolId carpoolId;
Expand Down Expand Up @@ -59,7 +67,28 @@ public boolean isMyCarpool(MemberId memberId) {
return this.getDriver().getMemberId().getId().equals(memberId.getId());
}

public void close() {
public void close(List<Form> forms) {
if (this.carpoolState == CarpoolState.CLOSE) {
throw new CustomException(ErrorCode.ALREADY_CLOSED);
}
this.carpoolState = CarpoolState.CLOSE;
registerEvent(
new CarpoolClosedEvent(
forms.stream()
.filter(form -> form.getFormState() == FormState.REQUEST)
.toList()
)
);
}

public void delete(List<Form> forms) {
if (this.carpoolState == CarpoolState.OPEN) {
throw new CustomException(ErrorCode.CANNOT_DELETE_OPEN_CARPOOL);
}

this.isDeleted = true;
registerEvent(
new CarpoolDeletedEvent(forms)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fullcar.carpool.domain.carpool.event;

import com.fullcar.carpool.domain.form.Form;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class CarpoolClosedEvent {
private List<Form> forms;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fullcar.carpool.domain.carpool.event;

import com.fullcar.carpool.domain.form.Form;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class CarpoolDeletedEvent {
private List<Form> forms;
}
32 changes: 17 additions & 15 deletions src/main/java/com/fullcar/carpool/domain/form/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,50 +63,52 @@ public class Form extends AbstractAggregateRoot<Form> {
@LastModifiedDate
private LocalDateTime updatedAt;

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

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

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

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

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

registerEvent(new FormStateChangedEvent(
NotificationDto.builder()
.nickName(passenger.getNickname())
.deviceToken(passenger.getDeviceToken())
registerEvent(
FormStateChangedEvent.builder()
.memberId(this.passenger.getMemberId())
.title(FormMessage.REJECT_TITLE.getMessage())
.body(FormMessage.REJECT_BODY.getMessage())
.build()
));
);
}

public void disconnectCarpool() {
this.carpoolId = null;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.fullcar.carpool.domain.form.event;

import com.fullcar.carpool.infra.dto.NotificationDto;
import com.fullcar.member.domain.member.Member;
import com.fullcar.member.domain.member.MemberId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FormStateChangedEvent {
private NotificationDto notificationDto;
private MemberId memberId;

private String title;

private String body;
}
Loading

0 comments on commit bfcdbbf

Please sign in to comment.