예제 #1
0
        public async Task <ActionResult <AirlineMicroservice.TOModels.TOFlight> > PostFlight(AirlineMicroservice.TOModels.TOFlight flight)
        {
            string role = User.Claims.First(c => c.Type == "Roles").Value;

            if (role != "aeroAdminNew" && role != "aeroAdmin")
            {
                return(BadRequest("You are not authorised to do this action"));
            }

            AirlineMicroservice.Models.Flight tempFlight = new AirlineMicroservice.Models.Flight(flight, _context);
            tempFlight.Version = 0;
            _context.Flights.Add(tempFlight);
            await _context.SaveChangesAsync();

            tempFlight.Connections = new List <AirlineMicroservice.Models.Connection>();
            foreach (AirlineMicroservice.TOModels.TOPrimaryObject connection in flight.Connections)
            {
                tempFlight.Connections.Add(new AirlineMicroservice.Models.Connection()
                {
                    ConntectionId = 0,
                    Flight        = tempFlight,
                    Value         = connection.Value.ToString()
                });
            }

            tempFlight.GenerateSeats();

            tempFlight.SegmentLengths = new List <AirlineMicroservice.Models.SegmentFlight>();
            foreach (var segment in tempFlight.Airline.SegmentLengths)
            {
                tempFlight.SegmentLengths.Add(new AirlineMicroservice.Models.SegmentFlight()
                {
                    Flight          = tempFlight,
                    SegmentFlightId = 0,
                    Ordinal         = segment.Ordinal,
                    Value           = int.Parse(segment.Value.ToString())
                });
            }

            tempFlight.SeatingArrangements = new List <AirlineMicroservice.Models.SeatArrangementFlight>();
            foreach (var seatArrangement in tempFlight.Airline.SeatingArrangements)
            {
                tempFlight.SeatingArrangements.Add(new AirlineMicroservice.Models.SeatArrangementFlight()
                {
                    Flight = tempFlight,
                    SeatArrangementFlightId = 0,
                    Ordinal = seatArrangement.Ordinal,
                    Value   = int.Parse(seatArrangement.Value.ToString())
                });
            }

            _context.Entry(tempFlight).State = EntityState.Modified;

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetFlight", new { id = tempFlight.FlightId }, new AirlineMicroservice.TOModels.TOFlight(tempFlight)));
        }
예제 #2
0
        public async Task <IActionResult> PutFlight(int id, TOFlight flight)
        {
            string role = User.Claims.First(c => c.Type == "Roles").Value;

            if (role != "aeroAdmin" && role != "aeroAdminNew")
            {
                return(BadRequest("You are not authorised to do this action"));
            }

            if (id != flight.FlightId)
            {
                return(BadRequest());
            }

            AirlineMicroservice.Models.Flight tempFlight = await _context.Flights.FindAsync(flight.FlightId);

            var success = false;

            if (tempFlight.Version != flight.Version)
            {
                return(Ok(new { success }));
            }

            tempFlight.Arrival   = DateTime.Parse(flight.Arrival);
            tempFlight.Departure = DateTime.Parse(flight.Departure);
            tempFlight.Version++;

            _context.Entry(tempFlight).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FlightExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            success = true;
            return(Ok(new { success }));
        }