public async Task <string> AddOrder(ShoppingCartVM shoppingCartVM, string userId, int shippingAddressId) { if (shoppingCartVM.Items.Count == 0) { return("-1"); } var order = new Order() { ApplicationUserId = userId, Id = Guid.NewGuid().ToString(), ShippingAddressId = shippingAddressId }; await _context.AddAsync(order); await _context.SaveChangesAsync(); foreach (var item in shoppingCartVM.Items) { var orderItem = new OrderMobilePhone() { OrderRef = order, OrderId = order.Id, MobilePhoneId = item.MobilePhoneId, MobilePhoneRef = item.MobilePhoneRef, Quantity = item.Quantity }; order.Items.Add(orderItem); } _context.Update(order); await _context.SaveChangesAsync(); return(order.Id); }
public async Task GetOrders_ManyOrders() { var serviceProvider = BuildInMemoryDBProvider(); using (var dbContext = serviceProvider.GetService <DatabaseContext>()) { //Arrange var user = new ApplicationUser() { Id = "1", }; var order = new Order() { Id = "1", Items = new List <OrderMobilePhone>(), ApplicationUserId = user.Id }; var order2 = new Order() { Id = "2", Items = new List <OrderMobilePhone>(), ApplicationUserId = user.Id }; var item = new OrderMobilePhone() { MobilePhoneId = 1, OrderId = "1", Quantity = 2 }; var item2 = new OrderMobilePhone() { MobilePhoneId = 2, OrderId = "1", Quantity = 1 }; order.Items.Add(item); order2.Items.Add(item2); dbContext.Add(order); dbContext.Add(order2); dbContext.Add(user); await dbContext.SaveChangesAsync(); var mockHttpContextAccessor = new Mock <IHttpContextAccessor>(); mockHttpContextAccessor.Object.HttpContext = new DefaultHttpContext(); var claims = new ClaimsPrincipal(); mockHttpContextAccessor.Setup(x => x.HttpContext.User).Returns(claims); List <ApplicationUser> _users = new List <ApplicationUser>(); var userManager = MockUserManager <ApplicationUser>(_users).Object; var orderRepository = new OrderRepository(dbContext); //Act var result = await orderRepository.GetOrders(user.Id).ToListAsync(); //Assert result.Should().HaveCount(2); result[0].Items.Should().HaveCount(1); result[1].Items.Should().HaveCount(1); result[0].ApplicationUserId.Should().Be(user.Id); result[1].ApplicationUserId.Should().Be(user.Id); } }