Ejemplo n.º 1
0
        public async Task <bool> Handle(DeleteItemFromBasket request, CancellationToken cancellationToken)
        {
            var basket =
                (await _basketRepository.FindAsync(x => x.GoodsId == request.GoodsId && x.UserId == request.UserId)).Data;

            if (basket == null)
            {
                throw new DomainException(ErrorType.BasketDoesNotExist);
            }

            if (basket.Count < request.Count)
            {
                throw new DomainException(ErrorType.BasketItemCountLessThenNeed);
            }

            if (basket.Count == request.Count)
            {
                _basketRepository.Delete(basket);
            }

            else
            {
                basket.Count -= request.Count;
                _basketRepository.Update(basket);
            }

            await _basketRepository.SaveAsync().ConfigureAwait(false);

            return(true);
        }
Ejemplo n.º 2
0
        public void AddProductToBasket(int productId, int userId)
        {
            var basketItems = new List <BasketItem>();

            basketItems.Add(new BasketItem()
            {
                ProductId = productId, AmountOfProducts = 1
            });
            var basket = basketRepo.GetAll().Where(b => (b.IsClosed == false && b.UserId == userId)).FirstOrDefault();

            if (basket == null)
            {
                basketRepo.Insert(new Basket()
                {
                    IsClosed    = false,
                    UserId      = userId,
                    BasketItems = basketItems
                });
            }
            else
            {
                var editBasketItem = basket.BasketItems.Where(b => b.ProductId == productId).FirstOrDefault();
                if (editBasketItem == null)
                {
                    basket.BasketItems.Add(new BasketItem()
                    {
                        ProductId = productId, AmountOfProducts = 1
                    });
                }
                else
                {
                    editBasketItem.AmountOfProducts++;
                }

                basketRepo.Update(basket);
            }
        }
Ejemplo n.º 3
0
        public Basket Update(Basket entity)
        {
            _repo.Update(entity);

            return(entity);
        }