Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active June 22, 2016 22:53
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/ad23a54be2ce455874395f4fb2b4d005 to your computer and use it in GitHub Desktop.
Save dcomartin/ad23a54be2ce455874395f4fb2b4d005 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Threading.Tasks;
using MediatR;
using Nancy.ModelBinding;
namespace Nancy.Linker.Demo.ChangePricingLevel
{
public class Module : NancyModule
{
public Module(IMediator mediator)
{
Put["/demo/customer/{CustomerID:Guid}/changepricinglevel", true] = async (parameters, token) =>
{
var cmd = this.Bind<Command>();
cmd.CustomerId = (Guid)parameters.CustomerID;
await mediator.SendAsync(cmd);
return HttpStatusCode.NoContent;
};
}
}
public class Command : IAsyncRequest
{
public Guid CustomerId { get; set; }
public Customer.PricingLevel PricingLevel { get; ; }
public Command(Guid customerId, Customer.PricingLevel pricingLevel)
{
CustomerId = customerId;
PricingLevel = pricingLevel;
}
}
public class Handler : IAsyncRequestHandler<Command, Unit>
{
private readonly DbFactory _dbFactory;
public Handler(DbFactory dbFactory)
{
_dbFactory = dbFactory;
}
public async Task<Unit> Handle(Command message)
{
using (var db = _dbFactory())
{
var customer = db.Customers.Single(x => x.CustomerId == message.CustomerId);
customer.ChangePricingLevel(message.PricingLevel);
await db.SaveAsync();
}
return Unit.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment