public async Task<IActionResult> Create(ProductViewModel product) { var command = new ProductCreateCommand(product); var result = await Bus.SubmitAsync(command); return Result(HttpStatusCode.Created, result); }
public async Task <IActionResult> Put(string name) { var command = new ProductCreateCommand(name); var result = await mediator.Send(command); return(Ok(result)); }
private static async Task RunAsync() { var messageDispatcher = ServiceProvider.GetRequiredService <IMessageDispatcher>(); var dataStore = ServiceProvider.GetRequiredService <IDataStore>(); var command = new ProductCreateCommand(Guid.NewGuid(), "myProduct"); var commandResult = await messageDispatcher.DispatchAsync(command); await Console.Out.WriteLineAsync(commandResult.ToString()); var product = await LoadProductUncachedAsync(command.Id); await Console.Out.WriteLineAsync(product.ProductName); var listModel = await LoadProductListModelUncachedAsync(command.Id); await Console.Out.WriteLineAsync(listModel.ProductName); var deleteModel = await dataStore.FindOneAsync <ProductDeleteModel>(p => p.Id == command.Id); var deleteCommand = new ProductDeleteCommand(deleteModel.Id, deleteModel.ConcurrencyToken); var deleteCommandResult = await messageDispatcher.DispatchAsync(deleteCommand); product = await LoadProductUncachedAsync(command.Id); await Console.Out.WriteLineAsync($"Product is{(product == null ? "" : " not")} deleted."); listModel = await LoadProductListModelUncachedAsync(command.Id); await Console.Out.WriteLineAsync($"Product is{(listModel == null ? "" : " not")} deleted."); await Console.In.ReadLineAsync(); }
public async Task ChangePrice() { var(_, repository, mediator) = TestSetup.Init(); var handler = new ProductCommandHandlers(repository, mediator); var id = Guid.NewGuid(); var productCreateCommand = new ProductCreateCommand { Id = id, Name = "mock name", Price = 500 }; await handler.Handle(productCreateCommand, CancellationToken.None); const int newPrice = 600; var priceChangeCommand = new ProductChangePriceCommand { ProductId = id, Price = newPrice }; await handler.Handle(priceChangeCommand, CancellationToken.None); var agg = await repository.GetHydratedAggregate <Kanayri.Domain.Product.Product>(id, CancellationToken.None); Assert.Equal(newPrice, agg.Price); }
public IActionResult Post([FromBody] ProductCreateCommand command) { if (!ModelState.IsValid) { return(new BadRequestObjectResult( new ResultViewModel { Success = false, Message = "Erro ao cadastrar o produto", Data = ModelState.GetErrors() })); } var category = _categoryRepository.GetById(command.CategoryId); if (category == null) { return(new NotFoundObjectResult($"Categoria com o id {command.CategoryId} não encontrado!")); } var product = _mapper.Map <Product>(command); _productRepository.Save(product); return(new OkObjectResult( new ResultViewModel { Success = true, Message = "Produto cadastrado com sucesso", Data = _mapper.Map <ProductCreateResult>(product) })); }
public IActionResult Put([FromBody] ProductCreateCommand command) { if (!ModelState.IsValid) { return(new BadRequestObjectResult( new ResultViewModel { Success = false, Message = "Erro ao cadastrar o produto", Data = ModelState.GetErrors() })); } var product = _productRepository.GetById(command.Id); if (product == null) { return(new NotFoundObjectResult($"Produto com o Id {command.Id} não encontrada!")); } product = _mapper.Map <Product>(command); //Salvar _productRepository.Update(product); return(new OkObjectResult( new ResultViewModel { Success = true, Message = "Produto atualizado com sucesso", Data = _mapper.Map <ProductCreateResult>(product) })); }
public Task CreateProduct(Guid categoryId, [FromBody] ProductCreateCommand productCreateCommand, CancellationToken cancellationToken) { if (productCreateCommand.CategoryId != categoryId) { throw new Exception(); } return(mediator.Send(productCreateCommand, cancellationToken)); }
public async Task <IActionResult> Post(ProductCreateCommand command) { if (!ModelState.IsValid) { return(BadRequest()); } return(Ok(await mediator.Send(command))); }
public void SetUp() { _dtoValidatorFactorykMock.ResetCalls(); _internalCommandService.ResetCalls(); _productQuery.ResetCalls(); _productValidator.ResetCalls(); _storeValidator.ResetCalls(); _createCommand = new ProductCreateCommand(_dtoValidatorFactorykMock.Object, _internalCommandService.Object, _productQuery.Object, _productValidator.Object, _storeValidator.Object); }
public void AddProductHandler_valid() { var repository = new FakeProductRepository(); var handler = new ProductHandler(repository); var command = new ProductCreateCommand(); command.Name = "Product D"; command.Price = 5.5m; var result = handler.Create(command); Assert.True(result.Success, result.Message); }
public AbstractApiResult Create(Product product) { var command = new ProductCreateCommand(product); var result = Bus.Submit(command); if (NotificationHandler.HasNotifications()) { return(ValidationErrorResult()); } return(result.Success ? (AbstractApiResult) new SuccessApiResult(HttpStatusCode.Created, result.Data) : (AbstractApiResult) new FailureApiResult(HttpStatusCode.BadRequest, result.Message)); }
public async Task <ActionResult <Product> > PostProduct([FromBody] ProductCreateDto product) { var newProductId = Guid.NewGuid(); var command = new ProductCreateCommand { Id = newProductId, Name = product.Name, Price = product.Price }; await _mediator.Publish(command); // Publish not Send as We can't read Data with Command only with Query return(await GetProduct(newProductId.ToString())); }
public void AddProductHandler_Lenght_Name_4_Invalid() { var repository = new FakeProductRepository(); var handler = new ProductHandler(repository); var command = new ProductCreateCommand(); command.Name = "Pro"; command.Price = 5.5m; var result = handler.Handle(command); Assert.False(result.Success, result.Message); }
public void AddProductHandler_Negative_Price_Invalid() { var repository = new FakeProductRepository(); var handler = new ProductHandler(repository); var command = new ProductCreateCommand(); command.Name = "Product D"; command.Price = -1; var result = handler.Handle(command); Assert.False(result.Success, result.Message); }
public ProductTest ( TestWebApplicationFactory factory, ITestOutputHelper output ) : base(factory, output) { var category = CategoryGenerator.GenerateAndSave(CategoryRepository); Command = new ProductCreateCommand { Title = "Product Title", Description = "Product description", Price = 150, Quantity = 10, CategoryId = category.Id }; }
public async Task QueryChangePrice() { var(_, repository, mediator) = TestSetup.Init(); var handler = new ProductCommandHandlers(repository, mediator); var id = Guid.NewGuid(); const int price1 = 500; const int price2 = 600; const int price3 = 700; var productCreateCommand = new ProductCreateCommand { Id = id, Name = "mock name", Price = price1 }; await handler.Handle(productCreateCommand, CancellationToken.None); var priceChange1 = new ProductChangePriceCommand { ProductId = id, Price = price2 }; await handler.Handle(priceChange1, CancellationToken.None); var priceChange2 = new ProductChangePriceCommand { ProductId = id, Price = price3 }; await handler.Handle(priceChange2, CancellationToken.None); var events = await repository.GetEventsOfType <ProductPriceChangedEvent>(id, CancellationToken.None); var changedEvents = events as ProductPriceChangedEvent[] ?? events.ToArray(); Assert.Equal(2, changedEvents.Count()); Assert.Equal(price2, changedEvents.ElementAt(0).Price); Assert.Equal(price3, changedEvents.ElementAt(1).Price); }
public CommandResult Create(ProductCreateCommand command) { var exist = _repository.Exists(command.Name); if (exist) { return(new CommandResult(false, "Já existe um Produto cadastrado com esse Nome. ", command)); } var product = new Product(command.Name, command.Price); AddNotifications(product); if (Invalid) { return(new CommandResult(false, GroupNotifications.Group(Notifications), command)); } _repository.Create(product); return(new CommandResult(true, "Produto cadastrado com sucesso! ", product)); }
public async Task CreateProduct() { var(context, repository, mediator) = TestSetup.Init(); var handler = new ProductCommandHandlers(repository, mediator); var command = new ProductCreateCommand { Id = Guid.NewGuid(), Name = "New Product", Price = 500 }; await handler.Handle(command, new CancellationToken(false)); var aggregates = await context.Aggregate.ToListAsync(); var events = await context.Events.ToListAsync(); Assert.Single(aggregates); Assert.Single(events); }
public CommandResult Create(ProductCreateCommand productCreate, List <WarehouseCreateCommand> warehouseCommandList) { if (productCreate.IsValid() == false) { return(CommandResult.Error(productCreate.ValidationResult.ToString())); } if (_productRepository.Exists(productCreate.Sku) == true) { return(CommandResult.Error("Sku do produto já existe.")); } foreach (var item in warehouseCommandList) { if (item.IsValid() == false) { return(CommandResult.Error(item.ValidationResult.ToString())); } } var product = new Product(sku: productCreate.Sku, name: productCreate.Name); var warehouseList = warehouseCommandList .Select(x => new Warehouse( sku: product.Sku, locality: x.Locality, quantity: x.Quantity, type: x.Type )); _productRepository.Create(product); foreach (var warehouse in warehouseList) { _warehouseRepository.Create(warehouse); } return(CommandResult.Ok()); }
public async Task Create(ProductCreateCommand command) => await mediator.Publish(command);
public async Task <IActionResult> Create(ProductCreateCommand command) { await mediator.Publish(command); return(Ok()); }
public async Task <IActionResult> Post([FromBody] ProductCreateCommand command) { var id = await _mediator.Send(command); return(await Get(id)); }
public void Handle(ProductCreateCommand command) { Entity = new Product(command.Id, new ProductName(command.ProductName)); }
public async Task <ActionResult <ProductDto> > Create(ProductCreateCommand command) { await mediator.Publish(command); return(Ok()); //TODO: Corregir }
public CommandResult Create(ProductCreateCommand command) { var result = _handler.Handle(command); return(result); }
public async Task <IActionResult> Create(ProductCreateCommand command, CancellationToken cancellationToken) { await _mediator.Publish(command, cancellationToken); return(Ok()); }
public async Task <IActionResult> Create(ProductCreateCommand notification) { await _mediator.Publish(notification); return(Ok()); }
public async Task <IActionResult> Create(ProductCreateCommand productCreateCommand) { return(StatusCode(201, await _mediator.Send(productCreateCommand))); }