public ActionResult Create(Flight flight) { if (User.Identity.Name != "admin") { return RedirectToAction("Forbidden"); } if (ModelState.IsValid) { var flights = from m in db.Flights select m; int count = flights.Where(x => x.FlightNumber == flight.FlightNumber).Count(); if (count == 0) { db.Flights.Add(flight); db.SaveChanges(); return RedirectToAction("Index"); } else { return RedirectToAction("NumberConflict"); } } return View(flight); }
public ActionResult Edit(Flight flight) { if (User.Identity.Name != "admin") { return RedirectToAction("Forbidden"); } if (flight.BusinessCapacity < flight.BusinessSeatsBooked || flight.EconomyCapacity < flight.EconomySeatsBooked) { return RedirectToAction("CapacityError"); } List<BookedFlights> raw = new List<BookedFlights>(); var bookedFlights = from m in db.BookedFlights select m; raw = bookedFlights.Where(x => x.FlightNumber == flight.FlightNumber).ToList(); foreach (BookedFlights z in raw) { z.Origin = flight.Origin; z.Destination = flight.Destination; z.Date = flight.Date; z.DepartureTime = flight.DepartureTime; z.Type = flight.Type; if(z.Class.Equals("economy",StringComparison.CurrentCultureIgnoreCase)){ z.Price = z.Seats * flight.EconomyClassPrice; } else{ z.Price = z.Seats * flight.BusinessClassPrice; } db.Entry(z).State = EntityState.Modified; } if (ModelState.IsValid) { db.Entry(flight).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(flight); }
public ActionResult Book(int id, string bookSeats, string typeClass) { if (!User.Identity.IsAuthenticated) { return RedirectToAction("Forbidden"); } unBooked = db.Flights.Find(id); int seats; int.TryParse(bookSeats, out seats); if (seats == 0) { return RedirectToAction("ErrorBooking"); } else if(typeClass.Equals("economy",StringComparison.CurrentCultureIgnoreCase) && seats <= unBooked.EconomySeatsAvailable){ flight.Date = unBooked.Date; flight.DepartureTime = unBooked.DepartureTime; flight.Destination = unBooked.Destination; flight.FlightNumber = unBooked.FlightNumber; flight.Origin = unBooked.Origin; flight.Price = unBooked.EconomyClassPrice * seats; flight.Type = unBooked.Type; flight.Seats = seats; flight.Class = typeClass; flight.BookedBy = User.Identity.Name; unBooked.EconomySeatsBooked += seats; } else if (typeClass.Equals("business", StringComparison.CurrentCultureIgnoreCase) && seats <= unBooked.BusinessSeatsAvailable) { flight.Date = unBooked.Date; flight.DepartureTime = unBooked.DepartureTime; flight.Destination = unBooked.Destination; flight.FlightNumber = unBooked.FlightNumber; flight.Origin = unBooked.Origin; flight.Price = unBooked.BusinessClassPrice * seats; flight.Type = unBooked.Type; flight.Seats = seats; flight.Class = typeClass; flight.BookedBy = User.Identity.Name; unBooked.BusinessSeatsBooked += seats; } else { return RedirectToAction("ErrorBooking"); } if (ModelState.IsValid) { db.BookedFlights.Add(flight); db.SaveChanges(); db.Entry(unBooked).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return RedirectToAction("ErrorBooking"); }