public async Task <IActionResult> Post([FromBody] CreateBook command) { var newBook = new Book { Id = Guid.NewGuid(), Title = command.Title, Author = command.Author, Cost = command.Cost, InventoryAmount = command.InventoryAmount, }; await _context.Books.AddAsync(newBook); await _context.SaveChangesAsync(); var @event = new BookCreated { Id = newBook.Id, Title = newBook.Title, Author = newBook.Author, Cost = newBook.Cost, InventoryAmount = newBook.InventoryAmount, UserId = command.UserId, Timestamp = DateTime.UtcNow }; await _messageProducer.PublishAsync(@event); return(StatusCode((int)HttpStatusCode.Created, new { newBook.Id })); }
internal void Apply(BookCreated bookCreated) { Id = bookCreated.Id; Title = bookCreated.Title; Author = bookCreated.Author; Tags = bookCreated.Tags; Cost = bookCreated.Cost; InventoryAmount = bookCreated.InventoryAmount; }
private void Handle(BookCreated evnt) { _connection.Insert( new { Id = evnt.Id, Name = evnt.BookInfo.Name, Description = evnt.BookInfo.Description, Author = evnt.BookInfo.Author, ISBN = evnt.BookInfo.ISBN, Publisher = evnt.BookInfo.Publisher }, "EventSourcing_Sample_Book", _transaction); }
protected virtual void Handle(BookCreated evnt) { var book = new BookEntity { Id = evnt.Id, Name = evnt.BookInfo.Name, Description = evnt.BookInfo.Description, Author = evnt.BookInfo.Author, ISBN = evnt.BookInfo.ISBN, Publisher = evnt.BookInfo.Publisher }; _entityManager.Create(book); }
protected virtual void OnBookCreated(EventArgs e) { BookCreated?.Invoke(this, e); }
public BookActor() { PersistenceId = Context.Self.Path.Name; Command <CreateBook>(createBook => { var bookCreated = new BookCreated ( id: createBook.Id, title: createBook.Title, author: createBook.Author, tags: createBook.Tags, cost: createBook.Cost, inventoryAmount: createBook.InventoryAmount ); Persist(bookCreated, _ => { _logger.Info($"Book created: {bookCreated}"); _aggregate.Apply(bookCreated); }); }); Command <AddTag>(addTag => { var tagAdded = new TagAdded(id: addTag.Id, tag: addTag.Tag); Persist(tagAdded, _ => { _logger.Info(tagAdded.ToString()); _aggregate.Apply(tagAdded); }); }); Command <RemoveTag>(removeTag => { var tagRemoved = new TagRemoved(id: removeTag.Id, tag: removeTag.Tag); Persist(tagRemoved, _ => { _logger.Info(tagRemoved.ToString()); _aggregate.Apply(tagRemoved); }); }); Command <GetBook>(getBook => { var bookDto = new BookDto ( id: _aggregate.Id, title: _aggregate.Title, author: _aggregate.Author, tags: _aggregate.Tags ?? new string[] { }, cost: _aggregate.Cost ); Sender.Tell(bookDto); }); Recover <BookCreated>(bookCreated => _aggregate.Apply(bookCreated)); Recover <TagAdded>(tagAdded => _aggregate.Apply(tagAdded)); Recover <TagRemoved>(tagRemoved => _aggregate.Apply(tagRemoved)); }