public async Task <ActionResult <TOFlight> > DeleteFlight(int id, [FromQuery] int version) { 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")); } var flight = await _context.Flights.Include(flight => flight.Seats).FirstOrDefaultAsync(flight => flight.FlightId == id); if (flight == null) { return(NotFound()); } bool success = false; if (flight.Version != version) { return(Ok(new { success })); } foreach (AirlineMicroservice.Models.Seat seat in flight.Seats) { var fastTicket = await _context.FastTickets.FindAsync(seat.SeatId); if (fastTicket != null) { _context.FastTickets.Remove(fastTicket); } } AirlineMicroservice.TOModels.TOFlight toFlight = new AirlineMicroservice.TOModels.TOFlight(flight); _context.Flights.Remove(flight); await _context.SaveChangesAsync(); success = true; return(Ok(new { success })); }
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))); }