public IActionResult DeleteCategory(GenericDeleteItemModel model) { var categoryExists = Db.Categories.Where(q => q.Id == model.Id).Any(); if (!categoryExists) { return(NotFound()); } var safeIdParameter = new SqlParameter("categoryId", model.Id); var deleteCommand = "DELETE FROM dbo.Items Where CategoryId = @categoryId;"; var rowsDeleted = Db.Database.ExecuteSqlRaw(deleteCommand, safeIdParameter); var success = (rowsDeleted > 0); if (success) { this.AddMessage(MessageType.Success, string.Format(LibBus.SUCCESS_CATEGORY_DELETED, rowsDeleted, (Business.Models.Category.CategoryType)model.Id)); } return(Ok(new { success, deletedCategoryId = model.Id })); }
public async Task <IActionResult> DeleteItem(GenericDeleteItemModel model) { var itemExists = Db.Items.Where(q => q.Id == model.Id).Any(); if (!itemExists) { return(NotFound()); } // I could put this First() call in the Remove() but I want a reference to it so the user knows their last action. var itemToDelete = Db.Items.First(q => q.Id == model.Id); Db.Items.Remove(itemToDelete); var success = (await Db.SaveChangesAsync() == 1); if (success) { this.AddMessage(MessageType.Success, string.Format(LibBus.SUCCESS_ITEM_DELETED, itemToDelete.Name, (Business.Models.Category.CategoryType)itemToDelete.CategoryId)); } return(Ok(new { success, deletedItemId = model.Id })); }