Пример #1
0
        public async Task <IActionResult> AddProductOrder(Guid orderId, ProductOrderAddDto productOrderAddDto)
        {
            //get user id from token and verify order ownership

            var product = await _productRepository.GetProductAsync(productOrderAddDto.ProductId);

            if (product == null)
            {
                return(BadRequest("Could not find product"));
            }

            var productOrder = new ProductOrder
            {
                OrderId   = orderId,
                Quantity  = productOrderAddDto.Quantity,
                UserId    = productOrderAddDto.UserId,
                ProductId = product.Id
            };
            await _productRepository.AddProductOrderAsync(productOrder);

            product.Stock -= productOrder.Quantity;

            if (!await _unitOfWOrk.SaveAsync())
            {
                return(BadRequest("Failed to add product to order"));
            }

            //should probably be updating the price every time we change a productorder
            return(NoContent());
        }
Пример #2
0
        public async Task <IActionResult> EditProductQuantity(Guid orderId, ProductOrderAddDto productOrderAddDto)
        {
            var product = await _productRepository.GetProductAsync(productOrderAddDto.ProductId);

            var productOrder = await _productRepository.GetProductOrderAsync(orderId, productOrderAddDto.ProductId);

            if (product == null || productOrder == null)
            {
                return(BadRequest("Could not find product"));
            }

            //if the items quantity is 0 remove the productorder
            if (productOrderAddDto.Quantity < 1)
            {
                product.Stock += productOrder.Quantity;
                _productRepository.RemoveProductOrder(productOrder);
                await _unitOfWOrk.SaveAsync();

                return(NoContent());
            }

            //re-add the old amount of products the user wanted
            product.Stock += productOrder.Quantity;
            if (productOrderAddDto.Quantity > product.Stock)
            {
                return(BadRequest($"Invalid quantity, there is only {product.Stock} in stock"));
            }

            // remove the new amount the user wants
            product.Stock        -= productOrderAddDto.Quantity;
            productOrder.Quantity = productOrderAddDto.Quantity;

            await _unitOfWOrk.SaveAsync();

            return(NoContent());
        }