Esempio n. 1
0
        public async Task <IActionResult> PerformPurchase([FromBody] JToken json)
        {
            var checkProducts = new List <CheckProduct>();

            //insert new check into DB
            var check = new CheckData();

            check.UtcTime = DateTime.UtcNow;
            await purchaseRepo.AddCheckData(check);

            //parse products info from JSON
            foreach (var item in json.Children().Children())
            {
                var productId = item.Value <int>("productId");
                var price     = item.Value <decimal>("price");
                var amount    = item.Value <int>("amount");

                checkProducts.Add(new CheckProduct
                {
                    ProductId = productId,
                    Price     = price,
                    Amount    = amount,
                    CheckId   = check.Id
                });
            }

            try
            {
                //insert info about bought products into DB
                await purchaseRepo.AddCheckProductsAsync(checkProducts);

                // return check to the client
                return(Ok(JsonConvert.SerializeObject(await ReturnCheck(check.Id))));
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.StackTrace);                 // something is bad
                return(new BadRequestResult());
            }
        }