Beispiel #1
0
        public async Task <IActionResult> GetFlightTakenSeats(long id)
        {
            if (ModelState.IsValid)
            {
                var user = await UserService.GetCurrentUser();

                if (user != null)
                {
                    var flight = await FlightService.GetFlight(id);

                    if (flight != null)
                    {
                        var aeroplane = await AeroplaneService.GetAeroplane(flight.AeroplaneId);

                        if (aeroplane != null)
                        {
                            var takenSeats = await ReservationService.GetTakenSeats(flight, aeroplane);

                            return(Ok(new { takenSeats }));
                        }
                    }
                }
            }

            return(BadRequest(ModelState));
        }
Beispiel #2
0
        public async Task <IActionResult> CreateFlight([FromBody] FlightDTO model)
        {
            if (ModelState.IsValid)
            {
                if (model.Departure > model.Arrival)
                {
                    return(BadRequest("Departure cannot be before landing."));
                }

                if (model.Departure < DateTime.Now)
                {
                    return(BadRequest("Departure cannot be before today's date."));
                }

                var user = await AvioAdminService.GetCurrentUser();

                if (user != null)
                {
                    var avioCompany = await AvioService.GetCompany(user.AvioCompanyId);

                    if (avioCompany != null)
                    {
                        model.DepartureLocation.DestinationId = 0;
                        model.ArrivalLocation.DestinationId   = 0;
                        foreach (Destination d in model.Transit)
                        {
                            d.DestinationId = 0;
                        }

                        var flight = new Flight()
                        {
                            AeroplaneId       = (await AeroplaneService.GetAeroplane(user.AvioCompanyId, model.Aeroplane)).AeroplaneId,
                            Arrival           = model.Arrival,
                            ArrivalLocation   = model.ArrivalLocation,
                            Departure         = model.Departure,
                            DepartureLocation = model.DepartureLocation,
                            AvioCompanyId     = user.AvioCompanyId,
                            Price             = model.Price,
                            Transit           = model.Transit,
                            TravelDistance    = model.TravelDistance,
                            TravelTime        = model.TravelTime
                        };

                        avioCompany.Flights.Add(flight);
                        await AvioService.UpdateCompany(avioCompany);

                        return(Ok(200));
                    }

                    return(BadRequest("Avio company not found."));
                }

                return(Unauthorized("You must log in as an administrator of this company."));
            }

            return(BadRequest("Not enough data provided."));
        }