コード例 #1
0
        public async Task <IActionResult> Post([FromBody] CreateBook command)
        {
            var newBook = new Book
            {
                Id              = Guid.NewGuid(),
                Title           = command.Title,
                Author          = command.Author,
                Cost            = command.Cost,
                InventoryAmount = command.InventoryAmount,
            };
            await _context.Books.AddAsync(newBook);

            await _context.SaveChangesAsync();

            var @event = new BookCreated
            {
                Id              = newBook.Id,
                Title           = newBook.Title,
                Author          = newBook.Author,
                Cost            = newBook.Cost,
                InventoryAmount = newBook.InventoryAmount,
                UserId          = command.UserId,
                Timestamp       = DateTime.UtcNow
            };
            await _messageProducer.PublishAsync(@event);

            return(StatusCode((int)HttpStatusCode.Created, new { newBook.Id }));
        }