コード例 #1
0
        public async Task <IActionResult> CreateCategory([FromBody] CreateCategoryForm form)
        {
            var category = new Category
            {
                Slug        = form.Name.Replace(" ", "-").ToLowerInvariant(),
                Name        = form.Name,
                Description = form.Description,
                Version     = 1,
                UserId      = UserId
            };

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

            // 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
            {
                Target = category.Id,
                UserId = UserId,
                Type   = ModerationTypes.Category,
            });

            await _context.SaveChangesAsync();

            return(Ok());
        }
 public ActionResult Create(CreateCategoryForm form)
 {
     if (ModelState.IsValid)
     {
         try
         {
             _categoryRepository.Insert(new Category(form.Name));
             return(RedirectToAction(nameof(Index)));
         }
         catch (Exception ex)
         {
             ViewBag.Exception = ex.Message;
         }
     }
     return(View(form));
 }
コード例 #3
0
        public ActionResult <CategoryShallowDTO> Post([FromBody] CreateCategoryForm category)
        {
            if (string.IsNullOrEmpty(category.Name))
            {
                return(BadRequest());
            }

            var cat = new Category
            {
                Name = category.Name
            };

            _context.Categories.Add(cat);
            _context.SaveChanges();

            return(Ok(new CategoryShallowDTO(cat)));
        }
コード例 #4
0
        public ActionResult <CategoryShallowDTO> Put(long id, [FromBody] CreateCategoryForm category)
        {
            if (string.IsNullOrEmpty(category.Name))
            {
                return(BadRequest());
            }

            var cat = _context.Categories.FirstOrDefault(x => x.Id == id);

            if (cat != null)
            {
                cat.Name = category.Name;
                _context.Categories.Update(cat);
                _context.SaveChanges();

                return(Ok(new CategoryShallowDTO(cat)));
            }
            else
            {
                return(NoContent());
            }
        }
コード例 #5
0
        public async Task <IActionResult> Create([FromBody] CreateCategoryForm form)
        {
            var category = new Category
            {
                Slug        = form.Name.Replace(" ", "-").ToLowerInvariant(),
                Name        = form.Name,
                Description = form.Description,
                Version     = 1,
                UserId      = UserId,
            };

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

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

            return(Ok());
        }