public static void AddFlight(FlightForm flight, int avioId) { using (var _context = new DiemServiceDB()) { if (flight.FlightClass == 0 || flight.seats == 0 || flight.Flight_Arrival_Time == null || flight.Flight_Departure_Time == null || flight.fromLocation == null || flight.toLocation == null || flight.price == null || flight.Flight_Departure_Time < DateTime.Now || flight.Flight_Departure_Time.Date > flight.Flight_Arrival_Time.Date ) { throw new Exception("BAD QEURY"); } string caller = ((ClaimsPrincipal)HttpContext.Current.User).FindFirst("username").Value; User loggedUser = _context.UserDbSet.Where(u => u.Username == caller).FirstOrDefault(); AvioCompany found = _context.AvioCompanyDbSet.Where(u => u.Id == avioId).Include(x => x.Owner).FirstOrDefault(); if (loggedUser.Role != Role.Admin && loggedUser.Username != found.Owner.Username) { return; } Flight toAdd = flight.toFlight(); toAdd.To_Location = _context.LocationDbSet.Add(toAdd.To_Location); toAdd.From_Location = _context.LocationDbSet.Add(toAdd.From_Location); toAdd.Provider = found; found.Flights.Add(_context.FlightDbSet.Add(toAdd)); _context.SaveChanges(); } }
public static void AvioAddFlight(FlightForm flight) { using (var _context = new DiemServiceDB()) { Flight toAdd = flight.toFlight(); toAdd.To_Location = _context.LocationDbSet.Add(toAdd.To_Location); toAdd.From_Location = _context.LocationDbSet.Add(toAdd.From_Location); _context.FlightDbSet.Add(toAdd); _context.SaveChanges(); } }