Skip to content

Commit

Permalink
fix: increaseApplicationCount() 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
suw0n committed Jul 16, 2024
1 parent 19c3558 commit efd7512
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public Response modifyApplication(@PathVariable int id) {

@DeleteMapping("/apply/{id}")
public Response cancelApplication(@PathVariable int id) {
return busApplicationUseCase.cancel(id);
return busApplicationUseCase.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
import b1nd.dodamcore.bus.domain.entity.Bus;
import b1nd.dodamcore.bus.domain.entity.BusMember;
import b1nd.dodamcore.bus.domain.exception.BusAlreadyApplyException;
import b1nd.dodamcore.bus.domain.exception.BusMemberNotFoundException;
import b1nd.dodamcore.common.util.ZonedDateTimeUtil;
import b1nd.dodamcore.member.application.MemberService;
import b1nd.dodamcore.member.domain.entity.Student;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.function.Consumer;


@Component
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
Expand All @@ -28,58 +22,41 @@ public class BusApplicationUseCase {
private final MemberService memberService;

public Response apply(int busId) {
Bus bus = busService.getByIdForUpdate(busId);
Student student = memberService.getStudentFromSession();
if(busApplicationService.hasValidApplication(student, ZonedDateTimeUtil.nowToLocalDateTime())) {
if(busApplicationService.hasMy(student)) {
throw new BusAlreadyApplyException();
}
busApplicationService.save(
BusMember.builder()
.bus(bus)
.student(student)
.build()
busApplicationService.save(BusMember.builder()
.bus(increaseBusApplicationCount(busId))
.student(student)
.build()
);
return Response.created("버스 신청 성공");
}

public Response modify(int busId) {
Bus newBus = busService.getByIdForUpdate(busId);
Student student = memberService.getStudentFromSession();
findValidApplication(
student,
ZonedDateTimeUtil.nowToLocalDateTime(),
(busMember) -> {
Bus oldBus = busService.getByIdForUpdate(busMember.getBus().getId());
oldBus.decreaseApplyCount();
busMember.modifyBus(newBus);
newBus.increaseApplyCount();
}
);
public Response modify(int newBusId) {
BusMember application = busApplicationService.getMy(memberService.getStudentFromSession());
decreaseBusApplicationCount(application.getBus().getId());
application.modifyBus(increaseBusApplicationCount(newBusId));
return Response.noContent("버스 신청 수정 성공");
}

public Response cancel(int busId) {
Student student = memberService.getStudentFromSession();
findValidApplication(
student,
ZonedDateTimeUtil.nowToLocalDateTime(),
(busMember) -> {
Bus bus = busService.getByIdForUpdate(busId);
bus.decreaseApplyCount();
busApplicationService.delete(busMember);
}
);
public Response cancel() {
BusMember application = busApplicationService.getMy(memberService.getStudentFromSession());
decreaseBusApplicationCount(application.getBus().getId());
busApplicationService.delete(application);
return Response.noContent("버스 신청 취소 성공");
}

private void findValidApplication(Student student, LocalDateTime now, Consumer<? super BusMember> action) {
busApplicationService.findValidApplication(student, now)
.ifPresentOrElse(
action,
() -> {
throw new BusMemberNotFoundException();
}
);
private Bus increaseBusApplicationCount(int busId) {
Bus bus = busService.getByIdForUpdate(busId);
bus.increaseApplyCount();
return bus;
}

private void decreaseBusApplicationCount(int busId) {
Bus bus = busService.getByIdForUpdate(busId);
bus.decreaseApplyCount();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import b1nd.dodamcore.bus.domain.entity.Bus;
import b1nd.dodamcore.bus.domain.entity.BusMember;
import b1nd.dodamcore.bus.domain.exception.BusMemberNotFoundException;
import b1nd.dodamcore.bus.repository.BusMemberRepository;
import b1nd.dodamcore.common.util.ZonedDateTimeUtil;
import b1nd.dodamcore.member.domain.entity.Student;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -25,12 +25,13 @@ public void delete(BusMember busMember) {
repository.delete(busMember);
}

public Optional<BusMember> findValidApplication(Student student, LocalDateTime now) {
return repository.findByStudentAndBus_LeaveTimeAfter(student, now);
public BusMember getMy(Student student) {
return repository.findByStudentAndBus_LeaveTimeAfter(student, ZonedDateTimeUtil.nowToLocalDateTime())
.orElseThrow(BusMemberNotFoundException::new);
}

public boolean hasValidApplication(Student student, LocalDateTime now) {
return repository.existsByStudentAndBus_LeaveTimeAfter(student, now);
public boolean hasMy(Student student) {
return repository.existsByStudentAndBus_LeaveTimeAfter(student, ZonedDateTimeUtil.nowToLocalDateTime());
}

public List<BusMember> getByBus(Bus bus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void increaseApplyCount() {
}

public void decreaseApplyCount() {
this.applyCount -= 1;
this.applyCount = Math.max(0, applyCount - 1);
}

public boolean isFullOfSeat() {
Expand Down

0 comments on commit efd7512

Please sign in to comment.