Beispiel #1
0
        public async Task <ActionResult <ProductViewModel> > GetByIdAsync(int id)
        {
            var existingProduct = await _queries.FindByIdAsync(id);

            if (existingProduct == null)
            {
                return(NotFound());
            }

            return(existingProduct);
        }
        public async Task <ActionResult <IEnumerable <DetailSpecificationProductViewModel> > > GetAllByProductIdAsync(int productId)
        {
            var existingProduct = await _productQueries.FindByIdAsync(productId);

            if (existingProduct == null)
            {
                return(NotFound());
            }

            return(await _queries.FindAllByProductIdAsync(productId));
        }
        public async Task <ActionResult <OrderViewModel> > CreateOrderAsync(CreateDetailOrderCommand createDetailOrderCommand)
        {
            if (createDetailOrderCommand.shoppingCarts.Count == 0)
            {
                return(BadRequest());
            }

            // Validation of resources

            var existingUser = await _userQueries.FindByUsernameAsync(createDetailOrderCommand.createOrderCommand.Username);

            if (existingUser == null)
            {
                return(NotFound("The user doesn't exist"));
            }

            double totalOrder = 0;

            for (int i = 0; i < createDetailOrderCommand.shoppingCarts.Count; i++)
            {
                var shoppingCartId = createDetailOrderCommand.shoppingCarts[i];

                var existingShoppingCart = await _shoppingCartQueries.FindByIdAsync(shoppingCartId);

                if (existingShoppingCart == null)
                {
                    return(NotFound("One or more of the shopping carts doesn't exist"));
                }

                // Calculate the total of the order

                totalOrder += existingShoppingCart.Total;

                // Validation if the product of the shoppingCart has enough amount according to the shoppinCart amount.

                var existingProduct = await _productQueries.FindByIdAsync(existingShoppingCart.ProductId);

                if (existingProduct == null)
                {
                    return(NotFound("The product doesn't exist"));
                }

                if (!Validations.IsPossibleToBuy(existingProduct, existingShoppingCart))
                {
                    return(BadRequest("There is no enough amount of some product to buy"));
                }
            }

            // Verify if the Totals are the same

            if (totalOrder != createDetailOrderCommand.createOrderCommand.Total)
            {
                return(BadRequest("The total of the order is wrong"));
            }

            // Creating the order

            var order = _mapper.Map <Order>(createDetailOrderCommand.createOrderCommand);

            await _behavior.CreateOrderAsync(order);

            // Creating the detailOrders

            for (int i = 0; i < createDetailOrderCommand.shoppingCarts.Count; i++)
            {
                var shoppingCartId = createDetailOrderCommand.shoppingCarts[i];

                var shoppingCartViewModel = await _shoppingCartQueries.FindByIdAsync(shoppingCartId);

                var productViewModel = await _productQueries.FindByIdAsync(shoppingCartViewModel.ProductId);

                Product      product      = _mapper.Map <Product>(productViewModel);
                ShoppingCart shoppingCart = _mapper.Map <ShoppingCart>(shoppingCartViewModel);

                var productId = product.Id;
                var orderId   = order.Id;
                var amount    = shoppingCart.Amount;
                var total     = shoppingCart.Total;

                // Discount amount of a product

                await _productBehavior.UpdateAmountProductAsync(product, amount);

                // Delete shoppingCart register

                await _shoppingCartBehavior.DeleteShoppingCartAsync(shoppingCart);

                // Create detailOrder

                await _detailOrderBehavior.CreateDetailOrderAsync(productId, orderId, amount, total);
            }

            var orderViewModel = await _queries.FindByIdAsync(order.Id);

            return(orderViewModel);
        }