Last active
June 22, 2016 22:53
-
-
Save dcomartin/ad23a54be2ce455874395f4fb2b4d005 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.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