Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/Static.cs Secret

Created Apr 14, 2021
Embed
What would you like to do?
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