public ActionResult Edit(int thisTripId, ViewEditTripsViewModel model) { if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId())) // check if current user has admin rights { return(RedirectToAction("AccessDenied", "Manage")); } //check for reservations. Cannot edit while there are any. Placeholder int NumberOfReservations = countReservavtionsMade((int)thisTripId); //check for reservations. Cannot edit while there are any and trip is yet to end var currTime = DateTime.Now; if (NumberOfReservations > 0 && model.TripInstance.DateBack > currTime) { return(RedirectToAction("Index", new { Message = ManageMessageId.CannotEditEntry })); } try { Trip trip = dbcontext.Trips.Find(thisTripId); //get current trip if (trip == null) { return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } trip.Name = model.TripInstance.Name; trip.DateDeparture = model.TripInstance.DateDeparture; trip.DateBack = model.TripInstance.DateBack; trip.NumSpots = model.TripInstance.NumSpots; trip.Price = model.TripInstance.Price; trip.Description = model.TripInstance.Description; trip.Banner = model.TripInstance.Banner; trip.CoachNumberId = model.TripInstance.CoachNumberId; dbcontext.SaveChanges(); return(RedirectToAction("Index", new { Message = ManageMessageId.EditDetailsSuccess })); } catch { return(RedirectToAction("Index", new { Message = ManageMessageId.Error })); } }
// GET: Trip/Edit/5 public ActionResult Edit(int?thisTripId) { if (thisTripId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (!UserRoleHelper.IsAdmin(User.Identity.GetUserId())) { if (!UserRoleHelper.IsEmployee(User.Identity.GetUserId()))// check if current user has admin or employee rights { return(RedirectToAction("AccessDenied", "Manage")); } } //check for reservations. Cannot edit while there are any. int NumberOfReservations = countReservavtionsMade((int)thisTripId); Trip trip = dbcontext.Trips.Find(thisTripId); //get current trip if (trip == null) { return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } ViewEditTripsViewModel model = new ViewEditTripsViewModel(); model.TripInstance = trip; //check for reservations. Cannot edit while there are any and trip is yet to end var currTime = DateTime.Now; if (NumberOfReservations > 0 && model.TripInstance.DateBack > currTime) { return(RedirectToAction("Index", new { Message = ManageMessageId.CannotEditEntry })); } //get a list of all sub-locations that this trip has var list = new List <TripLocationsInstanceViewModels>(); foreach (var item in dbcontext.Trip_Locations.ToList()) { if (item.Id_Trip == thisTripId) { foreach (var location in dbcontext.Locations.ToList()) { if (item.Id_Location == location.Id) { list.Add(new TripLocationsInstanceViewModels { Country = location.Country, Town = location.Town, Name = location.Name, Description = location.Description, LocationImage = location.LocationImage, Number = item.Number, RouteInstanceId = item.Id }); } } } } model.Route = new TripLocationsViewModels(); if (list.Count() > 0) { model.Route.ListElement = list; } if (thisTripId != null && dbcontext.Trips.Find(thisTripId) != null) { model.Route.Id_Trip = (int)thisTripId; } // list that has every coach in database var listOfCoaches = dbcontext.Coaches.ToList(); var currDate = DateTime.Now; // go through every trip in db that is in progress atm. A coach assigned to that trip will be removed from our list, so it cant be assigned //to this currently edited trip foreach (var coach in dbcontext.Coaches.ToList()) { foreach (var tripInstance in dbcontext.Trips.ToList()) { if (tripInstance.DateDeparture < model.TripInstance.DateDeparture && model.TripInstance.DateDeparture < tripInstance.DateBack) { if (coach.Id == tripInstance.CoachNumberId) { listOfCoaches.Remove(coach); } } } } ViewBag.DateDeparture = model.TripInstance.DateDeparture; ViewBag.DateBack = model.TripInstance.DateBack; model.CoachVehicleIdList = new SelectList(listOfCoaches, "Id", "VehicleNumber"); return(View(model)); }
// GET: Trip/Details/5 public ActionResult Details(int?thisTripId) { if (thisTripId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } // ViewBag value for holding part of google maps url data string ViewBag.Markers = ""; string[] colours = { "red", "green", "blue" }; Trip trip = dbcontext.Trips.Find(thisTripId); if (trip == null) { return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } ViewEditTripsViewModel model = new ViewEditTripsViewModel(); model.TripInstance = trip; var list = new List <TripLocationsInstanceViewModels>(); int i = 0; foreach (var item in dbcontext.Trip_Locations.ToList()) { if (item.Id_Trip == thisTripId) { foreach (var location in dbcontext.Locations.ToList()) { if (item.Id_Location == location.Id) { // Create a marker string using current location data; can be modified for any url string if needed depending on map provider ++i; // cycles the string array in a loop ViewBag.Markers += "&markers=color:" + colours[(i - 1) % 3] + "|label:" + i.ToString() + "|" + location.Name + "," + location.Town + "," + location.Country; // ******************** list.Add(new TripLocationsInstanceViewModels { Country = location.Country, Town = location.Town, Name = location.Name, Description = location.Description, LocationImage = location.LocationImage, Number = item.Number, RouteInstanceId = item.Id }); } } } } model.Route = new TripLocationsViewModels(); if (list.Count() > 0) { model.Route.ListElement = list; } if (thisTripId != null && dbcontext.Trips.Find(thisTripId) != null) { model.Route.Id_Trip = (int)thisTripId; } //return free spots left FOR NOW STATIC VALUES. Bookings controller is not implemented model.NumSpotsLeft = countFreeSpots((int)thisTripId); //return number of reservations model.NumberOfReservations = countReservavtionsMade((int)thisTripId); // add coach info to List foreach (var item in dbcontext.Coaches.ToList()) { if (trip.CoachNumberId == item.Id) { model.CoachBanner = item.VehScreenshot; model.CoachModel = item.Brand + " " + item.VehModel + " - no." + item.VehicleNumber; } } return(View(model)); }