public ActionResult Edit(int id = 0) { // add the Cities to the Viewbag CityRepository cityRepository = new CityRepository(); List <City> allCities = cityRepository.GetAll(); ViewBag.AllCities = new SelectList(allCities, "ID", "Name"); // add the Categories to the Viewbag CategoryRepository categoryRepository = new CategoryRepository(); List <Category> allCategories = categoryRepository.GetAll(); ViewBag.AllCategories = new SelectList(allCategories, "ID", "Name"); // create the viewmodel, based on the Restaurant from the database RestaurantViewModel model = new RestaurantViewModel(); RestaurantRepository restaurantRepository = new RestaurantRepository(); Restaurant dbRestaurant = restaurantRepository.GetByID(id); if (dbRestaurant != null) { model = new RestaurantViewModel(dbRestaurant); } return(View(model)); }
// GET: Restaurants/Details/5 public ActionResult Details(int id) { try { if (restaurantRepository.GetByID(id) == null) { throw new ArgumentNullException(); } RestaurantDetailsVM vm = new RestaurantDetailsVM(restaurantRepository, reviewRepository, id); return(View(vm)); } catch (Exception e) { log.Error($"[Restaurants Controller] [Details] Exception thrown: {e.Message}"); return(RedirectToAction("Index")); } }
public RestaurantDetailsVM(RestaurantRepository newRestaurantRepository, ReviewRepository newReviewRepository, int newID) { this.ID = newID; reviewRepository = newReviewRepository; restaurantRepository = newRestaurantRepository; MyReviews = reviewRepository.GetReviewsByRestaurantID(this.ID); MyRestaurant = restaurantRepository.GetByID(this.ID); }
public ActionResult Edit(RestaurantViewModel viewModel) { if (viewModel == null) { // this should not be possible, but just in case, validate TempData["ErrorMessage"] = "Ups, a serious error occured: No Viewmodel."; return(RedirectToAction("Index")); } // get the item from the database by ID RestaurantRepository restaurantRepository = new RestaurantRepository(); Restaurant dbRestaurant = restaurantRepository.GetByID(viewModel.ID); // if there is no object in the DB, this is a new item -> will create a new one if (dbRestaurant == null) { dbRestaurant = new Restaurant(); dbRestaurant.DateCreated = DateTime.Now; } // update the DB object from the viewModel dbRestaurant.CategoryID = viewModel.CategoryID; dbRestaurant.CityID = viewModel.CityID; dbRestaurant.Name = viewModel.Name; dbRestaurant.Description = viewModel.Description; // save the image to the local folder if there is an uploaded image // we have to generate a unique file name, to avoid duplication when the same image is uploaded for another restaurant if (Request.Files.Count > 0) { HttpPostedFileBase file = Request.Files[0]; // check if the uploaded file is a valid file and only then proceed if (file.ContentLength > 0 && string.IsNullOrEmpty(file.FileName) == false) { string imagesPath = Server.MapPath(Constants.ImagesDirectory); string uniqueFileName = string.Format("{0}_{1}", DateTime.Now.Ticks, file.FileName); string savedFileName = Path.Combine(imagesPath, Path.GetFileName(uniqueFileName)); file.SaveAs(savedFileName); dbRestaurant.ImageName = uniqueFileName; } } restaurantRepository.Save(dbRestaurant); TempData["Message"] = "The restaurant was saved successfully"; return(RedirectToAction("Index")); }
public ActionResult Delete(int id) { RestaurantRepository repository = new RestaurantRepository(); Restaurant restaurant = repository.GetByID(id); RestaurantsDeleteViewModel model = new RestaurantsDeleteViewModel(); model.ID = restaurant.ID; model.Name = restaurant.Name; model.Address = restaurant.Address; model.Phone = restaurant.Phone; model.Capacity = restaurant.Capacity; model.OpenHour = restaurant.OpenHour; model.CloseHour = restaurant.CloseHour; return(View(model)); }
public ActionResult Edit(int?id) { RestaurantRepository repository = new RestaurantRepository(); RestaurantsEditViewModel model = new RestaurantsEditViewModel(); if (id.HasValue) { Restaurant restaurant = repository.GetByID(id.Value); model.ID = restaurant.ID; model.Name = restaurant.Name; model.Address = restaurant.Address; model.Phone = restaurant.Phone; model.Capacity = restaurant.Capacity; model.OpenHour = restaurant.OpenHour; model.CloseHour = restaurant.CloseHour; } return(View(model)); }
public ActionResult Details(int id = 0) { RestaurantRepository restaurantRepository = new RestaurantRepository(); // get the DB object Restaurant dbRestaurant = restaurantRepository.GetByID(id); if (dbRestaurant == null) { // when we have RedirectToAction, we can not use Viewbag - so we use a TempData! TempData["ErrorMessage"] = "Could not find a restaurant with ID = " + id; return(RedirectToAction("Index")); } else { // create the view model RestaurantViewModel model = new RestaurantViewModel(dbRestaurant); return(View(model)); } }
public ActionResult Edit(RestaurantViewModel viewModel) { if (viewModel == null) { // this should not be possible, but just in case, validate TempData["ErrorMessage"] = "Ups, a serious error occured: No Viewmodel."; return(RedirectToAction("Index")); } // get the item from the database by ID RestaurantRepository restaurantRepository = new RestaurantRepository(); Restaurant dbRestaurant = restaurantRepository.GetByID(viewModel.ID); if (dbRestaurant == null) { // this is a new restaurant so create a new object dbRestaurant = new Restaurant(); dbRestaurant.DateCreated = DateTime.Now; } dbRestaurant.CategoryID = viewModel.CategoryID; dbRestaurant.CityID = viewModel.CityID; dbRestaurant.Name = viewModel.Name; dbRestaurant.Description = viewModel.Description; dbRestaurant.ImageName = viewModel.ImageName; // save the image to the local folder if (Request.Files.Count > 0) { HttpPostedFileBase file = Request.Files[0]; string imagesPath = Server.MapPath(Constants.ImagesDirectory); string uniqueFileName = string.Format("{0}_{1}", DateTime.Now.Millisecond, file.FileName); string savedFileName = Path.Combine(imagesPath, Path.GetFileName(uniqueFileName)); file.SaveAs(savedFileName); dbRestaurant.ImageName = uniqueFileName; } restaurantRepository.Save(dbRestaurant); TempData["Message"] = "The restaurant was saved successfully"; return(RedirectToAction("Index")); }
public Restaurant GetRestaurant() { RestaurantRepository restaurantRepository = new RestaurantRepository(); return(restaurantRepository.GetByID(RestaurantID)); }