public async Task <IActionResult> Post(ProductViewModel model, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.Values));
            }

            var product = Mapper.Map <Product>(model);

            var category = await _context.Categories.FirstOrDefaultAsync(p => p.Id == model.CategoryId, cancellationToken);

            product.Category = category;

            await _context.Products.AddAsync(product, cancellationToken);

            var @event = new ProductCreatedIntegrationEvent(
                product.Id,
                product.Name,
                product.Category.Id,
                product.Category.Name,
                product.Price,
                product.Featured);

            await _integrationEvent.SaveEventAndProductContextChangesAsync(@event, cancellationToken);

            await _integrationEvent.PublishThroughEventBusAsync(@event, cancellationToken);

            return(CreatedAtRoute(new { product.Id }, product));
        }
Exemple #2
0
        public async Task <ActionResult> CreateProduct([FromBody] Product product)
        {
            _applicationContext.Products.Add(product);
            var productCreatedEvent = new ProductCreatedIntegrationEvent(product);

            _integrationEventLogService.Save(productCreatedEvent);
            await _integrationContextEventService.SaveApplicationContextAndEventLogsContextChangesAsync();

            await _integrationContextEventService.PublishEvent(productCreatedEvent);

            return(CreatedAtAction(nameof(GetById), new { id = product.Id }, null));
        }
        public async Task PublishThroughEventBusAsync(ProductCreatedIntegrationEvent @event, CancellationToken cancellationToken = default(CancellationToken))
        {
            await _eventBus.Publish(@event, cancellationToken);

            await _eventLogService.MarkEventAsPublishedAsync(@event, cancellationToken);
        }
 public async Task PublishThroughEventBusAsync(ProductCreatedIntegrationEvent @event, CancellationToken cancellationToken = default(CancellationToken))
 {
     await _salesContext.SaveChangesAsync(cancellationToken);
 }