-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
84 lines (63 loc) · 2.21 KB
/
Copy pathProgram.cs
File metadata and controls
84 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using agg_store;
using agg_store.Commands;
using agg_store.Queries;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("EventStore");
if (connectionString is null)
{
Console.WriteLine("connection string was null");
return;
}
builder.Services.AddEventStoreClient(connectionString);
builder.Services.AddSingleton<EventStoreService>();
builder.Services.AddSingleton<GetActualBalanceQuery>();
builder.Services.AddSingleton<WithdrawCommand>();
builder.Services.AddSingleton<DepositCommand>();
builder.Services.AddSingleton<GetHistoryQuery>();
builder.Services.AddSingleton<RollbackCommand>();
var app = builder.Build();
app.MapGet(
"/get-balance/{id:guid}",
async ([FromServices] GetActualBalanceQuery query, [FromRoute] Guid id) =>
{
var account = await query.Execute(id);
return Results.Ok(account.Balance);
}
);
app.MapGet(
"/withdraw/{id:guid}/{amount:decimal}",
async ([FromServices] GetActualBalanceQuery query, [FromServices] WithdrawCommand command, [FromRoute] Guid id, decimal amount) =>
{
var account = await query.Execute(id);
await command.Execute(account, amount);
return Results.Ok(account.Balance);
}
);
app.MapGet(
"/deposit/{id:guid}/{amount:decimal}",
async ([FromServices] GetActualBalanceQuery query, [FromServices] DepositCommand command, [FromRoute] Guid id, decimal amount) =>
{
var account = await query.Execute(id);
await command.Execute(account, amount);
return Results.Ok(account.Balance);
}
);
app.MapGet(
"/history/{id:guid}",
async ([FromServices] GetHistoryQuery query, [FromRoute] Guid id) =>
{
var result = await query.Execute(id);
return Results.Ok(result);
}
);
app.MapGet(
"/rollback/{id:guid}/{transaction:guid}",
async ([FromServices] GetActualBalanceQuery query, [FromServices] RollbackCommand command, [FromRoute] Guid id, [FromRoute] Guid transaction) =>
{
var account = await query.Execute(id);
var result = await command.Execute(account, transaction);
return Results.Ok(result ? "Success" : "Fail");
}
);
app.Run();