public async Task <int> EditCategoryAsync(CategoryEditBindingModel model)
        {
            Validator.ThrowIfNull(model);
            var categoryFromDb = await this.DbContext.Categories.FindAsync(model.Id);

            categoryFromDb.Name        = model.Name;
            categoryFromDb.Order       = model.Order;
            categoryFromDb.Title       = model.Title;
            categoryFromDb.Description = model.Description;
            this.DbContext.Categories.Update(categoryFromDb);
            await this.DbContext.SaveChangesAsync();

            return(categoryFromDb.Id);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(CategoryEditBindingModel model)
        {
            bool categoryExists = await this.categoriesService.CheckIfCategoryExistsAsync(model.Id);

            if (!categoryExists)
            {
                return(this.ProcessNullEntity(nameof(Category)));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            model = TSelfExtensions.TrimStringProperties(model);
            var categoryNameWithSameOrder = await this.categoriesService
                                            .CategoryWithSameOrderAsync(model.Id, model.Order);

            if (categoryNameWithSameOrder != null)
            {
                this.ModelState.AddModelError(
                    nameof(model.Order),
                    $"{nameof(Category)} \"{categoryNameWithSameOrder}\" already has order \"{model.Order}\".");
                return(this.View(model));
            }

            int id = await this.categoriesService.EditCategoryAsync(model);

            var messageModel = new MessageModel()
            {
                Type    = MessageType.Success,
                Message = string.Format(Messages.EntityEditSuccess, nameof(Category), id)
            };

            TempDataExtensions.Put(this.TempData, Constants.TempDataKey, messageModel);

            return(this.RedirectToAction("Details", "Categories", new { id = id, Area = "Admin" }));
        }
        public IActionResult <CategoryViewModel> Edit(HttpSession session, HttpResponse response, CategoryEditBindingModel cebm)
        {
            if (!this.signInManager.IsAuthenticated(session))
            {
                //TODO: switch to home
                Redirect(response, "/forum/login");
            }
            Category cat = this.data.Categories.FindByPredicate(c => c.Name == cebm.OldCatName);

            cat.Name = cebm.NewCatName;
            this.data.SaveChanges();
            this.Redirect(response, "/categories/all");
            return(null);
        }