Ejemplo n.º 1
0
        public async Task <IActionResult> Charge([FromHeader(Name = "Stripe-Token")] string stripeToken, OrderInfoDto customerInfo)
        {
            var basketJson = HttpContext.Session.GetString("Basket");

            if (string.IsNullOrEmpty(basketJson))
            {
                return(BadRequest("Basket Cookie is empty"));
            }

            var basketProductsCookie = JsonConvert.DeserializeObject <List <ProductFromBasketCookieDto> >(basketJson);

            if (!await _synchronizeBasket.Do(HttpContext.Session, basketProductsCookie) && !_synchronizeBasket.MissingStocks.Any())
            {
                return(BadRequest("Stocks in Basket are not able to be assign to that Session"));
            }

            var productsFromRepo = await _unitOfWork.StockOnHold.GetStockOnHoldWithProductForCharge(HttpContext.Session, basketProductsCookie);

            if (productsFromRepo.Count < 1)
            {
                return(BadRequest("Error occured during retrieving data products from Database"));
            }

            decimal basketPrice = _countOrderPrice.Do(productsFromRepo);

            var basketForPaymentDto = _createCharge.Do(stripeToken, basketPrice, basketProductsCookie);

            var order = _unitOfWork.Order.ScaffoldOrderForCreation(customerInfo, basketForPaymentDto);

            // var stockIdWithQtyToRemove = productsFromRepo.ToDictionary(s => s.StockId, s => s.StockQty);

            // await _unitOfWork.Stock.RemoveStockQty(stockIdWithQtyToRemove);

            await _unitOfWork.StockOnHold.DeleteRangeAsync(s => s.SessionId == HttpContext.Session.Id);

            _unitOfWork.Order.Add(order);

            if (await _unitOfWork.SaveAsync())
            {
                Response.Cookies.Delete("Basket");
                //TODO Replace that
                return(Ok(201));
            }


            return(BadRequest("Something went wrong during saving Order"));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <BasketDto> > GetBasket()
        {
            var basketJson = HttpContext.Session.GetString("Basket");

            if (string.IsNullOrEmpty(basketJson))
            {
                return(NotFound());
            }

            var basketCookie = JsonConvert.DeserializeObject <List <ProductFromBasketCookieDto> >(basketJson);


            var stocksWithProductsFromRepo = await _unitOfWork.Stock.GetStockWithProductForBasket(basketCookie);

            decimal basketPrice = _countOrderPrice.Do(stocksWithProductsFromRepo);

            var basketToReturn = new BasketDto()
            {
                BasketPrice    = basketPrice,
                BasketProducts = stocksWithProductsFromRepo
            };

            return(Ok(basketToReturn));
        }