Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active April 18, 2016 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/09b116e78bc94b711362120e39a091b0 to your computer and use it in GitHub Desktop.
Save dcomartin/09b116e78bc94b711362120e39a091b0 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using MediatR;
using Nancy;
using Nancy.ModelBinding;
namespace ThinControllers.Demo.ChangeCustomerPricingLevel
{
public class Module : NancyModule
{
public Module(IMediator mediator)
{
Put["/demo/changepricinglevel/{MessageId:Guid}", true] = async (parameters, token) =>
{
var cmd = this.Bind<Command>();
var envelop = new Envelope<Command>((Guid)parameters.MessageId, cmd);
await mediator.SendAsync(envelop);
return HttpStatusCode.NoContent;
};
}
}
public class Command : IAsyncRequest
{
public Guid CustomerId { get; }
public PricingLevel PricingLevel { get; }
public Command(Guid customerId, PricingLevel pricingLevel)
{
CustomerId = customerId;
PricingLevel = pricingLevel;
}
}
public class Handler : IAsyncRequestHandler<Envelope<Command>, Unit>
{
private readonly CustomeRepository _repository;
public Handler(CustomeRepository repository)
{
_repository = repository;
}
public async Task<Unit> Handle(Envelope<Command> message)
{
var customer = _repository.Get(message.Body.CustomerId);
try
{
customer.SetOperation(message.MessageId);
}
catch (InvalidOperationException)
{
return Unit.Value;
}
customer.ChangePricingLevel(message.Body.PricingLevel);
await _repository.SaveAsync(customer);
return Unit.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment