Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created April 14, 2021 21:14
Show Gist options
  • Save dcomartin/c4d52d3f137573b5583fce33758eeb71 to your computer and use it in GitHub Desktop.
Save dcomartin/c4d52d3f137573b5583fce33758eeb71 to your computer and use it in GitHub Desktop.
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