Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created Dec 15, 2021
Embed
What would you like to do?
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