示例#1
0
        public async Task <ResponseEntity <bool> > DecreaseProductFromBasket(EditProductToBasketInputDTO input)
        {
            var response = new ResponseEntity <bool>();

            try
            {
                var basketDetail = await(from b in _basketRepository.Table
                                         join bd in _basketDetailRepository.Table on b.Id equals bd.BasketId
                                         where bd.ProductId == input.ProductId && (b.CustomerId == input.CustomerId || b.BasketKey == input.BasketKey)
                                         select bd).FirstOrDefaultAsync();
                if (basketDetail != null && basketDetail.Quantity > (int)decimal.One)
                {
                    basketDetail.Quantity--;
                    await _basketDetailRepository.UpdateAsync(basketDetail);
                }
                else if (basketDetail != null && basketDetail.Quantity == (int)decimal.One)
                {
                    await _basketDetailRepository.DeleteAsync(basketDetail);
                }
                response.Result = true;
            }
            catch (Exception ex)
            {
                response.Exception    = ex;
                response.ErrorMessage = ex.Message;
                response.HttpCode     = HttpStatusCode.InternalServerError;
            }
            return(response);
        }
示例#2
0
        public async Task <ResponseEntity <bool> > AddProductToBasket(EditProductToBasketInputDTO input)
        {
            var response = new ResponseEntity <bool>();

            try
            {
                var basketDetail = await(from b in _basketRepository.Table
                                         join bd in _basketDetailRepository.Table on b.Id equals bd.BasketId
                                         where bd.ProductId == input.ProductId && (b.CustomerId == input.CustomerId || b.BasketKey == input.BasketKey)
                                         select bd).FirstOrDefaultAsync();
                if (basketDetail != null)
                {
                    basketDetail.Quantity++;
                    await _basketDetailRepository.UpdateAsync(basketDetail);

                    response.Result = true;
                    return(response);
                }
                var basket = await _basketRepository.FirstAsync(x => x.CustomerId == input.CustomerId || x.BasketKey == input.BasketKey);

                if (basket == null)
                {
                    basket = new Basket
                    {
                        BasketKey  = input.BasketKey,
                        CustomerId = input.CustomerId
                    };
                    await _basketRepository.InsertAsync(basket);
                }

                await _basketDetailRepository.InsertAsync(new BasketDetail
                {
                    BasketId  = basket.Id,
                    ProductId = input.ProductId,
                    Quantity  = (int)decimal.One
                });

                response.Result = true;
            }
            catch (Exception ex)
            {
                response.Exception    = ex;
                response.ErrorMessage = ex.Message;
                response.HttpCode     = HttpStatusCode.InternalServerError;
            }
            return(response);
        }
 public async Task <IActionResult> DeletProductFromBasket(EditProductToBasketInputDTO input)
 {
     return(HttpEntity(await _basketService.DeleteProductFromBasket(input)));
 }