示例#1
0
        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);
        }
示例#2
0
        public async Task SaveAsync(BookMessage model)
        {
            bool inserted = false;
            var  book     = await _db.Books.FilterTitle(model.Title).FirstOrDefaultAsync();

            if (book == null)
            {
                inserted = true;
                book     = new Entities.Book();
            }

            book.Title    = model.Title;
            book.Language = model.Language;

            if (model.Category != null)
            {
                var category = await _db.Categories.FirstOrDefaultAsync(x => x.Name == model.Category);

                if (category == null)
                {
                    category      = new Entities.BookCategory();
                    category.Name = model.Category;
                }

                if (!book.Categories.Any(x => x.Category.Name == category.Name))
                {
                    book.Categories.Add(new Entities.BookCategoryBook()
                    {
                        Category = category
                    });
                }
            }

            var authorName = model.Author;

            if (FeatureAddTableAtuthor.Get())
            {
                book.Author = await _db.Authors.FirstOrDefaultAsync(x => x.Name == authorName);

                if (book.Author == null)
                {
                    book.Author      = new Entities.BookAuthor();
                    book.Author.Name = authorName;
                }
            }
            else
            {
                book.AuthorName = authorName;
            }

            if (inserted)
            {
                await _db.AddAsync(book);
            }
            else
            {
                _db.Update(book);
            }

            await _db.SaveChangesAsync();

            var message = new BookUpdateMessage
            {
                Id       = book.Id,
                Title    = book.Title,
                Author   = authorName,
                Language = book.Language
            };

            await _bus.PublishAsync(ExchangeNames.Book, message);
        }