Example #1
0
        private static void RunCQRSMediatR()
        {
            try
            {
                var mediator = BuildMediator();

                //Add new Product
                var product = new CommandsMediatR.AddNewProductCommand {
                    Id = Guid.NewGuid(), Name = "iPhone 11", Description = "Apple iphone 11"
                };
                var res = mediator.Send(product);

                //Update Product Unit Price
                mediator.Send(new CommandsMediatR.UpdateProductUnitPriceCommand {
                    Id = product.Id, UnitPrice = 200
                });

                //Update Product Current Stock
                mediator.Send(new CommandsMediatR.UpdateProductCurrentStockCommand {
                    Id = product.Id, CurrentStock = 600
                });


                //Fine Products By Name
                var productsByName = mediator.Send(new QueriesMediatR.GetProductsByNameQuery {
                    Name = "iPhone"
                });
                foreach (var item in productsByName.Result)
                {
                    Console.WriteLine(item.ToString());
                }

                //Fine Products By Name
                var outOfStockProducts = mediator.Send(new QueriesMediatR.FindOutOfStockProductsQuery());
                foreach (var item in outOfStockProducts.Result)
                {
                    Console.WriteLine(item.ToString());
                }

                //Delete Product
                mediator.Send(new CommandsMediatR.DeleteProductCommand {
                    Id = product.Id
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public async Task <IActionResult> PostAsync(CommandsMediatR.AddNewProductCommand command)
        {
            var response = await _mediator.Send(command);

            return(NoContent());
        }