예제 #1
0
 public void AddEntriesToFlightEmployees(List <Employee> Employees, int FlightId)
 {
     for (int i = 0; i < Employees.Count(); i++)
     {
         FlightEmployee NewFlightEmployee = new FlightEmployee
         {
             FlightId   = FlightId,
             EmployeeId = Employees[i].Id
         };
         _context.Add(NewFlightEmployee);
         _context.SaveChanges();
     }
 }
예제 #2
0
        public static async Task <string> addFlightToDatabase(WebhookRequest request, FlightsAPIContext _apiContext, FlightContext _dbContext)
        {
            // Extracts the parameters of the request
            var    requestParameters = request.QueryResult.Parameters;
            string fromCity          = requestParameters.Fields["from-city"].StringValue;
            string toCity            = requestParameters.Fields["to-city"].StringValue;

            DateTime departure = DateTimeOffset.Parse(requestParameters.Fields["date-time"].StringValue).UtcDateTime.Date;
            var      flight    = await _apiContext.GetFlight(fromCity, toCity, departure);

            if (flight.Quotes.Any())
            {
                FlightModel flightItem = new FlightModel
                {
                    FromCity      = flight.Places.Where(p => p.PlaceId == flight.Quotes[0].OutboundLeg.OriginId).First().Name,
                    ToCity        = flight.Places.Where(p => p.PlaceId == flight.Quotes[0].OutboundLeg.DestinationId).First().Name,
                    DepartureDate = flight.Quotes[0].OutboundLeg.DepartureDate.Date,
                    Price         = flight.Quotes[0].MinPrice,
                };
                _dbContext.Add(flightItem);
                await _dbContext.SaveChangesAsync();

                textToReturn = "Done!";
            }
            else
            {
                textToReturn = "Could not add a flight";
            }
            return(textToReturn);
        }
예제 #3
0
        // POST
        public void Add(Plane newPlane)
        {
            // define function for generating a random identifier
            const string LetterPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            const string NumberPool = "1234567890";
            Random       random     = new Random();

            string RandomString(int Length)
            {
                string part1 = new string(Enumerable.Repeat(NumberPool, Length / 2)
                                          .Select(s => s[random.Next(s.Length)]).ToArray());
                string part2 = new string(Enumerable.Repeat(LetterPool, Length / 2)
                                          .Select(s => s[random.Next(s.Length)]).ToArray());

                return(part1 + part2);
            }

            bool NoMatch = true;
            // Fetch all identifiers
            var Identifiers = _context.Planes.Select(plane => plane.Identifier).ToList();

            while (NoMatch)
            {
                // If random identifier doesnt exist in database, bind it and persist newPlane
                string RandomIndentifier = RandomString(6);
                if (!Identifiers.Contains(RandomIndentifier))
                {
                    newPlane.Identifier = RandomIndentifier;
                    NoMatch             = false;
                }
            }

            _context.Add(newPlane);
            _context.SaveChanges();
        }
예제 #4
0
        // POST
        public void Add(Employee NewEmployee)
        {
            // define function for generating a random number
            const string chars  = "0123456789";
            Random       random = new Random();

            string RandomString(int length)
            {
                return(new string(Enumerable.Repeat(chars, length)
                                  .Select(s => s[random.Next(s.Length)]).ToArray()));
            }

            bool noMatch = true;
            // Fetch all emplyee numbers
            var employeeNumbers = _context.Employees.Select(employee => employee.EmployeeNumber).ToList();

            while (noMatch)
            {
                // If random number doesnt exist in database, bind it and persist newEmployee
                string randomNumber = RandomString(5);
                if (!employeeNumbers.Contains(randomNumber))
                {
                    NewEmployee.EmployeeNumber = randomNumber;
                    noMatch = false;
                }
            }

            _context.Add(NewEmployee);
            _context.SaveChanges();
        }
예제 #5
0
 public void CreateFlight(Flight flight)
 {
     using (FlightContext dbContext = new FlightContext())
     {
         dbContext.Add <Flight>(flight);
         dbContext.SaveChanges();
     }
 }
예제 #6
0
        public async Task <IActionResult> Create([Bind("ID,From,To,Stops,Price,departing,arrival,seatsAvailable")] AirRoutes airRoutes)
        {
            if (ModelState.IsValid)
            {
                _context.Add(airRoutes);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(airRoutes));
        }
예제 #7
0
        public async Task <IActionResult> Create([Bind("ID,FlightID,Origin,Destination,Date,DepartureTime,ArrivalTime,DistanceTravelled")] Flight flight)
        {
            if (ModelState.IsValid)
            {
                _context.Add(flight);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(flight));
        }
예제 #8
0
        public async Task <IActionResult> Create([Bind("FlightId,AircraftType,FromLocation,ToLocation,DepartureTime,ArrivalTime")] Flight flight)
        {
            if (ModelState.IsValid)
            {
                _context.Add(flight);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(flight));
        }
