public async Task <IActionResult> DeleteProductFromCategory(int productId, int categoryId) { ItemToCategory itemToCategory = await db.ItemsToCategories.SingleOrDefaultAsync(p => (p.ItemId == productId) && (p.CategoryId == categoryId)); if (itemToCategory != null) { db.ItemsToCategories.Remove(itemToCategory); await db.SaveChangesAsync(); await logger.AddToLogAsync($"Product ID:{itemToCategory.ItemId} deleted from category ID:{itemToCategory.CategoryId}"); } return(RedirectToRoute("AdminSelectToAddToCategory", new { categoryId })); }
public async void DeleteProductFromCategory() { Item basicItem = new Item { Price = 15, Title = "War never changes", Description = "The Romans waged war to gather slaves and wealth.", PictureFilePath = "VaultBoy.png", }; Category category = new Category { Name = "Why Im antisocial" }; ItemToCategory itemToCategory = new ItemToCategory { Item = basicItem, Category = category }; fixture.db.Items.Add(basicItem); fixture.db.Categories.Add(category); fixture.db.ItemsToCategories.Add(itemToCategory); await fixture.db.SaveChangesAsync(); basicItem = await fixture.db.Items.AsNoTracking().FirstOrDefaultAsync(p => p.Title == basicItem.Title); category = await fixture.db.Categories.AsNoTracking().FirstOrDefaultAsync(p => p.Name == category.Name); itemToCategory = await fixture.db.ItemsToCategories.AsNoTracking().SingleOrDefaultAsync(p => (p.ItemId == basicItem.Id) && (p.CategoryId == category.Id)); await controller.DeleteProductFromCategory(basicItem.Id, category.Id); var actual = await fixture.db.ItemsToCategories.SingleOrDefaultAsync(p => (p.ItemId == basicItem.Id) && (p.CategoryId == category.Id)); Assert.NotNull(itemToCategory); Assert.Null(actual); }