示例#1
0
        /// <summary>
        /// UpdateBook()
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="vm"></param>
        /// <returns></returns>
        public async Task <BookViewModel> UpdateBook(int Id, BookViewModel vm)
        {
            BookViewModel book = await _db.Books.Where(a => a.ID == Id).SingleOrDefaultAsync();

            if (book != null)
            {
                string originalEdition = book.Edition;
                book.Title         = vm.Title;
                book.Author        = vm.Author;
                book.Edition       = vm.Edition;
                book.Genre         = vm.Genre;
                book.ISBN          = vm.ISBN;
                book.Location      = vm.Location;
                book.YearPublished = vm.YearPublished;
                book.DateUpdated   = DateTime.Now;
                _db.Update(book);
                await _db.SaveChangesAsync();

                // Detect a changed field.
                if (vm.Edition != originalEdition)
                {
                    BookInventoryChangedIntegrationEvent bookInventoryChangedIntegrationEvent
                        = new BookInventoryChangedIntegrationEvent(Id, book.Edition, originalEdition);
                    await _eventBus.Publish(bookInventoryChangedIntegrationEvent);

                    await Task.Delay(2000);
                }
            }
            return(book);
        }
        public static void Run([ServiceBusTrigger("bookloanasbqueue",
                                                  Connection = "AzureWebJobsServiceBusQueue")] Message myQueueItem,
                               ILogger log)
        {
            // Deserialize the body of the message..
            var body = Encoding.UTF8.GetString(myQueueItem.Body);
            BookInventoryChangedIntegrationEvent bookInventoryChangedIntegrationEvent =
                JsonConvert.DeserializeObject <BookInventoryChangedIntegrationEvent>(body);

            log.LogInformation($"C# ServiceBus queue trigger function processed message: { bookInventoryChangedIntegrationEvent.BookId}");
            log.LogInformation($"C# ServiceBus queue trigger function processed message old edition: {bookInventoryChangedIntegrationEvent.OldEdition}");
            log.LogInformation($"C# ServiceBus queue trigger function processed message new edition: {bookInventoryChangedIntegrationEvent.NewEdition}");
        }