private async Task <bool> HandleAsync(NewProductAdded e) { Console.WriteLine($"Product created: Customer Id = {e.ProductId}, Name = {e.Name}, Price = {e.Price}, Weight = {e.WeightKg}"); using (var db = GetOrderDb()) { try { await db.Products.AddAsync(new Product() { ProductId = e.ProductId, Name = e.Name, Price = e.Price, WeightKg = e.WeightKg }); await db.SaveChangesAsync(); } catch (DbUpdateException) { Console.WriteLine($"Skipped adding product with product id {e.ProductId}."); } } return(true); }
public async Task <IActionResult> PostAsync([FromBody] AddProduct command) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // save new product Product product = Mapper.Map <Product>(command); var newP = await _productRepository.CreateAsync(product); // send event NewProductAdded e = Mapper.Map <NewProductAdded>(newP); await _messagePublisher.PublishMessageAsync(e.MessageType, e, ""); // return result return(CreatedAtRoute("GetById", new { id = newP.Id }, newP)); }
private async Task <bool> HandleAsync(NewProductAdded e) { using (var dbContext = GetDbContext()) { if (e != null) { await dbContext.Products.AddAsync(new Product { ProductId = e.Id, Name = e.Name, Weight = e.WeightKg, }); await dbContext.SaveChangesAsync(); } } return(true); }