Example #1
0
 public IActionResult Post(Category category)
 {
     using var context = new ProjectWebApiContext();
     context.Categories.Add(category);
     context.SaveChanges();
     return(Created("", category));
 }
Example #2
0
        public IActionResult Delete(int id)
        {
            using var context = new ProjectWebApiContext();
            var deletedCategory = context.Categories.Find(id);

            if (deletedCategory == null)
            {
                return(NotFound());
            }
            context.Remove(deletedCategory);
            context.SaveChanges();
            return(NoContent());
        }
Example #3
0
        public IActionResult UpdateCategory(Category category)
        {
            using var context = new ProjectWebApiContext();
            var updatedCategory = context.Find <Category>(category.Id);

            if (updatedCategory == null)
            {
                return(NotFound());
            }
            updatedCategory.Name = category.Name;
            context.Update(updatedCategory);
            context.SaveChanges();
            return(NoContent());
        }