public async Task <Book> Handle(CreateBookCommand request, CancellationToken cancellationToken) { using (var uow = _unitOfWorkFactory.Create()) { var bookModel = _entityFactory.CreateBook(request.Name, request.Date, request.Summary, request.Picture); var result = await uow.Books.CreateAsync(bookModel, cancellationToken); return(new Book(result)); } }
public async Task <Book> Handle(UpdateBookCommand request, CancellationToken cancellationToken) { using (var uow = _unitOfWorkFactory.Create()) { var bookModel = _entityFactory.CreateBook(request.Id, request.Name, request.Date, request.Summary, request.Picture); foreach (var author in request.Authors) { if (author.Id.HasValue && author.Id != Guid.Empty) { var authorModel = _entityFactory.CreateAuthor( author.Id.Value, author.FirstName, author.LastName, new LifePeriod(author.DateOfBirth, author.DateOfDeath)); bookModel.AddAuthor(authorModel); } else { var saved = await uow.Authors.FindAsync( author.FirstName, author.LastName, author.DateOfBirth, author.DateOfDeath, cancellationToken); if (saved == null) { var authorModel = _entityFactory.CreateAuthor( author.FirstName, author.LastName, new LifePeriod(author.DateOfBirth, author.DateOfDeath)); bookModel.AddAuthor(authorModel); } else { bookModel.AddAuthor(saved); } } } var result = await uow.Books.UpdateAsync(bookModel, cancellationToken); return(new Book(result)); } }
public static Book ToBook(this BookEntity entity, IEntityFactory entityFactory, bool recursive = true) { var rates = entity.Rates != null?entity.Rates.Select(x => x.ToRate(entityFactory)) : new List <BookRate>(); var book = entityFactory.CreateBook(entity.Id, entity.Name, entity.Date, entity.Summary, entity.Picture, rates); if (recursive && entity.Authors != null) { foreach (var author in entity.Authors) { book.AddAuthor(author.Author.ToAuthor(entityFactory, false)); } } return(book); }
public async Task SeedLibraryAsync(CancellationToken token) { if (!_ctx.Authors.Any() || !_ctx.Books.Any()) { using (var unitOfWork = _unitOfWorkFactory.Create()) { var ivanko = _entityFactory.CreateAuthor( "Ivan", "Ivchenko", new LifePeriod(new DateTime(1988, 1, 11))); var slavko = _entityFactory.CreateAuthor( "Myroslava", "Tarcha", new LifePeriod(new DateTime(1993, 5, 5))); var astrid = _entityFactory.CreateAuthor( "Astrid", "Lindgren", new LifePeriod(new DateTime(1907, 11, 14), new DateTime(2002, 1, 14))); for (var i = 1; i < 10; i++) { var book = _entityFactory.CreateBook( "Karlsson-on-the-Roof " + i, new DateTime(2016, 09, 08), "Karlsson-on-the-Roof is a character who figures in a series of children's books by the Swedish author Astrid Lindgren"); book.AddAuthor(astrid); } for (var i = 1; i < 10; i++) { var book = _entityFactory.CreateBook( "Adventures of Vivchyk and Tarchavka" + i, new DateTime(2016, 09, 08), "Adventures of Vivchyk and Tarchavka " + i); book.AddAuthor(ivanko); book.AddAuthor(slavko); } for (var i = 1; i < 10; i++) { var book = _entityFactory.CreateBook( "Adventures of Vivchyk" + i, new DateTime(2016, 09, 08), "Adventures of Vivchyk before he has met Tarchavka " + i); book.AddAuthor(ivanko); } for (var i = 1; i < 10; i++) { var book = _entityFactory.CreateBook( "Adventures of Tarchavka" + i, new DateTime(2016, 09, 08), "Adventures of Tarchavka before she has met Vivchyk " + i); book.AddAuthor(slavko); } await unitOfWork.Authors.CreateAsync(ivanko, token); await unitOfWork.Authors.CreateAsync(slavko, token); await unitOfWork.Authors.CreateAsync(astrid, token); } } }