-
-
Save dcomartin/c4d52d3f137573b5583fce33758eeb71 to your computer and use it in GitHub Desktop.
This file contains 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 Microsoft.EntityFrameworkCore; | |
using NServiceBus; | |
using Sales.Contracts; | |
namespace Sales.Features | |
{ | |
public static class CancelOrder | |
{ | |
public class Command : ICommand | |
{ | |
public Guid OrderId { get; set; } | |
} | |
public class Handler : IHandleMessages<Command> | |
{ | |
private readonly SalesDbContext _dbContext; | |
public Handler(SalesDbContext dbContext) | |
{ | |
_dbContext = dbContext; | |
} | |
public async Task Handle(Command message, IMessageHandlerContext context) | |
{ | |
var order = await _dbContext.Orders.SingleAsync(x => x.OrderId == message.OrderId); | |
order.Status = OrderStatus.Cancelled; | |
await _dbContext.SaveChangesAsync(); | |
await context.Publish<OrderCancelled>(cancelled => | |
{ | |
cancelled.OrderId = message.OrderId; | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment