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 GetWithBlogsById(int id)
        {
            using var context = new ProjectWebApiContext();
            var categoryWithBlogs = context.Categories.Where(I => I.Id == id).Include(I => I.Blogs).ToList();

            if (categoryWithBlogs == null)
            {
                return(NotFound());
            }
            return(Ok(categoryWithBlogs));
        }
Example #3
0
        public IActionResult Get(int id)
        {
            using var context = new ProjectWebApiContext();
            var category = context.Categories.Find(id);

            if (category == null)
            {
                return(NotFound());
            }
            return(Ok(category));
        }
Example #4
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 #5
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());
        }
Example #6
0
 public IActionResult Get()
 {
     using var context = new ProjectWebApiContext();
     return(Ok(context.Categories.ToList()));
 }