public async Task AddAsync_AddOne_UpdateIfExists() { // Arrange Product product1 = new Product { Id = 1, ProductName = "Racing Car", CategoryId = 1, Description = "Nice Racing Car", ImagePath = "abc.png", UnitPrice = 23.1 }; var cartId = Guid.NewGuid().ToString(); CartItem cartItem = new CartItem { Id = Guid.NewGuid().ToString(), CartId = cartId, ProductId = product1.Id, Product = product1, Created = DateTime.Now, Quantity = 1 }; var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase("WingtipToysDB").Options); context.Products.Add(product1); context.CartItems.Add(cartItem); context.SaveChanges(); ICartService service = new CartService(context, _mapper); CartItemDto cartDto = new CartItemDto { CartId = cartId, ProductId = product1.Id, Quantity = 3 }; // Act await service.AddAsync(cartDto); // Assert var tempList = context.CartItems.ToList(); Assert.AreEqual(1, tempList.Count); Assert.AreEqual(cartItem.Id, tempList[0].Id); Assert.AreEqual(cartItem.CartId, tempList[0].CartId); Assert.AreEqual(cartItem.ProductId, tempList[0].ProductId); Assert.AreEqual(3, tempList[0].Quantity); // Clean data context.Products.RemoveRange(context.Products); context.CartItems.RemoveRange(context.CartItems); context.SaveChanges(); context.Dispose(); }
public async Task AddAsync_CartItemDto_AddOne() { string dbName = "AddAsync_CartItemDto_AddOne"; // Arrange Product product1 = new Product { Id = 1, ProductName = "Racing Car", CategoryId = 1, Description = "Nice Racing Car", ImagePath = "abc.png", UnitPrice = 23.1 }; var cartId = Guid.NewGuid().ToString(); var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase("WingtipToysDB").Options); context.Products.Add(product1); context.SaveChanges(); ICartService service = new CartService(context, _mapper); CartItemDto cartDto = new CartItemDto { CartId = cartId, ProductId = product1.Id, Quantity = 3 }; // Act string result = await service.AddAsync(cartDto); // Assert Assert.AreEqual(cartId, result); }
public async Task PatchAsync_Quantity_UpdateOne() { string dbName = "PatchAsync_Quantity_UpdateOne"; // Arrange Product product1 = new Product { Id = 1, ProductName = "Racing Car", CategoryId = 1, Description = "Nice Racing Car", ImagePath = "abc.png", UnitPrice = 23.1 }; var cartId = Guid.NewGuid().ToString(); CartItem cartItem = new CartItem { Id = Guid.NewGuid().ToString(), CartId = cartId, ProductId = product1.Id, Product = product1, Created = DateTime.Now, Quantity = 1 }; var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase(dbName).Options); context.Products.Add(product1); context.CartItems.Add(cartItem); context.SaveChanges(); ICartService service = new CartService(context, _mapper); // Act await service.PatchAsync(cartItem.CartId, cartItem.Id, 3); // Assert CartItem item = context.CartItems.FirstOrDefault(c => c.Id == cartItem.Id); Assert.IsNotNull(item); Assert.AreEqual(3, item.Quantity); // Clean data context.Products.RemoveRange(context.Products); context.CartItems.RemoveRange(context.CartItems); context.SaveChanges(); context.Dispose(); }
public int Add(OrderDto orderDto) { Order order = _mapper.Map <Order>(orderDto); // check products before update foreach (OrderDetail detail in order.OrderDetails) { Product product = _context.Products.AsNoTracking().FirstOrDefault(p => p.Id == detail.ProductId); if (product == null) { throw new KeyNotFoundException($"The product with ID={detail.ProductId} was not found."); } } order.HasBeenShipped = false; order.OrderDate = DateTime.Now; // order.PaymentTransactionId = ""; // TO DO _context.Orders.Add(order); _context.SaveChanges(); return(order.Id); }
public async Task DeleteAsync_CartId_ItemId_DeleteOne() { string dbName = "DeleteAsync_CartId_ItemId_DeleteOne"; // Arrange Product product1 = new Product { Id = 1, ProductName = "Racing Car", CategoryId = 1, Description = "Nice Racing Car", ImagePath = "abc.png", UnitPrice = 23.1 }; Product product2 = new Product { Id = 2, ProductName = "Luxery Car", CategoryId = 1, Description = "Very expencive CAr", ImagePath = "abcd.png", UnitPrice = 150.1 }; var cartId = Guid.NewGuid().ToString(); CartItem cartItem = new CartItem { Id = Guid.NewGuid().ToString(), CartId = cartId, ProductId = product1.Id, Created = DateTime.Now, Quantity = 1 }; CartItem cartItem2 = new CartItem { Id = Guid.NewGuid().ToString(), CartId = cartId, ProductId = product2.Id, Created = DateTime.Now, Quantity = 5 }; var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase(dbName).Options); context.Products.Add(product1); context.CartItems.Add(cartItem); context.Products.Add(product2); context.CartItems.Add(cartItem2); context.SaveChanges(); ICartService service = new CartService(context, _mapper); // Act await service.DeleteAsync(cartItem.CartId, cartItem.Id); // Assert var tempList = context.CartItems.ToList(); Assert.AreEqual(1, tempList.Count); // Clean data context.Products.RemoveRange(context.Products); context.CartItems.RemoveRange(context.CartItems); context.SaveChanges(); context.Dispose(); }