public async Task Consume(ConsumeContext <IDeleteProductCommand> context) { try { var connectionStringSettings = new ConnectionStringSettings(); var blobStorageSettings = new BlobStorageSettings(); _configurationRoot.GetSection("ConnectionStrings").Bind(connectionStringSettings); _configurationRoot.GetSection("BlobStorage").Bind(blobStorageSettings); var dbContext = ProductDbContext.GetProductDbContext(connectionStringSettings.DefaultConnection); var repository = new RepositoryWrapper(dbContext); var product = repository.Product.GetProductById(Guid.Parse(context.Message.ProductId)); if (product != null) { var deleteblob = new PhotoBlob(blobStorageSettings); await deleteblob.DeleteBlob(product.BlobName); } var productService = new ProductCatalogService(repository); var result = await productService.DeleteProduct(context.Message.ProductId); var deletedEvent = new ProductDeletedEvent(result); await context.RespondAsync(deletedEvent); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// DELETE /api/products/{id} /// </summary> /// <param name="id"></param> /// <param name="data"></param> /// <returns></returns> public HttpResponseMessage Delete(int id, ProductModel model) { var context = this.DbContext; var entity = context.Products.Find(id); if (entity == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } if (!this.User.CanDelete(entity)) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)); } // create the web event var webEvent = new ProductDeletedEvent(entity); // delete the entity context.Products.Remove(entity); // persist changes to the database context.SaveChanges(); // fire the web event webEvent.Raise(); return(new HttpResponseMessage(HttpStatusCode.NoContent)); }
public void ProductDeletedEvent_Ctor_Should_Set_Arguments_Correctly() { Guid productId = Guid.NewGuid(); var @event = new ProductDeletedEvent(productId); Assert.Equal(productId, @event.ProductId); Assert.Equal(productId, @event.AggregateId); Assert.Equal(typeof(Catalog.Models.Product), @event.AggregateType); }
public void Handle(ProductDeletedEvent @event) { try { EventStore.Save(@event); } catch { throw; } }
public async Task Handle(DeleteProductCommand command, CancellationToken cancellationToken) { var DeletedEvent = new ProductDeletedEvent { ProductId = command.ProductId, CreatedAt = DateTime.Now }; // Insert event to Command Db await _eventSources.InserEvent(DeletedEvent, cancellationToken); await _kafkaProducer.Send(DeletedEvent, "PosServices"); }
/// <summary> /// Receive messages continuously from a Topic /// </summary> /// <param name="message"></param> /// <param name="token"></param> /// <returns></returns> private async Task ProcessMessagesAsync(Message message, CancellationToken token) { // Log the incoming msg _logger.LogInformation($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}"); //Deserialize incoming msg ProductDeletedEvent @event = JsonConvert.DeserializeObject <ProductDeletedEvent> (Encoding.UTF8.GetString(message.Body)); //Publish message to call the event handler await _mediator.Publish(@event, token).ConfigureAwait(false); // Complete the message so that it is not received again. await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false); }
/// <summary> /// Handles the DeleteProduct Command and raises corresponding events. /// </summary> /// <param name="request">Command object</param> /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> /// <returns>void</returns> public async Task <Unit> Handle(DeleteProductCommand request, CancellationToken cancellationToken = default(CancellationToken)) { //Delete Product await _repository.Delete(request.ProductId); //Log information _logger.LogInformation($"Deleted product with productId : {request.ProductId} from store."); //Create ProductDeletedEvent from request ProductDeletedEvent @event = new ProductDeletedEvent(request.ProductId); //Publish Event await _mediator.Publish(@event, cancellationToken); //Return void return(new Unit()); }
public async Task DeleteProduct(Guid productId) { try { var product = await Repository.GetByKeyAsync <Product>(productId); product.Delete(); await Repository.SaveChangesAsync(); var @event = new ProductDeletedEvent(productId); EventBus.RaiseEvent(@event); } catch { throw; } }
/// <summary> /// Implementation of <see cref="IProductCommands.DeleteProduct(Guid)"/> /// </summary> /// <param name="productId">The product id</param> /// <returns></returns> public virtual async Task DeleteProduct(Guid productId) { try { if (productId == Guid.Empty) { throw new ArgumentException("value cannot be empty", nameof(productId)); } var product = await Repository.GetByKeyAsync <Product>(productId); product.Delete(); await Repository.SaveChangesAsync(); var @event = new ProductDeletedEvent(productId); EventBus.RaiseEvent(@event); } catch { throw; } }
public Task Handle(ProductDeletedEvent message, CancellationToken cancellationToken) { // Send some see you soon e-mail return(Task.CompletedTask); }
private ConsumeContext <ProductDeletedEvent> GetContext(string reference = reference) { var productDeletedEvent = new ProductDeletedEvent(reference); return(Mock.Of <ConsumeContext <ProductDeletedEvent> >(x => x.Message == productDeletedEvent)); }
private void When(ProductDeletedEvent @event) { IsDeleted = true; }