-
-
Save dcomartin/3dc8d4d208ae40372e6b8e00c9f50a90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading.Tasks; | |
| using DotNetCore.CAP; | |
| using Sales.Contracts; | |
| namespace Shipping | |
| { | |
| public class CreateShippingLabel : ICapSubscribe | |
| { | |
| private readonly ShippingDbContext _dbContext; | |
| public CreateShippingLabel(ShippingDbContext dbContext) | |
| { | |
| _dbContext = dbContext; | |
| } | |
| [CapSubscribe(nameof(OrderPlaced))] | |
| public async Task Handle(OrderPlaced orderPlaced, [FromCap]CapHeader header) | |
| { | |
| var messageId = header.GetMessageId(); | |
| if (await _dbContext.HasBeenProcessed(messageId, nameof(CreateShippingLabel))) | |
| { | |
| return; | |
| } | |
| using (var trx = await _dbContext.Database.BeginTransactionAsync()) | |
| { | |
| await _dbContext.ShippingLabels.AddAsync(new ShippingLabel | |
| { | |
| OrderId = orderPlaced.OrderId, | |
| OrderDate = DateTime.UtcNow | |
| }); | |
| await _dbContext.SaveChangesAsync(); | |
| await _dbContext.IdempotentConsumer(messageId, nameof(CreateShippingLabel)); | |
| await trx.CommitAsync(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment