Exemplo n.º 1
0
        public async Task <IActionResult> UpdateFlight(int fid, [FromBody] FlightViewModel flight)
        {
            try
            {
                var flightToUpdate = await FlightService.GetFlightByIdAsync(fid);

                if (flightToUpdate == null)
                {
                    return(NotFound());
                }

                if (flightToUpdate.GateId != flight.GateId)
                {
                    var gate = await FlightService.GetFlightsByGateIdAsync(flight.GateId);

                    if (!FlightService.MoveFlightToGate(flightToUpdate, gate))
                    {
                        return(StatusCode(500, "cannot move flight."));
                    }
                }
                if (flightToUpdate.IsCancel != flight.IsCancel)
                {
                    FlightService.CancelFlight(flightToUpdate, flight.IsCancel);
                }
                else
                {
                    var flightClone = new Flight
                    {
                        Arrival      = DateTime.Parse(flight.Arrival),
                        Departure    = DateTime.Parse(flight.Departure),
                        Id           = flightToUpdate.Id,
                        GateId       = flightToUpdate.GateId,
                        Description  = flight.Description,
                        FlightNumber = flight.FlightNumber,
                        IsCancel     = flight.IsCancel
                    };
                    if (!FlightService.UpdateFlightTime(flightClone))
                    {
                        return(StatusCode(500, "cannot save flight, check parameters are set correctly."));
                    }
                }
                return(new NoContentResult());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }