public static List <Ticket> CreateListOfTickets(FlightPath path, PaymentType paymentType, DateTime date) { var scheduledFlights = new List <ScheduledFlight>(); using var db = new AirContext(); foreach (Flight flight in path.flights) { // see if a scheduled flight exists for this flight on this day already var sf = db.ScheduledFlights.Include(sf => sf.Flight).ThenInclude(fl => fl.PlaneType).Where(sf => sf.Flight.FlightId == flight.FlightId).SingleOrDefault(sf => sf.DepartureTime.Date == date); if (sf != null) { // If a flight exists already, simply add it to our list of scheduled flights scheduledFlights.Add(sf); } else { // Otherwise, we need to make a scheduled flight for it var sfNew = new ScheduledFlight { FlightId = flight.FlightId, DepartureTime = date.Add(flight.DepartureTime), TicketsPurchased = 1 }; // add it to our list scheduledFlights.Add(sfNew); // and the db db.ScheduledFlights.Add(sfNew); db.SaveChanges(); } } var tickets = new List <Ticket>(); foreach (ScheduledFlight sf in scheduledFlights) { // Now we need to make a ticket for each scheduled flight var ticket = new Ticket { Flight = sf, PaymentType = paymentType }; // Increment the amount of tickets purchased sf.TicketsPurchased++; // add it to our list to return tickets.Add(ticket); // and add it to the db db.Tickets.Add(ticket); db.SaveChanges(); } // return the tickets we made return(tickets); }
public ActionResult Create(AirInfo airinfo) { try { if (ModelState.IsValid) { db.AirInfos.Add(airinfo); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (Exception ex) { ModelState.AddModelError(String.Empty, ex); } return(View(airinfo)); }
override protected void OnNavigatedTo(NavigationEventArgs e) { if (isCustomer) { // if we have a customer we need to load a bunch of info var db = new AirContext(); var user = db.Users.Include(dbuser => dbuser.CustInfo) .ThenInclude(custInfo => custInfo.Trips) .ThenInclude(trip => trip.Origin) .Include(user => user.CustInfo) .ThenInclude(custInfo => custInfo.Trips) .ThenInclude(trip => trip.Destination) .Include(user => user.CustInfo) .ThenInclude(custInfo => custInfo.Trips) .ThenInclude(trip => trip.Tickets) .ThenInclude(ticket => ticket.Flight) .ThenInclude(scheduledFlight => scheduledFlight.Flight) .ThenInclude(flight => flight.Origin) .Include(user => user.CustInfo) .ThenInclude(custInfo => custInfo.Trips) .ThenInclude(trip => trip.Tickets) .ThenInclude(ticket => ticket.Flight) .ThenInclude(scheduledFlight => scheduledFlight.Flight) .ThenInclude(flight => flight.Destination) .Single(dbuser => dbuser.UserId == UserSession.userId); CustomerInfo customerInfo = user.CustInfo; // award points for any trips that have departed & were not yet claimed int newPoints = 0; // loop through all of their trips foreach (Trip trip in customerInfo.Trips) { // check if the trip has departed if (trip.GetFormattedDeparted() == "Trip has departed!") { // make sure they haven't claimed the points already if (!trip.PointsClaimed) { // Award the appropriate points to the user UserUtilities.AwardPoints(user, (trip.TotalCost / 100) * 10); newPoints = trip.TotalCost / 100; // keep track of this for the ui trip.PointsClaimed = true; // set the points claimed to true var dbtrip = db.Trips.Single(dbtripinterior => dbtripinterior.TripId == trip.TripId); dbtrip.PointsClaimed = true; // and update the db as well db.SaveChanges(); } } } // Display basic info about the user WelcomeText.Text = $"Welcome back {customerInfo.Name}!"; PointsText.Text = $"You currently have {customerInfo.PointsAvailable + newPoints} points available, and overall you have used {customerInfo.PointsUsed} points."; CreditText.Text = $"You currently have a credit balance of ${customerInfo.CreditBalance / 100} with us."; TicketSummaryText.Text = $"You have booked {customerInfo.Trips.ToArray().Length} trips with us."; // and fill our list view with the customers trips TripList.ItemsSource = customerInfo.Trips; } }
// subtracts the specified amount of credit from the specified user public static void UseCredit(User user, int amount) { using var db = new AirContext(); var customerInfo = db.Users.Include(user => user.CustInfo).Where(dbuser => dbuser.UserId == user.UserId).FirstOrDefault().CustInfo; if (customerInfo != null) { customerInfo.CreditBalance -= amount; } db.SaveChanges(); }
// adds the specified amount of points to the specified user public static void AwardPoints(User user, int amount) { using var db = new AirContext(); var customerInfo = db.Users.Include(user => user.CustInfo).Where(dbuser => dbuser.UserId == user.UserId).FirstOrDefault().CustInfo; if (customerInfo != null) { customerInfo.PointsAvailable += amount; } db.SaveChanges(); }
private void HandleUpdateAccount() { // validate input if (ValidateInput()) { User currentUser = null; CustomerInfo custInfo = null; if (UserSession.userLoggedIn) { var db = new AirContext(); var user = db.Users.Include(dbuser => dbuser.CustInfo).Single(dbuser => dbuser.UserId == UserSession.userId); currentUser = user; // if the current user is a customer, then update their information from the fields if (user.UserRole == Role.CUSTOMER) { custInfo = currentUser.CustInfo; custInfo.Name = NameInput.Text; custInfo.Address = AddressInput.Text; custInfo.City = CityInput.Text; custInfo.State = StateInput.Text; custInfo.Zip = ZipInput.Text; custInfo.PhoneNumber = PhoneInput.Text; custInfo.Age = (int)AgeInput.Value; custInfo.CreditCardNumber = CreditCardInput.Text; } } // if they are updating their password then we need to update their hashed password if (!string.IsNullOrWhiteSpace(PasswordInput.Password) && !string.IsNullOrWhiteSpace(ConfirmPasswordInput.Password)) { currentUser.HashedPass = PasswordHandler.HashPassword(PasswordInput.Password); } using (var db = new AirContext()) { // save the updated customer info in the database var dbuser = db.Users.Single(user => user.LoginId == currentUser.LoginId); if (custInfo != null) { dbuser.CustInfo = custInfo; } dbuser.HashedPass = currentUser.HashedPass; db.SaveChanges(); } // display to the user that we updated their info successfull outputInfo.Title = "Account Information Updated!"; outputInfo.Message = "Your Account Information was updated successfully!"; outputInfo.Severity = InfoBarSeverity.Success; outputInfo.IsOpen = true; } }
private void EditDepartButton_Click(object sender, RoutedEventArgs e) { if (ValidateEditParameters()) { // Ready to change time. using var db = new AirContext(); // grab object that was selected from list var editFlight = DepartList.SelectedItem as FlightPath; // check if the flight has already been scheduled var existingFlights = db.ScheduledFlights.Include(sf => sf.Flight) .Where(sf => sf.Flight.FlightId == editFlight.flights[0].FlightId) .ToList(); // if the flight has been scheduled you can't edit it if (existingFlights.Count > 0) { OutputInfo.Title = "You cannot edit this flight!"; OutputInfo.Message = "This flight has already been scheduled at least once so it cannot be edited!"; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; return; } // check that something was selected if (editFlight != null) { // get time from time picker TimeSpan selectedTime = (TimeSpan)timePickerAdd.SelectedTime; // get plane from db to delete from list db.Flights.Single(flight => flight.FlightId == editFlight.flights[0].FlightId).DepartureTime = selectedTime; db.SaveChanges(); // refresh list Frame.Navigate(typeof(LEManageFlightsPage), passedLEParams); } else { OutputInfo.Title = "Invalid Input!"; OutputInfo.Message = "You must select a flight first."; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; } } else { OutputInfo.Title = "Invalid Input!"; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; } }
public static void CancelTrip(Trip trip, User user) { using var db = new AirContext(); // Loop through all tickets foreach (Ticket ticket in trip.Tickets) { var dbticket = db.Tickets.Include(ticket => ticket.Flight).Single(dbtick => dbtick.TicketId == ticket.TicketId); // cancel them ticket.IsCanceled = true; dbticket.IsCanceled = true; // subtract 1 ticket purchased from scheduled flight ticket.Flight.TicketsPurchased -= 1; dbticket.Flight.TicketsPurchased -= 1; db.SaveChanges(); } // award user credit based on the overall price UserUtilities.AwardCredit(user, trip.TotalCost); // cancel the trip trip.IsCanceled = true; var dbtrip = db.Trips.Single(dbtripinterior => dbtripinterior.TripId == trip.TripId); dbtrip.IsCanceled = true; db.SaveChanges(); }
private void AddButton_Click(object sender, RoutedEventArgs e) { if (ValidateAddParameters()) { // get data from GUI and add flight using var db = new AirContext(); string originCode = StripAirportCode(originPickerAdd.Text); string destCode = StripAirportCode(destPickerAdd.Text); var originAirport = db.Airports.Single(Airport => Airport.AirportCode == originCode); var destAirport = db.Airports.Single(Airport => Airport.AirportCode == destCode); TimeSpan selectedTime = (TimeSpan)timePickerAdd.SelectedTime; // Make sure that this isn't a new route if (EnsureExistingRoute(originAirport, destAirport)) { // Add new flight // plane type defaults to 737 Flight flight = new() { Origin = originAirport, Destination = destAirport, PlaneType = db.Planes.Single(plane => plane.PlaneId == 1), DepartureTime = selectedTime }; db.Flights.Add(flight); db.SaveChanges(); OutputInfo.Title = "Success!"; OutputInfo.Message = $"Flight was successfully added!"; OutputInfo.Severity = InfoBarSeverity.Success; OutputInfo.IsOpen = true; } else { OutputInfo.Title = "Route Doesn't Exist!"; OutputInfo.Message = $"Talk to your manager if you really think we should add a brand new route."; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; } } else { OutputInfo.Title = "Invalid Input!"; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; } }
private void DeleteFlight_Click(object sender, RoutedEventArgs e) { using var db = new AirContext(); // grab object that was selected from list var deleteFlight = DepartList.SelectedItem as FlightPath; // check if the flight has already been scheduled at least once var existingFlights = db.ScheduledFlights.Include(sf => sf.Flight) .Where(sf => sf.Flight.FlightId == deleteFlight.flights[0].FlightId) .ToList(); // if the flight already exists, display an error and return if (existingFlights.Count > 0) { OutputInfo.Title = "You cannot delete this flight!"; OutputInfo.Message = "This flight has already been scheduled at least once so it cannot be deleted!"; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; return; } // check that something was selected if (deleteFlight != null) { // get plane from db to delete from list db.Remove(db.Flights.Single(flight => flight.FlightId == deleteFlight.flights[0].FlightId)); db.SaveChanges(); // refresh list Frame.Navigate(typeof(LEManageFlightsPage), passedLEParams); } else { OutputInfo.Title = "Invalid Input!"; OutputInfo.Message = "You must select a flight first."; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; } }
private void B747_Click(object sender, RoutedEventArgs e) { using var db = new AirContext(); var planeChangeflight = DepartList.SelectedItem as FlightPath; // check that a flight was selected if (planeChangeflight != null) { // get plane from db to change to change plane var NewPlane = db.Planes.Single(plane => plane.PlaneId == 2); db.Flights.Single(flight => flight.FlightId == planeChangeflight.flights[0].FlightId).PlaneType = NewPlane; db.SaveChanges(); // refresh list Frame.Navigate(typeof(MMSelectPlanePage), passedMMParams); } else { OutputInfo.Title = "Invalid Input!"; OutputInfo.Message = "You must select a flight first."; OutputInfo.Severity = InfoBarSeverity.Error; OutputInfo.IsOpen = true; } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { User user = null; using (AirContext db = new AirContext()) { user = db.Users.FirstOrDefault(u => u.Email == model.Name); } if (user == null) { // создаем нового пользователя using (AirContext db = new AirContext()) { db.Users.Add(new User { Email = model.Name, Password = model.Password }); db.SaveChanges(); user = db.Users.Where(u => u.Email == model.Name && u.Password == model.Password).FirstOrDefault(); } // если пользователь удачно добавлен в бд if (user != null) { FormsAuthentication.SetAuthCookie(model.Name, true); return(RedirectToAction("Index", "Home")); } } else { ModelState.AddModelError("", "Пользователь с таким логином уже существует"); } } return(View(model)); }
public void AddPurchase(Purchase purchase) { db.Purchases.Add(purchase); db.SaveChanges(); }
// adds a user to the db public static void AddUserToDB(User user) { using var db = new AirContext(); db.Users.Add(user); db.SaveChanges(); }