public async Task <IActionResult> DeleteCategory([FromRoute(Name = "category_id")] Guid categoryId) { // validate input if (!ModelState.IsValid) { return(HttpResult.BadRequest(ModelState)); } // locate category var foundCategory = await _bagsContext.TagCategories .Where(category => category.Id == categoryId) .SingleOrDefaultAsync(); if (foundCategory == null) { return(HttpResult.NoContent()); } // validate it doesn't have any tags if (foundCategory.Tags.Any()) { return(HttpResult.Conflict("Category must have no associated tags before it can be deleted.")); } // delete _bagsContext.TagCategories.Remove(foundCategory); await _bagsContext.SaveChangesAsync(); return(HttpResult.NoContent()); }
public async Task <IActionResult> DeleteTag([FromRoute(Name = "tag_id")] Int32 tagId) { // validate input if (!ModelState.IsValid) { return(HttpResult.BadRequest(ModelState)); } // locate tag var foundTag = await _bagsContext.Tags .Include(tag => tag.Products) .Where(tag => tag.Id == tagId) .SingleOrDefaultAsync(); if (foundTag == null) { return(HttpResult.NoContent()); } // validate it doesn't have any products if (foundTag.Products.Any()) { return(HttpResult.Conflict("Tag must have no associated products before it can be deleted.")); } // delete _bagsContext.Tags.Remove(foundTag); await _bagsContext.SaveChangesAsync(); return(HttpResult.NoContent()); }
public async Task <IActionResult> RemoveTag([FromRoute(Name = "product_id")] Int32 productId, [FromRoute(Name = "tag_id")] Int32 tagId) { // validate input if (!ModelState.IsValid) { return(HttpResult.BadRequest(ModelState)); } // validate product var foundProduct = await _bagsContext.Products .Where(product => product.Id == productId) .SingleOrDefaultAsync(); if (foundProduct == null) { return(HttpResult.NotFound($"{productId}")); } // validate tag var foundTag = await _bagsContext.Tags .Where(tag => tag.Id == tagId) .SingleOrDefaultAsync(); if (foundTag == null) { return(HttpResult.NotFound($"{tagId}")); } // locate product tag var foundProductTag = await _bagsContext.ProductTags .Where(productTag => productTag.ProductId == foundProduct.Id && productTag.TagId == foundTag.Id) .SingleOrDefaultAsync(); if (foundProductTag == null) { return(HttpResult.NoContent()); } // delete _bagsContext.ProductTags.Remove(foundProductTag); await _bagsContext.SaveChangesAsync(); return(HttpResult.NoContent()); }
public async Task <IActionResult> DeleteProduct([FromRoute(Name = "product_id")] Int32 productId) { // validate input if (!ModelState.IsValid) { return(HttpResult.BadRequest(ModelState)); } // locate product var foundProduct = await _bagsContext.Products .Where(product => product.Id == productId) .SingleOrDefaultAsync(); if (foundProduct == null) { return(HttpResult.NoContent()); } // delete _bagsContext.Products.Remove(foundProduct); await _bagsContext.SaveChangesAsync(); return(HttpResult.NoContent()); }