Пример #1
0
        public async Task <ICommandResult> Handle(UpdateProductPriceCommand command)
        {
            command.Validate();

            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "Dados de entrada in válidos.", this));
            }

            var product = _productRepository.GetById(command.Id);

            if (product == null)
            {
                AddNotification("Produto", "Produto não cadastrado");
                return(new CommandResult(false, "Produto não cadastrado.", null));
            }

            if (command.TypeOperation == TypeOperation.Acrescimo)
            {
                product.SetIncreasePrice(command.Percent);
            }
            else
            {
                product.SetDiscontPrice(command.Percent);
            }

            if (product.Price <= 0)
            {
                AddNotification("Produto.Preço", "O valor da porcentagem gerou um valor de produto menor ou igual a 0");
            }

            // Checar as notificações
            if (Invalid)
            {
                return(new CommandResult(false, "Não foi possível realizar a alteração do preço", this));
            }

            // Salvar as Informações
            _productRepository.Edit(product);

            // Retornar informações
            return(new CommandResult(true, "Alteração realizada com sucesso!", product));
        }
Пример #2
0
 public async Task <IProcessResult> Put(UpdateProductPriceCommand command) => await mediator.Send(command);
Пример #3
0
 public async Task <IActionResult> UpdatePrice([FromBody] UpdateProductPriceCommand command, [FromServices] ProductHandler handler)
 {
     return(ResponseAsync(handler.Handle(command).Result as CommandResult));
 }
Пример #4
0
        static async Task Main(string[] args)
        {
            // create and configure the service container
            IServiceCollection serviceCollection = ConfigureServices();

            // build the service provider
            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            IUnitOfWork uow = serviceProvider.GetService <IUnitOfWork>();

            // get all users
            var users = await uow.GetUserRepository().GetAllAsync();

            PrintUsers(users);

            // get all products
            var prods = await uow.GetProductRepository().GetAllAsync();

            PrintProducts(prods);

            Command cmd;
            Command saveCommand = new SaveCommand(uow);

            // create carts for users[0] & users[1]
            cmd = new CreateCartCommand(users[0]);
            cmd.Execute();

            cmd = new CreateCartCommand(users[1]);
            cmd.Execute();

            var cart1 = users[0].Carts.Last();
            var cart2 = users[1].Carts.Last();

            saveCommand.Execute();

            // some operation on npaul's cart
            cmd = new AddProductToCartCommand(cart1, prods[0], 5);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart1, prods[1], 3);
            cmd.Execute();

            cmd = new UpdateCartItemQuantityCommand(cart1, prods[0], 6);
            cmd.Execute();

            saveCommand.Execute();

            PrintCart(cart1);

            // some operation on jdoe's cart
            cmd = new AddProductToCartCommand(cart2, prods[0], 1);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart2, prods[1], 3);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart2, prods[2], 2);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart2, prods[3], 2);
            cmd.Execute();

            saveCommand.Execute();

            PrintCart(cart2);

            cmd = new UpdateProductPriceCommand(prods[1], 15);
            cmd.Execute();

            saveCommand.Execute();

            PrintCart(cart1);
            PrintCart(cart2);

            // cleaning
            cmd = new DeleteCartCommand(cart1, uow.GetCartRepository());
            cmd.Execute();

            cmd = new DeleteCartCommand(cart2, uow.GetCartRepository());
            cmd.Execute();

            saveCommand.Execute();

            cmd = new UpdateProductPriceCommand(prods[1], 17.50);
            cmd.Execute();

            saveCommand.Execute();
        }