public async Task <bool> AddSupplementToCartAsync(int supplementId, ShoppingCart shoppingCart) { Supplement supplement = await this.database .Supplements .Include(s => s.Manufacturer) .Where(s => s.Id == supplementId) .FirstOrDefaultAsync(); if (supplement.Quantity == 0) { return(false); } if (shoppingCart.Supplements.Any(s => s.Id == supplementId)) { shoppingCart.Supplements.Where(s => s.Id == supplementId).FirstOrDefault().Quantity += 1; } else { SupplementInCartServiceModel supplementInCart = Mapper.Map <SupplementInCartServiceModel>(supplement); supplementInCart.Quantity = 1; shoppingCart.Supplements.Add(supplementInCart); } return(true); }
public async Task AddSupplementToCartAsync_WithSupplementId_ShouldIncrementQuantityInCartAndReturnTrue() { // Arrange FitStoreDbContext database = this.Database; DatabaseHelper.SeedData(database); Supplement supplement = database.Supplements.Find(supplementId); SupplementInCartServiceModel supplementInCart = new SupplementInCartServiceModel { Id = supplement.Id, Name = supplement.Name, Quantity = supplement.Quantity, Price = supplement.Price, ManufacturerName = supplement.Manufacturer.Name }; ShoppingCart shoppingCart = new ShoppingCart(); shoppingCart.Supplements.Add(supplementInCart); IOrderService orderService = new OrderService(database); // Act bool result = await orderService.AddSupplementToCartAsync(supplementId, shoppingCart); // Assert result.Should().Be(true); supplementInCart.Quantity.Should().Be(supplement.Quantity + 1); }
public async Task FinishOrderAsync_WithQuantityLessThanAvailable_ShouldReturnFalse() { // Arrange FitStoreDbContext database = this.Database; DatabaseHelper.SeedData(database); Supplement supplement = database.Supplements.Find(supplementId); SupplementInCartServiceModel supplementInCart = new SupplementInCartServiceModel { Id = supplement.Id, Name = supplement.Name, Quantity = 10, Price = supplement.Price, ManufacturerName = supplement.Manufacturer.Name }; ShoppingCart shoppingCart = new ShoppingCart(); shoppingCart.Supplements.Add(supplementInCart); IOrderService orderService = new OrderService(database); // Act bool result = await orderService.FinishOrderAsync(userId, shoppingCart); // Assert result.Should().Be(false); }
public async Task IsLastAvailableSupplementAlreadyAdded_WithSupplementIdAndShoppingCartWithSupplement_ShouldReturnTrue() { // Arrange FitStoreDbContext database = this.Database; DatabaseHelper.SeedData(database); Supplement supplement = database.Supplements.Find(supplementId); SupplementInCartServiceModel supplementInCart = new SupplementInCartServiceModel { Id = supplement.Id, Name = supplement.Name, Quantity = 1, Price = supplement.Price, ManufacturerName = supplement.Manufacturer.Name }; ShoppingCart shoppingCart = new ShoppingCart(); shoppingCart.Supplements.Add(supplementInCart); IOrderService orderService = new OrderService(database); // Act bool result = await orderService.IsLastAvailableSupplementAlreadyAdded(supplementId, shoppingCart); // Assert result.Should().Be(true); }
public void RemoveAllSupplementsFromCartAsync_WithSupplementId_ShouldRemoveSupplementFromCartAndReturnTrue() { // Arrange FitStoreDbContext database = this.Database; DatabaseHelper.SeedData(database); Supplement supplement = database.Supplements.Find(supplementId); SupplementInCartServiceModel supplementInCart = new SupplementInCartServiceModel { Id = supplement.Id, Name = supplement.Name, Quantity = 10, Price = supplement.Price, ManufacturerName = supplement.Manufacturer.Name }; ShoppingCart shoppingCart = new ShoppingCart(); shoppingCart.Supplements.Add(supplementInCart); IOrderService orderService = new OrderService(database); // Act bool result = orderService.RemoveAllSupplementsFromCartAsync(supplementId, shoppingCart); // Assert result.Should().Be(true); shoppingCart.Supplements.Count().Should().Be(0); }
public bool RemoveAllSupplementsFromCartAsync(int supplementId, ShoppingCart shoppingCart) { if (shoppingCart.Supplements.Any(s => s.Id == supplementId)) { SupplementInCartServiceModel supplemenInCart = shoppingCart .Supplements .Where(s => s.Id == supplementId) .FirstOrDefault(); shoppingCart.Supplements.Remove(supplemenInCart); return(true); } return(false); }
public async Task <bool> IsLastAvailableSupplementAlreadyAdded(int supplementId, ShoppingCart shoppingCart) { Supplement supplement = await this.database .Supplements .Where(s => s.Id == supplementId) .FirstOrDefaultAsync(); SupplementInCartServiceModel supplementInCart = shoppingCart .Supplements .Where(s => s.Id == supplementId) .FirstOrDefault(); if (supplementInCart == null) { return(false); } return(supplement.Quantity == supplementInCart.Quantity); }