예제 #1
0
 public IActionResult AddCategory(Category category)
 {
     using var context = new WebApiCrudContext();
     context.Categories.Add(category);
     context.SaveChanges();
     return(Created("", category));
 }
예제 #2
0
        public IActionResult Get(int id)
        {
            using var context = new WebApiCrudContext();
            var category = context.Categories.Find(id);

            if (category == null)
            {
                return(NotFound());
            }
            return(Ok(category));
        }
예제 #3
0
        public IActionResult GetWithBlogsById(int id)
        {
            using var context = new WebApiCrudContext();
            var categoryIdFind = context.Categories.Find(id);

            if (categoryIdFind == null)
            {
                return(NotFound());
            }
            var categorywithblogs = context.Categories.Where(x => x.Id == id).Include(x => x.Blogs).ToList();

            return(Ok(categorywithblogs));
        }
예제 #4
0
        public IActionResult DeleteCategory(int id)
        {
            using var context = new WebApiCrudContext();
            var deletedCategory = context.Categories.Find(id);

            if (deletedCategory == null)
            {
                return(NotFound());
            }
            context.Remove(deletedCategory);
            context.SaveChanges();
            return(NoContent());
        }
예제 #5
0
        public IActionResult UpdateCategory(Category c)
        {
            using var context = new WebApiCrudContext();
            var updatecategory = context.Categories.Find(c.Id);

            if (updatecategory == null)
            {
                return(NotFound());
            }
            updatecategory.Name = c.Name;
            context.Update(updatecategory);
            context.SaveChanges();
            return(NoContent());
        }
예제 #6
0
 public IActionResult  Get()
 {
     using var context = new WebApiCrudContext();
     return(Ok(context.Categories.ToList()));
 }