Created
June 23, 2016 01:24
-
-
Save dcomartin/a7eee5a1e221bbe839cdabf346eedced 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.Linq; | |
using System.Threading.Tasks; | |
using MediatR; | |
using Nancy.ModelBinding; | |
namespace Nancy.Linker.Demo.Features.ChangePricingLevel | |
{ | |
public class Module : NancyModule | |
{ | |
public static readonly Route Route = new Route("ChangePricingLevel", | |
"/demo/customers/{CustomerId:int}/changepricinglevel"); | |
public Module(IMediator mediator) | |
{ | |
Put[Route.Name, Route.Path, true] = | |
async (parameters, token) => | |
{ | |
var cmd = this.Bind<Command>(); | |
cmd.CustomerId = (int) parameters.CustomerId; | |
await mediator.SendAsync(cmd); | |
return HttpStatusCode.NoContent; | |
}; | |
} | |
} | |
public static class RouteLinkerExtension | |
{ | |
public static string ChangePricingLevel(this RouteLinker routeLinker, int customerId) | |
{ | |
return routeLinker.BuildAbsoluteRoute(Module.Route, new {CustomerId = customerId}); | |
} | |
} | |
public class Command : IAsyncRequest | |
{ | |
public int CustomerId { get; set; } | |
public Customer.PricingLevel PricingLevel { get; } | |
public Command(int 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