Exemplo n.º 1
0
        public async Task AddToBasketAsyncWorksCorrectlyWhenAddingDish()
        {
            await this.AddDishesToDB();

            var itemToAdd = new AddItemToBasketViewModel()
            {
                Type  = "Dish",
                Count = 3,
                Id    = "test",
            };

            await this.BasketService.AddToBasketAsync(itemToAdd, "1");

            Assert.Equal(1, this.DbContext.Baskets.Count());

            var itemToAdd2 = new AddItemToBasketViewModel()
            {
                Type  = "Dish",
                Count = 3,
                Id    = "test1",
            };

            await this.BasketService.AddToBasketAsync(itemToAdd2, UserId);

            var actual = this.DbContext.Baskets.FirstOrDefault(x => x.User.Id == UserId).Dishes;

            Assert.Equal(2, actual.Count());
        }
Exemplo n.º 2
0
        public async Task AddToBasketAsyncWorksCorrectlyWhenAddingExistingDrinks()
        {
            await this.AddDrinksToDB();

            int count = 3;

            var itemToAdd = new AddItemToBasketViewModel()
            {
                Type  = "Drink",
                Count = count,
                Id    = "test",
            };

            await this.BasketService.AddToBasketAsync(itemToAdd, UserId);

            await this.BasketService.AddToBasketAsync(itemToAdd, UserId);

            Assert.True(this.DbContext.Baskets.FirstOrDefault(x => x.User.Id == UserId).Drinks.Where(x => x.Quantity == count * 2).Any());
        }
Exemplo n.º 3
0
        // Adding item to basket (doesnt matter if dish or drink). Also creating the basket if non existing
        public async Task AddToBasketAsync(AddItemToBasketViewModel itemToAdd, string userId)
        {
            if (!this.basketRepository.AllAsNoTracking().Any(x => x.User.Id == userId))
            {
                await this.basketRepository.AddAsync(new Basket()
                {
                    Id   = userId,
                    User = this.userService.GetUserById(userId),
                });

                await this.basketRepository.SaveChangesAsync();
            }

            if (itemToAdd.Type.ToString().ToLower() == "drink")
            {
                var existingDrink = this.GetBasketDrinkInBasketById(userId, itemToAdd.Id);
                if (existingDrink != null)
                {
                    await this.AddQuantityToExistingDrinkAsync(existingDrink, itemToAdd.Count);
                }
                else
                {
                    await this.AddBasketDrinkItemAsync(userId, itemToAdd.Count, itemToAdd.Id);
                }
            }
            else if (itemToAdd.Type.ToString().ToLower() == "dish")
            {
                var existingDish = this.GetBasketDishInBasketById(userId, itemToAdd.Id);
                if (existingDish != null)
                {
                    await this.AddQuantityToExistingDishAsync(existingDish, itemToAdd.Count);
                }
                else
                {
                    await this.AddBasketDishItemAsync(userId, itemToAdd.Count, itemToAdd.Id);
                }
            }
        }
Exemplo n.º 4
0
        public async Task AddItemToBasket(AddItemToBasketViewModel addItem)
        {
            var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            await this.basketService.AddToBasketAsync(addItem, userId);
        }