Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created December 2, 2020 22:47
Show Gist options
  • Select an option

  • Save dcomartin/3dc8d4d208ae40372e6b8e00c9f50a90 to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/3dc8d4d208ae40372e6b8e00c9f50a90 to your computer and use it in GitHub Desktop.
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