public Airline(TOAirline airline) { Name = airline.Name; Address = airline.Address; Description = airline.Description; Image = airline.Image; Rating = airline.Rating; }
public async Task <ActionResult <TOAirline> > PostAirline(TOAirline airline) { string role = User.Claims.First(c => c.Type == "Roles").Value; if (role != "aeroAdminNew") { return(BadRequest("You are not authorised to do this action")); } Airline tempAirline = new Airline(airline); _context.Airlines.Add(tempAirline); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (AirlineExists(tempAirline.Name)) { return(Conflict()); } else { throw; } } tempAirline.Destinations = new List <Destination>(); foreach (var destination in airline.Destinations) { tempAirline.Destinations.Add(new Destination() { Airline = tempAirline, DestinationId = destination.Id, Value = destination.Value.ToString() });; } tempAirline.Prices = new List <Price>(); foreach (var price in airline.Prices) { tempAirline.Prices.Add(new Price() { Airline = tempAirline, PriceId = price.Id, Ordinal = price.Ordinal, Value = double.Parse(price.Value.ToString()) }); } tempAirline.SegmentLengths = new List <Segment>(); foreach (var segment in airline.SegmentLengths) { tempAirline.SegmentLengths.Add(new Segment() { Airline = tempAirline, SegmentId = segment.Id, Ordinal = segment.Ordinal, Value = int.Parse(segment.Value.ToString()) }); } tempAirline.SeatingArrangements = new List <SeatArrangement>(); foreach (var seatArrangement in airline.SeatingArrangements) { tempAirline.SeatingArrangements.Add(new SeatArrangement() { Airline = tempAirline, SeatArrangementId = seatArrangement.Id, Ordinal = seatArrangement.Ordinal, Value = int.Parse(seatArrangement.Value.ToString()) }); } _context.Entry(tempAirline).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> PutAirline(string id, TOAirline airline) { 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 != airline.Name) { return(BadRequest()); } Airline tempAirline = new Airline(airline); Airline oldAirline = await _context.Airlines .Include(c => c.Destinations) .Include(c => c.SeatingArrangements) .Include(c => c.SegmentLengths) .Include(c => c.Prices) .Include(c => c.Flights).ThenInclude(c => c.Seats) .FirstOrDefaultAsync(c => c.Name == airline.Name); _context.Entry(oldAirline).CurrentValues.SetValues(tempAirline); #region destinations tempAirline.Destinations = new List <Destination>(); foreach (var destination in airline.Destinations) { tempAirline.Destinations.Add(new Destination() { Airline = tempAirline, DestinationId = destination.Id, Value = destination.Value.ToString() }); } var destinations = oldAirline.Destinations.ToList(); foreach (var destination in destinations) { var des = tempAirline.Destinations.SingleOrDefault(i => i.DestinationId == destination.DestinationId); if (des != null) { _context.Entry(destination).CurrentValues.SetValues(des); } else { _context.Remove(destination); } } foreach (var des in tempAirline.Destinations) { if (destinations.All(i => i.DestinationId != des.DestinationId)) { oldAirline.Destinations.Add(des); } } #endregion #region prices tempAirline.Prices = new List <Price>(); foreach (var price in airline.Prices) { tempAirline.Prices.Add(new Price() { Airline = tempAirline, PriceId = price.Id, Value = double.Parse(price.Value.ToString()), Ordinal = price.Ordinal }); } var prices = oldAirline.Prices.ToList(); foreach (var price in prices) { var pri = tempAirline.Prices.SingleOrDefault(i => i.PriceId == price.PriceId); if (pri != null) { _context.Entry(price).CurrentValues.SetValues(pri); } else { _context.Remove(price); } } foreach (var pri in tempAirline.Prices) { if (prices.All(i => i.PriceId != pri.PriceId)) { oldAirline.Prices.Add(pri); } } foreach (Flight flight in oldAirline.Flights) { if (flight.Departure > DateTime.Now) { foreach (Seat seat in flight.Seats) { if (!seat.Occupied) { switch (seat.Type) { case "first": seat.Price = airline.Prices[0].Value * flight.Distance; break; case "business": seat.Price = airline.Prices[1].Value * flight.Distance; break; case "economy": seat.Price = airline.Prices[2].Value * flight.Distance; break; } _context.Entry(seat).State = EntityState.Modified; } } } } await _context.SaveChangesAsync(); #endregion region #region segmentLength tempAirline.SegmentLengths = new List <Segment>(); foreach (var segment in airline.SegmentLengths) { tempAirline.SegmentLengths.Add(new Segment() { Airline = tempAirline, SegmentId = segment.Id, Value = int.Parse(segment.Value.ToString()), Ordinal = segment.Ordinal }); } var segmentLengths = oldAirline.SegmentLengths.ToList(); foreach (var segmentLength in segmentLengths) { var segLen = tempAirline.SegmentLengths.SingleOrDefault(i => i.SegmentId == segmentLength.SegmentId); if (segLen != null) { _context.Entry(segmentLength).CurrentValues.SetValues(segLen); } else { _context.Remove(segmentLength); } } foreach (var segLen in tempAirline.SegmentLengths) { if (segmentLengths.All(i => i.SegmentId != segLen.SegmentId)) { oldAirline.SegmentLengths.Add(segLen); } } #endregion #region Seat tempAirline.SeatingArrangements = new List <SeatArrangement>(); foreach (var seatArrangement in airline.SeatingArrangements) { tempAirline.SeatingArrangements.Add(new SeatArrangement() { Airline = tempAirline, SeatArrangementId = seatArrangement.Id, Value = int.Parse(seatArrangement.Value.ToString()), Ordinal = seatArrangement.Ordinal }); } var seats = oldAirline.SeatingArrangements.ToList(); foreach (var seat in seats) { var se = tempAirline.SeatingArrangements.SingleOrDefault(i => i.SeatArrangementId == seat.SeatArrangementId); if (se != null) { _context.Entry(seat).CurrentValues.SetValues(se); } else { _context.Remove(seat); } } foreach (var se in tempAirline.SeatingArrangements) { if (seats.All(i => i.SeatArrangementId != se.SeatArrangementId)) { oldAirline.SeatingArrangements.Add(se); } } #endregion try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AirlineExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }