public async Task <bool> Handle(SaveBookCommand request, CancellationToken cancellationToken) { var inserted = false; var book = await _db.Books.FilterTitle(request.Title).FirstOrDefaultAsync(cancellationToken); if (book == null) { inserted = true; book = new Entities.Book(); } book.Title = request.Title; book.Language = request.Language; book.AuthorName = request.Author; if (request.Category != null) { var category = await _db.Categories.FirstOrDefaultAsync(x => x.Name == request.Category, cancellationToken); if (category == null) { category = new Entities.BookCategory { Name = request.Category }; } if (!book.Categories.Any(x => x.Category.Name == category.Name)) { book.Categories.Add(new Entities.BookCategoryBook() { Category = category }); } } if (inserted) { await _db.AddAsync(book, cancellationToken); } await _db.SaveChangesAsync(cancellationToken); var message = new UpdateBookEvent { Title = book.Title, Author = book.AuthorName, Language = book.Language }; await _bus.PublishAsync(ContextNames.Exchange.Book, message); return(true); }
public void UpdateBook(Book book) { foreach (Book b in books) { if (ReferenceEquals(b, book)) { b.Title = book.Title; b.Author = book.Author; b.ReleaseDate = book.ReleaseDate; b.Genre = book.Genre; } } foreach (Book b in books) { Console.WriteLine(b.Title + b.Author + b.Genre); } UpdateBookEvent?.Invoke(book); }
public void UpdateBook(Book book) { UpdateBookEvent?.Invoke(book); }