Exemplo n.º 1
0
        public async Task Add(Guid reservationId, Guid userId, ReservationOffer request)
        {
            var offer = await _reservationsQuerier.GetReservationOffer(request.OfferRequest);

            if (!offer.Equals(request))
            {
                throw new Exception("Offer has expired.");
            }

            if (AnyPlaceIsInvalid(request.OfferRequest.Places))
            {
                throw new Exception("Invalid place configuration.");
            }

            Show show = await _ctx.Shows.Include(s => s.Reservations).FirstOrDefaultAsync(s => s.Id == request.OfferRequest.ShowId);

            if (show is null)
            {
                throw new KeyNotFoundException(nameof(request.OfferRequest.ShowId));
            }

            User user = await _ctx.Users.FindAsync(userId);

            if (user is null)
            {
                throw new KeyNotFoundException(nameof(userId));
            }

            Reservation reservation = ReservationFactory.Create(reservationId, user);

            AddPlacesToReservation(request.OfferRequest.Places, reservation);
            show.AddReservation(reservation);

            await _ctx.SaveChangesAsync();

            _emailSender.Send(new Email()); //TODO
        }