public static string DisplayDetailsForSeatsInFlight(Flight flight) { StringBuilder builder = new StringBuilder(); using (var context = new ABS()) { // Gets the seats for a given flight. var query = context.Seats.Where(s => s.FlightId == flight.FlightId); // Foreach First Buisiness Economy. foreach (string name in Enum.GetNames(typeof(SeatClass))) { // Finds the section by SeatClass. var section = context.Sections.Where(m => m.SeatClassName == name).FirstOrDefault(); // Checks if the associated floght have such seatClass. var checker = query.Where(s => s.SectionId == section.SectionId).FirstOrDefault(); // If the flight does not have such seat class nothing more is appended. if (checker == null) { continue; } // Appends the name of the seat class and the seats. builder.Append($"\n\t{name} \n\t"); foreach (var seat in query.Where(s => s.SectionId == section.SectionId)) { builder.Append($"{seat.Row}{seat.Col} {seat.Status};"); } } builder.Append("\n"); return(builder.ToString()); } }
public void CreateSection(string aName, string flightName, int rows, int cols, SeatClass s) { try { if (rows < 1 || rows > 100 || cols < 1 || cols > 10) { throw new Exception(ExceptionHelper.FlightSectionOutOfBound); } using (var context = new ABS()) { Airline airline = _airlineRepository.Retreive(aName); Flight flight = _flightRepository.Retreive(airline.AirlineId, flightName); if (airline == null) { throw new Exception(ExceptionHelper.NonExistentAirline); } if (flight == null) { throw new Exception(ExceptionHelper.FlightNotFound); } _seatRepository.AddSeatsForFlight(flight, rows, cols, s); Console.WriteLine("Succesfully inserted section"); } } catch (Exception e) { Console.WriteLine($"{e.Message} - {aName} {flightName}"); } }
public void AddSeatsForFlight(Flight flight, int row, int col, SeatClass seatClass) { using (var context = new ABS()) { // Gets the section for the enum seatClass var section = context.Sections.Where(s => s.SeatClassName == seatClass.ToString()) .FirstOrDefault(); // Checks if such section is already associated with the flight var existingSection = context.Seats.Where(s => s.FlightId == flight.FlightId && s.SectionId == section.SectionId) .FirstOrDefault(); if (existingSection != null) { throw new Exception(ExceptionHelper.ExistingSection); } List <Seat> seats = new List <Seat>(); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { Seat seatToAdd = new Seat(); seatToAdd.Col = Convert.ToChar('A' + j).ToString(); //translates the integer value to alphabethical char seatToAdd.Row = i + 1; //because we want the rows to start at 1 not at 0 seatToAdd.FlightId = flight.FlightId; seatToAdd.Status = true; seatToAdd.SectionId = section.SectionId; seats.Add(seatToAdd); } } context.Seats.AddRange(seats); context.SaveChanges(); } }
public IEnumerable <Airport> Retreive() { using (var context = new ABS()) { return(context.Airports.ToList()); } }
public IEnumerable <Flight> RetreiveAllFlightsForAirline(Airline airline) { using (var context = new ABS()) { var flights = context.Flights.Where(f => f.AirlineId == airline.AirlineId).ToList(); return(flights); } }
public List <Seat> AllAvailableSeatsList(Flight fl) { using (var context = new ABS()) { var freeSeatForFlight = context.Seats.Include(co => co.Section) .Where(s => s.FlightId == fl.FlightId && s.Status == true) .ToList(); return(freeSeatForFlight); } }
public IEnumerable <Flight> FlightsFromOriginToDestination(Airport originAirport, Airport destinationAirport) { using (var context = new ABS()) { var flightsFromOrigToDest = context.Flights.Include(co => co.Airline) .Where(f => f.OrgAirportId == originAirport.AirportId && f.DestAirportId == destinationAirport.AirportId) .ToList(); // ToList is used to not dispose dbcontext before we need it return(flightsFromOrigToDest); }; }
public Airline Retreive(int airlineId) { using (var context = new ABS()) { var airline = context.Airlines.Where(a => a.AirlineId == airlineId).FirstOrDefault(); if (airline == null) { throw new Exception(ExceptionHelper.NonExistentAirline); } return(airline); } }
// Retreives flight by Id public Flight Retreive(int flId) { using (var context = new ABS()) { var flight1 = context.Flights.Where(f => f.FlightId == flId) .FirstOrDefault(); if (flight1 == null) { throw new Exception(ExceptionHelper.NonExistentFlight); } return(flight1); } }
// Retreives flight by airlineID and flightName public Flight Retreive(int airlineId, string flightName) { using (var context = new ABS()) { var flight1 = context.Flights.Where(f => f.AirlineId == airlineId && f.FlightName == flightName) .FirstOrDefault(); if (flight1 == null) { throw new Exception(ExceptionHelper.NonExistentFlight); } return(flight1); } }
public bool HasAvailableSeats(Flight fl) { using (var context = new ABS()) { var freeSeatForFlight = context.Seats.Where(s => s.FlightId == fl.FlightId && s.Status == true) .FirstOrDefault(); if (freeSeatForFlight == null) { return(false); } return(true); } }
public void AddNewFlight(Flight flight) { using (var context = new ABS()) { var flight1 = context.Flights.Where(f => f.AirlineId == flight.AirlineId && f.FlightName == flight.FlightName) .FirstOrDefault(); if (flight1 != null) { throw new Exception(ExceptionHelper.AlreadyExistentFlight); } context.Flights.Add(flight); context.SaveChanges(); } }
/// <summary> /// Retreives all airports. /// </summary> /// <returns></returns> public string DisplayAirportsDetails() { StringBuilder builder = new StringBuilder(); using (var context = new ABS()) { foreach (Airport airport in context.Airports) { builder.Append($"{airport.Name}\n"); } } return(builder.ToString()); }
public void AddNewAirline(Airline airline) { using (var context = new ABS()) { var airline1 = context.Airlines.Where(a => a.Name == airline.Name).FirstOrDefault(); if (airline1 != null) { throw new Exception(ExceptionHelper.AlreadyExistentAirline); } context.Airlines.Add(airline); context.SaveChanges(); } }
public void AddNewAirport(Airport airport) { using (var context = new ABS()) { // Tries to find airport with the same name as the passed entity and adds it to the database only if it is not found var airp = context.Airports.Where(a => a.Name == airport.Name).FirstOrDefault(); if (airp != null) { throw new Exception(ExceptionHelper.AlreadyExistentAirport); } context.Airports.Add(airport); context.SaveChanges(); } }
/// <summary> /// Retreives an airport with name from the parameter. /// </summary> public Airport Retreive(string airportName) { if (String.IsNullOrEmpty(airportName)) { throw new Exception(ExceptionHelper.NullAirportName); } using (var context = new ABS()) { var airport = context.Airports.Where(a => a.Name == airportName).FirstOrDefault(); if (airport == null) { throw new Exception(ExceptionHelper.NonExistentAirport); } return(airport); } }
public string DisplayFlightsDetails() { StringBuilder builder = new StringBuilder(); using (var context = new ABS()) { foreach (Flight flight in context.Flights) { builder.Append($"\nFlight name: {flight.FlightName}, " + $"Airline: {flight.Airline.Name}, " + $"Origin: {context.Airports.Find(flight.OrgAirportId).Name}, " + $"Destination: {context.Airports.Find(flight.DestAirportId).Name}, " + $"Date: {flight.Date.ToString("dd.MM.yyyy")} \n Seats: \n"); builder.Append(SeatRepository.DisplayDetailsForSeatsInFlight(flight)); } } return(builder.ToString()); }
public bool BookSeat(Flight flight, SeatClass seatClass, int row, string col) { using (var context = new ABS()) { var section = context.Sections.Where(s => s.SeatClassName == seatClass.ToString()) .FirstOrDefault(); var seatToBook = context.Seats.Where(s => s.FlightId == flight.FlightId && s.SectionId == section.SectionId && s.Row == row && s.Col == col) .FirstOrDefault(); if (seatToBook.Status == false) { return(false); } seatToBook.Status = false; context.SaveChanges(); return(true); } }
public string DisplaySeatsDetails() { StringBuilder builder = new StringBuilder(); using (var context = new ABS()) { foreach (var flight in context.Flights) { var query = context.Seats.Where(s => s.FlightId == flight.FlightId); foreach (string name in Enum.GetNames(typeof(SeatClass))) { var section = context.Sections.Where(m => m.SeatClassName == name) .FirstOrDefault(); builder.Append($"\n {name} \n"); foreach (var seat in query.Where(s => s.SectionId == section.SectionId)) { builder.Append($"{seat.Col}{seat.Row} {seat.Status};"); } } } return(builder.ToString()); } }