public async Task <ActionResult> EditAsync(long?id, FoodVM model) { if (id == null) { return(RedirectWithMessage(nameof(Index), "Missing id")); } Food current = await _calories.GetFoodAsync(id.Value); if (current == null) { return(RedirectWithMessage(nameof(Index), "Can't find food with id {0}", id.Value)); } if (model.Name != current.Name && await _calories.FoodExistsAsync(model.Name)) { ModelState.AddModelError(nameof(model.Name), $"Food {model.Name} alredy exists"); } if (!ModelState.IsValid) { return(View(model)); } await _calories.EditFoodAsync(current, model.Name, model.Calories, model.Unit); return(RedirectWithMessage(nameof(Index), "Food {0} has been updated", current.Name)); }
public ActionResult DeleteMeal(Meal meal, int intDayID, int intUserID) { FoodVM model = new FoodVM() { LstMealsForDay = MealDAL.GetMealsByDayAndUser(intDayID, intUserID).Where(m => m != meal) }; return(View("Food", model)); }
public ActionResult FoodUpdate(FoodVM model, IEnumerable <HttpPostedFileBase> file) { try { string physicalPath = "~/images/Food/"; int maxFileSize = 500000; Dictionary <FileResultItem, FileResultType> resultModel = FileDocumentUpload(file, maxFileSize, physicalPath, new string[] { "image/gif", "image/png", "image/jpeg", "image/pjpeg", "image/bmp", "image/x-png", "image/jpg" }); foreach (var item in resultModel) { if (item.Value == FileResultType.Error || item.Value == FileResultType.NoneFile || item.Value == FileResultType.SizeOver || item.Value == FileResultType.WrongType) { RemoveAll(resultModel.Keys, physicalPath); TempData["NoteCss"] = "warning"; TempData["NoteText"] = ControlMessages(item.Value, maxFileSize).Keys.FirstOrDefault().ToString(); return(View("FoodUpdate")); } } foreach (var item in resultModel.Keys) { model.food.ImageURL = item.UploadPath; } } catch (Exception ex) { TempData["NoteCss"] = "danger"; TempData["NoteText"] = "Bilinmeyen Hata!"; TempData["NoteError"] = ex.Message; } model.food.IsActive = true; model.food.UpdatedDate = DateTime.Now; model.food.CreatedDate = DateTime.Now; model.food.CreatedByID = 1; var oldFood = _unitOfWork.FoodRepository.Find(x => x.ID == model.food.ID); if (ModelState.IsValid) { List <FileResultItem> fileResultItems = new List <FileResultItem> { new FileResultItem { UploadPath = oldFood.ImageURL } }; RemoveAll(fileResultItems, "~/images/Food/"); _unitOfWork.FoodRepository.Detach(oldFood); _unitOfWork.FoodRepository.Update(model.food); _unitOfWork.Save(); } return(RedirectToAction("FoodList")); }
public async Task <ActionResult> CreateAsync([Bind("Name", "Calories", "Unit")] FoodVM model) { if (await _calories.FoodExistsAsync(model.Name)) { ModelState.AddModelError(nameof(model.Name), $"Food {model.Name} alredy exists"); } if (!ModelState.IsValid) { return(View(model)); } Person who = await _auth.GetCurrentPersonAsync(User); Food created = await _calories.AddFoodAsync(who, model.Name, model.Calories, model.Unit); return(RedirectWithMessage(nameof(Index), "Food {0} was created", created.Name)); }
public ActionResult FoodHome() { if (HomeController.USER_NUMBER == -1) { return(RedirectToAction("NewUser", "Home")); } else { User user = UserDAL.GetUser(HomeController.USER_NUMBER); Day day = DayDAL.GetDayByUserAndDay(user.intUserID); FoodVM model = new FoodVM() { user = user, LstMealsForDay = MealDAL.GetMealsByDayAndUser(day.intDayID, user.intUserID) }; return(View("FoodHome", model)); } }
public ActionResult FoodAdd(FoodVM model, IEnumerable <HttpPostedFileBase> file) { try { string physicalPath = "~/images/Food/"; int maxFileSize = 500000; Dictionary <FileResultItem, FileResultType> resultModel = FileDocumentUpload(file, maxFileSize, physicalPath, new string[] { "image/gif", "image/png", "image/jpeg", "image/pjpeg", "image/bmp", "image/x-png", "image/jpg" }); foreach (var item in resultModel) { if (item.Value == FileResultType.Error || item.Value == FileResultType.NoneFile || item.Value == FileResultType.SizeOver || item.Value == FileResultType.WrongType) { RemoveAll(resultModel.Keys, physicalPath); TempData["NoteCss"] = "warning"; TempData["NoteText"] = ControlMessages(item.Value, maxFileSize).Keys.FirstOrDefault().ToString(); return(View("FoodAdd")); } } foreach (var item in resultModel.Keys) { _unitOfWork.FoodRepository.Insert(new Food { Name = model.food.Name, ImageURL = item.UploadPath, Description = model.food.Description, CreatedDate = DateTime.Now, CarbValue = model.food.CarbValue, CreatedByID = 1, IsActive = true, UpdatedDate = DateTime.Now }); _unitOfWork.Save(); } } catch (Exception ex) { TempData["NoteCss"] = "danger"; TempData["NoteText"] = "Bilinmeyen Hata!"; TempData["NoteError"] = ex.Message; } return(RedirectToAction("FoodList")); }
public FoodController() { _unitOfWork = new UnitOfWork(); _foodVM = new FoodVM(); }