public async Task <IActionResult> Edit(CategorySetModel categorySetModel) { if (this.ModelState.IsValid) { var category = await this.categoryService.FindAsync(categorySetModel.Id); category.Name = categorySetModel.Name; category.Description = categorySetModel.Description; if (categorySetModel.FormFile != null) { string fileName = $"\\images\\{Path.GetRandomFileName()}.jpg"; string filePath = string.Concat(env.WebRootPath, fileName); using (var stream = System.IO.File.Create(filePath)) { categorySetModel.FormFile.CopyTo(stream); category.ImageUrl = fileName; } } await this.categoryService.SaveAsync(category); return(Ok()); } return(BadRequest("Model is not valid")); }
public async Task <IActionResult> Create(CategorySetModel categorySetModel) { if (this.ModelState.IsValid) { if (categorySetModel.FormFile != null) { string fileName = $"\\images\\{Path.GetRandomFileName()}.jpg"; string filePath = string.Concat(env.WebRootPath, fileName); using (var stream = System.IO.File.Create(filePath)) { categorySetModel.FormFile.CopyTo(stream); categorySetModel.ImageUrl = fileName; } } var category = mapper.Map <Category>(categorySetModel); await this.categoryService.CreateAsync(category); return(RedirectToAction(nameof(CategoryController.All))); } return(View(categorySetModel)); }
public async Task SetMenuSectionsFromCategories(CategorySetModel input) { var menu = _menuRepository.FirstOrDefault(input.MenuId); foreach (var inputAvailableCategory in input.AvailableCategories) { if (inputAvailableCategory.Checked) { //GetCategory var category = _categoryRepository.FirstOrDefault(a => a.Id == inputAvailableCategory.CategoryId); //GetCategoryContent var contents = _categoryContentRepository.GetAllList(a => a.CategoryId == inputAvailableCategory.CategoryId); //If the category has no contents throw an error if (!contents.Any()) { throw new UserFriendlyException(L("CategoryHasNoContent")); } //Create a new section with the name of the category var section = await CreateMenuSection(category, menu); //Now we add the translations for each category (the categories are going to be converted to sections) await CreateSectionContents(contents, section); //Now we get each page in the category and set the menu items await SetMenuItemsFromPagesInCategory(category, section); } else { await RemoveItemsFromCategory(inputAvailableCategory.CategoryId); } } }
public async Task <IActionResult> Edit(CategorySetModel input) => await this.Handle( async() => await this.categoryService.Edit(input.Id, input), success : RedirectToAction(nameof(CategoryController.All)), failure : View(input));