Example #1
0
        public async Task <ActionResult <User> > GetByUsernameAsync(string username)
        {
            var existingUser = await _queries.FindByUsernameAsync(username);

            if (existingUser == null)
            {
                return(NotFound());
            }
            return(existingUser);
        }
Example #2
0
        public async Task <ActionResult <IEnumerable <MedicalAppointmentViewModel> > > GetAllByUsernameAsync(string username)
        {
            var existingUser = await _userQueries.FindByUsernameAsync(username);

            if (existingUser == null)
            {
                return(NotFound());
            }
            return(await _queries.FindByUsernameAsync(username));
        }
Example #3
0
        public async Task <ActionResult <ShoppingCartViewModel> > AddProductShoppingCartAsync(ShoppingCartCommand shoppingCartCommand)
        {
            var existingProduct = await _queries.FindByIdAsync(shoppingCartCommand.ProductId);

            var existingUser = await _userQueries.FindByUsernameAsync(shoppingCartCommand.Username);

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

            var existingProductInShoppingCart = await _shoppingCartQueries.FindByIdUsernameAsync(shoppingCartCommand.ProductId, shoppingCartCommand.Username);

            if (existingProductInShoppingCart != null)
            {
                return(Conflict());
            }

            var shoppingCart = _mapper.Map <ShoppingCart>(shoppingCartCommand);

            await _shoppingCartBehavior.CreateShoppingCartAsync(shoppingCart, existingProduct.Price);

            var shoppingCartViewModel = await _shoppingCartQueries.FindByIdAsync(shoppingCart.Id);

            return(shoppingCartViewModel);
        }
Example #4
0
        public async Task <ActionResult <User> > CreateUserAsync(CreateUserCommand createUserCommand)
        {
            var existingUser = await _queries.FindByUsernameAsync(createUserCommand.Username);

            if (existingUser != null)
            {
                return(Conflict());
            }

            var user = _mapper.Map <User>(createUserCommand);
            await _behavior.CreateUserAsync(user);

            return(user);
        }
        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);
        }