public async Task <ReservationDto> InsertAsync(ReservationUpsertRequest req) { List <string> screeningIncludes = new List <string> { nameof(Screening.Pricing), nameof(Screening.Movie) }; Screening screening = await _unit.Screenings.GetAsync(req.ScreeningId, screeningIncludes); ApplicationUserDto currentUser = await _authService.GetCurrentUserAsync(); IEnumerable <int> selectedSeatIds = req.SelectedSeats; await ValidateRequestAsync(req, screening, selectedSeatIds); Reservation reservation = new Reservation { UserId = currentUser.Id, Screening = screening, IsCancelled = false, TicketQuantity = req.SelectedSeats.Count, ReservationCode = GenerateReservationCode() }; Invoice invoice = CreateInvoice(reservation, screening.PricingId, req.SelectedSeats.Count); foreach (var selectedSeatId in req.SelectedSeats) { SeatReservation seatReservation = new SeatReservation { Reservation = reservation, SeatId = selectedSeatId }; reservation.SeatReservations.Add(seatReservation); } reservation.Invoice = invoice; await _reservationRepo.InsertAsync(reservation); await _unit.SaveAsync(); ReservationDto reservationDto = _mapper.Map <ReservationDto>(reservation); reservationDto.User = null; reservationDto.Invoice = null; reservationDto.Screening = null; await SendReservationAsync(screening, currentUser, reservation); return(reservationDto); }
public async Task <ReservationDto> CheckoutReservation() { if (NewReservation.Screening == null || NewReservation.SelectedSeats.Count == 0) { return(null); } ReservationUpsertRequest request = new ReservationUpsertRequest { ScreeningId = NewReservation.Screening.Id, SelectedSeats = NewReservation.SelectedSeats?.Select(x => x.SeatingModel.Seat.Id).ToList() }; var result = await _reservationsApi.Insert <ReservationDto>(request); return(result); }
private async Task ValidateRequestAsync(ReservationUpsertRequest req, Screening screening, IEnumerable <int> selectedSeatIds) { if (screening == null) { throw new ArgumentNullException($"{nameof(Screening)} with Id {req.ScreeningId} not found."); } if (screening.DateAndTime < DateTime.UtcNow) { throw new ScreeningAvailabilityException(ValidationMessages.DATE_NOT_FUTURE); } if (req.SelectedSeats == null || req.SelectedSeats.Count == 0) { throw new ScreeningAvailabilityException(ValidationMessages.NO_SEATS_SELECTED); } bool areAllSeatsFree = await _screeningService.AreSeatsFreeAsync(req.ScreeningId, selectedSeatIds); if (!areAllSeatsFree) { throw new ScreeningAvailabilityException(ValidationMessages.ALL_SEATS_NOT_FREE); } }
public Task <ReservationDto> UpdateAsync(int id, ReservationUpsertRequest req) { throw new System.NotImplementedException(); }