Beispiel #1
0
        public async Task CreateAsync(PurchaseMessage message)
        {
            var numbersDropped = new List <DropCopyNumberMessage>();

            var order = await _db.Orders
                        .Include(x => x.Items)
                        .ThenInclude(x => x.Book)
                        .Include(x => x.Customer)
                        .FirstOrDefaultAsync(x => x.Number == message.Number);

            if (order == null)
            {
                order = new Order
                {
                    Number     = message.Number,
                    Status     = StatusOrder.Opened,
                    CreateDate = DateTime.UtcNow,
                    Customer   = await _db.Customers.FirstOrDefaultAsync(x => x.DocumentId == message.CustomerId)
                };

                if (order.Customer == null)
                {
                    order.Customer = new Customer
                    {
                        DocumentId = message.CustomerId,
                        Name       = message.CustomerName
                    };
                }

                order.Items = new List <OrderItem>();

                foreach (var item in message.Items)
                {
                    var orderItem = await GetItemAsync(_db, item);

                    if (IntegrationWithLibrary.Get())
                    {
                        numbersDropped.Add(new DropCopyNumberMessage()
                        {
                            Number = item.Number
                        });
                    }

                    order.Items.Add(orderItem);
                }

                await _db.AddAsync(order);
            }
            else
            {
                order.Items = new List <OrderItem>();

                foreach (var item in message.Items)
                {
                    var orderItem = await GetItemAsync(_db, item);

                    if (IntegrationWithLibrary.Get())
                    {
                        numbersDropped.Add(new DropCopyNumberMessage()
                        {
                            Number = item.Number
                        });
                    }

                    order.Items.Add(orderItem);
                }

                _db.Update(order);
            }

            await _db.SaveChangesAsync();

            if (IntegrationWithLibrary.Get())
            {
                await _bus.PublishAllAsync(QueueNames.Library, numbersDropped);
            }

            var dto = new ShippingDtoMessage
            {
                OrderId = order.Id
            };

            await _bus.PublishAsync(QueueNames.Bookstore, dto);
        }
Beispiel #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);
        }