예제 #9
0
        public async Task <IActionResult> Create([Bind("FlightTicketID,FlightNumber,FlightDestination")] FlightTicket flightTicket)
        {
            if (ModelState.IsValid)
            {
                _context.Add(flightTicket);
                _logger.LogInformation("Added flight");
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(flightTicket));
        }
        public async Task <IActionResult> Create([Bind("RouteID,FromCityID,ToCityID,BasicFare,AveDuration")] Route route)
        {
            if (ModelState.IsValid)
            {
                _context.Add(route);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["FromCityID"] = new SelectList(_context.Cities, "CityID", "Name", route.FromCityID);
            ViewData["ToCityID"]   = new SelectList(_context.Cities, "CityID", "Name", route.ToCityID);
            return(View(route));
        }
        public async Task <IActionResult> Create([Bind("CityID,Name,AirportName,PostCode,StateOrTerritory,State")] City city)
        {
            city.State = city.StateOrTerritory.ToString();

            if (ModelState.IsValid)
            {
                _context.Add(city);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(city));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Email,FlightTicketID")] Reservation reservation)
        {
            if (ModelState.IsValid)
            {
                _context.Add(reservation);
                _logger.LogInformation("added booking");
                await _context.SaveChangesAsync();

                var ticket = (from f in _context.Reservations
                              where f.Id == reservation.Id
                              select f).First();
                return(RedirectToAction("Edit", "Reservations", ticket));
            }
            ViewData["FlightTicketID"] = new SelectList(_context.FlightTickets, "FlightTicketID", "FlightTicketID", reservation.FlightTicketID);
            return(View(reservation));
        }
        public async Task <IActionResult> Create(int FromCityID, int ToCityID)
        {
            var flightSchedule = new Flight_Schedule {
            };

            if (await TryUpdateModelAsync <Flight_Schedule>(flightSchedule,
                                                            "",
                                                            c => c.Flight_ScheduleID, c => c.RouteID, c => c.DepartDateTime, c => c.ArriveDateTime, c => c.AirCraftID, c => c.Economy, c => c.Business, c => c.PremEconomy, c => c.First))
            {
                //if the route in this schedule does not exist, then reject to create
                if (!await _context.Routes.AnyAsync(r => r.FromCity.CityID == FromCityID && r.ToCity.CityID == ToCityID))  //get rid of the route that does not exist
                {
                    ViewBag.ErrorNoRoute   = "No such route exists!";
                    ViewData["FromCityID"] = new SelectList(_context.Cities, "CityID", "Name");
                    ViewData["ToCityID"]   = new SelectList(_context.Cities, "CityID", "Name");
                    return(View(flightSchedule));
                }
                int RouteID = await _context.Routes.Where(r => r.FromCity.CityID == FromCityID && r.ToCity.CityID == ToCityID).Select(r => r.RouteID).FirstAsync(); //get the ID of route between2 cities

                flightSchedule.Route = await _context.Routes.Where(r => r.RouteID == RouteID).FirstAsync();                                                         //set the route in the new Flight_Schedule

                try
                {
                    _context.Add(flightSchedule);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException /* ex */)
                {
                    //Log the error (uncomment ex variable name and write a log.)
                    ModelState.AddModelError("", "Unable to save changes. " +
                                             "Try again, and if the problem persists, " +
                                             "see your system administrator.");
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["FromCityID"] = new SelectList(_context.Cities, "CityID", "Name");
            ViewData["ToCityID"]   = new SelectList(_context.Cities, "CityID", "Name");
            return(View(flightSchedule));
        }
예제 #14
0
        public int InitialBooking(List <int> tripList, string cabin, int passangers, DateTime bookingDateTime)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    int suspendedID;
                    if (_context.BookingTransactions.Any())
                    {
                        suspendedID = _context.BookingTransactions.OrderByDescending(b => b.SuspendedID).Select(b => b.SuspendedID).FirstOrDefault() + 1;
                    }
                    else
                    {
                        IncrementCounter incrementCounter = new IncrementCounter();
                        suspendedID = incrementCounter.NextValue(); //生成一个自动递增的数
                    }

                    //var bookingDateTime = DateTime.Now;
                    foreach (int tripNo in tripList)
                    {
                        Flight_Schedule flight_Schedule = _context.Flight_Schedules.Where(x => x.Flight_ScheduleID == tripNo).FirstOrDefault();

                        for (int i = 0; i < passangers; i++)
                        {
                            //generate a specific booking transaction(BookingTransaction) for every passanger
                            //每一个passanger都单独生成一比业务记录(BookingTransaction)
                            BookingTransaction bookingTransaction = new BookingTransaction {
                                BookingDateTime = bookingDateTime, DepartDateTime = flight_Schedule.DepartDateTime, Flight_ScheduleID = flight_Schedule.Flight_ScheduleID, PersonalID = "", SuspendedID = suspendedID, CabinType = (CabinType)Enum.Parse(typeof(CabinType), cabin), Suspended = true
                            };
                            _context.Add(bookingTransaction);
                        }
                        //_context.SaveChanges();
                    }

                    //change the booked seats number according the cabin and the nuumber of passengers
                    switch (cabin)
                    {
                    case "Economy":
                        foreach (var t in tripList)
                        {
                            _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.Economy = f.Economy - passangers);
                        }
                        break;

                    case "Business":
                        foreach (var t in tripList)
                        {
                            _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.Business = f.Business - passangers);
                        }
                        break;

                    case "PremEconomy":
                        foreach (var t in tripList)
                        {
                            _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.PremEconomy = f.PremEconomy - passangers);
                        }
                        break;

                    case "First":
                        foreach (var t in tripList)
                        {
                            _context.Flight_Schedules.Where(f => f.Flight_ScheduleID == t).ToList().ForEach(f => f.First = f.First - passangers);
                        }
                        break;
                    }
                    //_context.SaveChanges();

                    transaction.Complete();
                    return(suspendedID);
                }
                catch (Exception ex)
                {
                    return(0);
                }
            }
        }