Esempio n. 1
0
        public async Task <IActionResult> GetAvailableSeats(int flightId)
        {
            var passengerCount = await _bookingRepository.GetNumberOfPassengersForFlight(flightId);

            if (passengerCount < 0)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }

            var seatCount = await _airplaneRepository.GetNumberOfSeatsForFlight(flightId);

            if (seatCount < 0)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }

            var dto = new SeatCountForFlightDto
            {
                FlightId  = flightId,
                SeatCount = seatCount - passengerCount
            };

            return(Ok(dto));
        }
        public async Task <IActionResult> Add(BookingForAddDto bookingForAdd)
        {
            if (!int.TryParse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty, out var userId))
            {
                return(Unauthorized());
            }

            var numberOfPassengers = await _bookingRepository.GetNumberOfPassengersForFlight(bookingForAdd.FlightId);

            var availableSeats =
                await _airplaneRepository.GetNumberOfSeatsForFlight(bookingForAdd.FlightId) - numberOfPassengers;

            if (bookingForAdd.Passengers.Count() > availableSeats)
            {
                return(BadRequest("There are no more seats available for this flight."));
            }

            var bookingToAdd = new Booking
            {
                UserId             = userId,
                FlightId           = bookingForAdd.FlightId,
                NumberOfPassengers = bookingForAdd.Passengers.Count(),
                DateOfBooking      = DateTime.UtcNow,
                IsCancelled        = false
            };

            var bookingResult = await _bookingRepository.Add(bookingToAdd);

            if (bookingResult == null)
            {
                return(BadRequest("There was an error while processing Your request."));
            }

            var newPassengers      = new List <Passenger>();
            var passengersToUpdate = new List <Passenger>();

            foreach (var dto in bookingForAdd.Passengers)
            {
                var passengerResult = await _passengerRepository.GetPassenger(dto);

                if (passengerResult == null)
                {
                    newPassengers.Add(_mapper.Map <Passenger>(dto));
                    continue;
                }

                var passengerToUpdate = _mapper.Map <Passenger>(dto);
                passengerToUpdate.Id = passengerResult.Id;
                passengersToUpdate.Add(passengerToUpdate);
            }

            var added = await _passengerRepository.AddPassengers(newPassengers);

            var updated = await _passengerRepository.UpdatePassengers(passengersToUpdate);

            if (added == null || updated == null)
            {
                return(BadRequest("There was an error while processing Your request."));
            }

            var passengerBookings = added.Select(passenger => new PassengerBooking
            {
                BookingId = bookingResult.Id, PassengerId = passenger.Id,
            }).ToList();

            passengerBookings.AddRange(updated.Select(passenger => new PassengerBooking
            {
                BookingId = bookingResult.Id, PassengerId = passenger.Id
            }));

            var passengerBookingResult = await _passengerRepository.AddPassengerBookings(passengerBookings);

            if (!passengerBookingResult)
            {
                return(BadRequest("There was an error while processing Your request."));
            }

            var flight = await _flightRepository.GetById(bookingForAdd.FlightId);

            if (flight == null)
            {
                return(BadRequest("There was an error while processing Your request."));
            }

            var paymentResult = await _paymentRepository
                                .Add(flight.PricePerPassenger, bookingForAdd.Passengers.Count(), bookingResult.Id);

            if (paymentResult == null)
            {
                return(BadRequest("There was an error while processing Your request."));
            }

            var paymentDto = _mapper.Map <PaymentDto>(paymentResult);

            paymentDto.Iban  = Constants.Iban;
            paymentDto.Swift = Constants.Swift;

            var user = await _userRepository.GetUserById(userId);

            if (user == null)
            {
                return(Ok("There was an error while processing Your request."));
            }

            var flightDetails = _flightRepository.GetDetailsById(bookingForAdd.FlightId);

            if (flightDetails == null)
            {
                return(Ok("There was an error while processing Your request."));
            }

            var emailResult = await SendEmail(user.Email, user.Username,
                                              paymentDto.ReferenceNumber, paymentDto.Iban, paymentDto.Swift, paymentDto.Amount, flightDetails.Result);

            if (!emailResult)
            {
                return(Ok("There was an error while processing Your request."));
            }

            return(Ok(paymentDto));
        }