Beispiel #1
0
        public async Task <IActionResult> Update([FromBody] UpdateCategoryForm form)
        {
            var category = _ctx.Categories.FirstOrDefault(x => x.Id == form.Id);

            if (category == null)
            {
                return(NoContent());
            }

            var newCategory = new Category
            {
                Slug        = category.Slug,
                Name        = category.Name,
                Description = form.Description,
                Version     = category.Version + 1,
                UserId      = UserId,
            };

            _ctx.Add(newCategory);
            await _ctx.SaveChangesAsync();

            _ctx.ModerationItems.Add(new ModerationItem
            {
                Current = category.Id,
                Target  = newCategory.Id,
                UserId  = UserId,
                Type    = ModerationTypes.Category,
            });
            await _ctx.SaveChangesAsync();

            return(Ok());
        }
 public ActionResult Edit(int id, UpdateCategoryForm form)
 {
     if (ModelState.IsValid)
     {
         try
         {
             _categoryRepository.Update(form.Id, new Category(form.Name));
             return(RedirectToAction(nameof(Index)));
         }
         catch (Exception ex)
         {
             ViewBag.Exception = ex.Message;
         }
     }
     return(View(form));
 }
Beispiel #3
0
        public async Task <IActionResult> UpdateStaged([FromBody] UpdateCategoryForm form)
        {
            var category = _ctx.Categories
                           .FirstOrDefault(x => x.Id == form.Id);

            if (category == null)
            {
                return(NoContent());
            }
            if (category.UserId != UserId)
            {
                return(BadRequest("Can't edit this category."));
            }

            category.Description = form.Description;

            await _ctx.SaveChangesAsync();

            return(Ok());
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ICategoryRepository categoryRepository = validationContext.GetService <ICategoryRepository>();

            if (action == ValidationAction.Insert)
            {
                if (categoryRepository.Exists((string)value))
                {
                    return(new ValidationResult(ErrorMessage));
                }
            }
            else
            {
                UpdateCategoryForm updateCategoryForm = (UpdateCategoryForm)validationContext.ObjectInstance;
                if (categoryRepository.Exists(updateCategoryForm.Id, (string)value))
                {
                    return(new ValidationResult(ErrorMessage));
                }
            }

            return(ValidationResult.Success);
        }
Beispiel #5
0
        public async Task <IActionResult> UpdateCategory([FromBody] UpdateCategoryForm form)
        {
            var category = _context.Categories.FirstOrDefault(x => x.Id == form.Id);

            if (category == null)
            {
                return(NoContent());
            }

            var newCategory = new Category
            {
                Slug        = category.Slug,
                Name        = category.Name,
                Description = form.Description,
                Version     = category.Version + 1,
                UserId      = UserId,
            };

            // Add category to DB
            _context.Add(newCategory);

            // Saves changes async so it doesn't wait for actual saving time to DB, await prevents blocking UI
            await _context.SaveChangesAsync();

            _context.ModerationItems.Add(new ModerationItem
            {
                Current = category.Id,
                Target  = newCategory.Id,
                UserId  = UserId,
                Type    = ModerationTypes.Category,
            });

            await _context.SaveChangesAsync();

            return(Ok());
        }