示例#1
0
        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();
            }
        }
示例#2
0
 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();
     }
 }
示例#3
0
 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();
     }
 }
示例#4
0
        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();
            }
        }
示例#5
0
        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);
            }
        }