Exemplo n.º 1
0
        public async Task <ActionResult <BucketResponseDTO> > UpdateBucket([FromRoute] Guid userId, BucketRequestDTO bucketRequest)
        {
            var bucket = _mapper.Map <Bucket>(bucketRequest);

            bucket.UserId = userId;

            // проверка наличия заказываемого количества товаров
            _warehouseServiceClient.AddHeader(Constants.USERID_HEADER, userId.ToString());
            var allProducts = await _warehouseServiceClient.ProductInfoAsync(bucket.Items.Select(g => g.ProductId).ToArray());

            foreach (var pr in bucket.Items)
            {
                var product = allProducts.FirstOrDefault(g => g.Id == pr.ProductId);
                if (product == null || pr.Quantity > product.RemainCount)
                {
                    return(BadRequest());
                }
            }

            var updateBucket = await _bucketRepository.UpdateBucketsAsync(bucket, userId);

            (decimal totalPrice, decimal discount)price = await _orderService.CalculateTotalPriceAsync(updateBucket, userId, true);

            var bucketsDTO = _mapper.Map <BucketResponseDTO>(updateBucket);

            bucketsDTO.SummaryPrice = price.totalPrice;
            bucketsDTO.Discount     = price.discount;
            return(Ok(bucketsDTO));
        }