-
-
Save dcomartin/5427ab2b1ce590db64b44a2159c0b145 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 EventSourcing.Demo; | |
using EventStore.ClientAPI.Exceptions; | |
using Microsoft.AspNetCore.Mvc; | |
var builder = WebApplication.CreateBuilder(args); | |
var app = builder.Build(); | |
app.MapGet("/products/{sku}", async (HttpResponse response, string sku) => | |
{ | |
using var stream = await WarehouseProductEventStoreStream.Factory(); | |
var product = await stream.Get(sku); | |
return new | |
{ | |
Sku = product.Aggregate.Sku, | |
Quantity = product.Aggregate.GetQuantityOnHand(), | |
Version = product.Version, | |
Commands = new Command[] | |
{ | |
new("InventoryAdjustment", $"/products/{sku}/{product.Version}/adjustment") | |
} | |
}; | |
}); | |
app.MapPost("/products/{sku}/{version}/adjustment", | |
async (HttpResponse response, [FromRoute]string sku, [FromRoute]long version, [FromBody]InventoryAdjustment inventoryAdjustment) => | |
{ | |
using var stream = await WarehouseProductEventStoreStream.Factory(); | |
var product = await stream.Get(sku); | |
product.Aggregate.AdjustInventory(inventoryAdjustment.Quantity, inventoryAdjustment.Reason); | |
try | |
{ | |
await stream.Save(product.Aggregate, version); | |
} | |
catch (WrongExpectedVersionException) | |
{ | |
response.StatusCode = 412; | |
} | |
}); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment