|
| 1 | +package com.github.booking.application.booking; |
| 2 | + |
| 3 | +import com.github.booking.application.booking.command.CancelBookingCommand; |
| 4 | +import com.github.booking.application.booking.command.ConfirmBookingCommand; |
| 5 | +import com.github.booking.application.booking.command.InitiateBookingCommand; |
| 6 | +import com.github.booking.application.booking.command.InitiateBookingResult; |
| 7 | +import com.github.booking.application.booking.command.ReleaseSeatCommand; |
| 8 | +import com.github.booking.application.booking.command.ReserveSeatCommand; |
| 9 | +import com.github.booking.domain.booking.BookingException; |
| 10 | +import com.github.booking.domain.booking.BookingId; |
| 11 | +import com.github.booking.domain.booking.BookingRepository; |
| 12 | +import com.github.booking.domain.hall.SeatNumber; |
| 13 | +import com.github.booking.domain.show.ShowException; |
| 14 | +import com.github.booking.domain.show.ShowId; |
| 15 | +import com.github.booking.domain.show.ShowRepository; |
| 16 | +import org.springframework.stereotype.Service; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +@Service |
| 21 | +public class BookingCommandHandlerImpl implements BookingCommandHandler { |
| 22 | + |
| 23 | + private final BookingRepository bookingRepository; |
| 24 | + private final ShowRepository showRepository; |
| 25 | + |
| 26 | + public BookingCommandHandlerImpl(final BookingRepository bookingRepository, final ShowRepository showRepository) { |
| 27 | + this.bookingRepository = Objects.requireNonNull(bookingRepository); |
| 28 | + this.showRepository = Objects.requireNonNull(showRepository); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public InitiateBookingResult initiateBooking(final InitiateBookingCommand command) { |
| 33 | + final var show = showRepository.findByShowId(new ShowId(command.showId())) |
| 34 | + .orElseThrow(ShowException::notFound); |
| 35 | + final var booking = show.initiateBooking(bookingRepository.nextBookingId()); |
| 36 | + |
| 37 | + bookingRepository.save(booking); |
| 38 | + |
| 39 | + return new InitiateBookingResult(booking.bookingId() |
| 40 | + .value()); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void reserveSeat(final ReserveSeatCommand command) { |
| 45 | + final var booking = bookingRepository.findByBookingId(new BookingId(command.bookingId())) |
| 46 | + .orElseThrow(BookingException::notFound); |
| 47 | + final var show = showRepository.findByShowId(booking.showId()) |
| 48 | + .orElseThrow(ShowException::notFound); |
| 49 | + |
| 50 | + show.reserveSeat(booking, new SeatNumber(command.seatNumber())); |
| 51 | + showRepository.save(show); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void releaseSeat(final ReleaseSeatCommand command) { |
| 56 | + final var booking = bookingRepository.findByBookingId(new BookingId(command.bookingId())) |
| 57 | + .orElseThrow(BookingException::notFound); |
| 58 | + final var show = showRepository.findByShowId(booking.showId()) |
| 59 | + .orElseThrow(ShowException::notFound); |
| 60 | + |
| 61 | + show.releaseSeat(booking, new SeatNumber(command.seatNumber())); |
| 62 | + showRepository.save(show); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void confirmBooking(final ConfirmBookingCommand command) { |
| 67 | + final var booking = bookingRepository.findByBookingId(new BookingId(command.bookingId())) |
| 68 | + .orElseThrow(BookingException::notFound); |
| 69 | + |
| 70 | + booking.confirm(); |
| 71 | + bookingRepository.save(booking); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void cancelBooking(final CancelBookingCommand command) { |
| 76 | + final var booking = bookingRepository.findByBookingId(new BookingId(command.bookingId())) |
| 77 | + .orElseThrow(BookingException::notFound); |
| 78 | + |
| 79 | + booking.cancel(); |
| 80 | + bookingRepository.save(booking); |
| 81 | + } |
| 82 | +} |
0 commit comments