public IActionResult Edit(int id) { var tourInDb = _context.Tours.Single(t => t.Id == id); // Check if an instance of Tour exists else return 404 if (tourInDb == null) { return(NotFound()); } // Check if the user ID that created the tour matches session user var currUser = _userManager.GetUserId(HttpContext.User); if (tourInDb.UserId != currUser) { return(BadRequest()); } // If checks has passed, map into a viewModel and allow editing. var viewModel = new TourFormViewModel(tourInDb); Country countryList = new Country(); ViewBag.CountryList = countryList.GetCountries(); return(View("TourForm", viewModel)); }
[ValidateAntiForgeryToken] //CSRF attack public ActionResult Update(TourFormViewModel tourFormViewModel) { if (!ModelState.IsValid) { tourFormViewModel.Genres = _unitOfWork.Genres.GetGenre(); return(View("TourForm", tourFormViewModel)); } var tour = _unitOfWork.Tours.GetTourWithAttendees(tourFormViewModel.Id); if (tour == null) { return(HttpNotFound()); } if (tour.TravellerID != User.Identity.GetUserId()) { return(new HttpUnauthorizedResult()); } tour.Modify(tourFormViewModel.GetDateTime(), tourFormViewModel.Place, tourFormViewModel.TotalSeat, tourFormViewModel.Cost, tourFormViewModel.Genre); //tour.Place = tourFormViewModel.Place; //tour.TotalSeat = tourFormViewModel.TotalSeat; //tour.DateTime = tourFormViewModel.GetDateTime(); //tour.Cost = tourFormViewModel.Cost; //tour.GenreID = tourFormViewModel.Genre; //update data _unitOfWork.Complete(); return(RedirectToAction("Mine", "Tour")); }
public IActionResult Save(TourFormViewModel viewModel) { if (!ModelState.IsValid) { Country countryList = new Country(); ViewBag.CountryList = countryList.GetCountries(); var tourModel = new TourFormViewModel(); return(View("TourForm", tourModel)); } var tour = _mapper.Map <TourFormViewModel, Tour>(viewModel); // Get the user incharge of creating the tour and associate it with the new tour var currUser = _userManager.GetUserId(HttpContext.User); var userInDb = _context.Users.Single(u => u.Id == currUser); // If the tour came as an ID of 0 it means it was newly created if (tour.Id == 0) { // Set current tour availability to tour capacity as it's brand new. tour.TourAvailability = tour.TourCapacity; tour.UserId = currUser; tour.User = userInDb; _context.Tours.Add(tour); } // Else it means the tour is attempting to be edited by a user. else { // Look up the DB for existing tour var tourInDb = _context.Tours.Single(t => t.Id == tour.Id); // We need to account for the possibility that the user could increase the Tour Capacity which will affect our Availibility if (tour.TourCapacity > tourInDb.TourCapacity) { int balance = tour.TourCapacity - tourInDb.TourCapacity; tourInDb.TourAvailability += balance; } tourInDb.Name = tour.Name; tourInDb.Description = tour.Description; tourInDb.Country = tour.Country; tourInDb.StartLocation = tour.StartLocation; tourInDb.EndLocation = tour.EndLocation; tourInDb.StartDate = tour.StartDate; tourInDb.EndDate = tour.EndDate; tourInDb.Price = tour.Price; tourInDb.TourCapacity = tour.TourCapacity; } _context.SaveChanges(); return(RedirectToAction("CreatedTours", "Tours")); }
public ActionResult Create() { var viewModel = new TourFormViewModel { Heading = "Create a Tour Event", Genres = _unitOfWork.Genres.GetGenre() }; return(View("TourForm", viewModel)); }
public IActionResult Create() { // Country Picker to Populate Country DDL Country countryList = new Country(); ViewBag.CountryList = countryList.GetCountries(); var tourModel = new TourFormViewModel(); return(View("TourForm", tourModel)); }
public ActionResult Edit(int id) { var userId = User.Identity.GetUserId(); var tour = _unitOfWork.Tours.GetTour(id); var viewModel = new TourFormViewModel { Genres = _unitOfWork.Genres.GetGenre(), Id = id, Date = tour.DateTime.ToString("d MMM yyyy"), Time = tour.DateTime.ToString("HH:mm"), Genre = tour.GenreID, Place = tour.Place, Cost = tour.Cost, TotalSeat = tour.TotalSeat, Heading = "Edit your Tour Event" }; return(View("TourForm", viewModel)); }
[ValidateAntiForgeryToken] //CSRF attack public ActionResult Create(TourFormViewModel tourFormViewModel) { if (!ModelState.IsValid) { tourFormViewModel.Genres = _unitOfWork.Genres.GetGenre(); return(View("TourForm", tourFormViewModel)); } var tour = new Tour { TravellerID = User.Identity.GetUserId(), DateTime = tourFormViewModel.GetDateTime(), GenreID = tourFormViewModel.Genre, Cost = tourFormViewModel.Cost, Place = tourFormViewModel.Place, TotalSeat = tourFormViewModel.TotalSeat }; _unitOfWork.Tours.Add(tour); _unitOfWork.Complete(); return(RedirectToAction("Mine", "Tour")); }