public ActionResult AddorEdit(int id = 0)
 {
     if (id == 0)
     {
         return(View(new Passenger()
         {
             Id = 0
         }));
     }
     else
     {
         var passenger = _passengerRepository.GetPassenger(id);
         return(View(passenger));
     }
 }
Esempio n. 2
0
        public async Task <ActionResult <Passenger> > Get(int id)
        {
            Passenger passenger = await _passengerRepository.GetPassenger(id);

            return(Ok(passenger));
        }
Esempio n. 3
0
 public IHttpActionResult Get(int id)
 {
     return(Ok(_passengerRepository.GetPassenger(id)));
 }
 public PassengerView GetPassenger(int?Id)
 {
     return(_PassengerRepository.GetPassenger(Id));
 }
        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));
        }
Esempio n. 6
0
 public Passenger GetPassenger(int id)
 {
     return(_passengerRepository.GetPassenger(id));
 }