-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWithdrawFunds.cs
More file actions
42 lines (37 loc) · 1.24 KB
/
WithdrawFunds.cs
File metadata and controls
42 lines (37 loc) · 1.24 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
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using Wolverine.Http;
using Wolverine.Marten;
namespace BankAccountES;
public record WithdrawFunds(Guid AccountId, decimal Amount)
{
public class Validator : AbstractValidator<WithdrawFunds>
{
public Validator()
{
RuleFor(x => x.AccountId).NotEmpty();
RuleFor(x => x.Amount).GreaterThan(0);
}
}
}
public static class WithdrawFundsEndpoint
{
// Sad path: insufficient funds check using the loaded aggregate
public static ProblemDetails Validate(WithdrawFunds command, Account account)
{
if (account.Balance < command.Amount)
return new ProblemDetails
{
Detail = $"Insufficient funds. Balance: {account.Balance:C}, requested: {command.Amount:C}",
Status = 400,
};
return WolverineContinue.NoProblems;
}
[WolverinePost("/api/accounts/{accountId}/withdrawals")]
[AggregateHandler]
public static (IResult, FundsWithdrawn) Post(WithdrawFunds command, Account account)
{
var newBalance = account.Balance - command.Amount;
return (Results.NoContent(), new FundsWithdrawn(command.AccountId, command.Amount, newBalance));
}
}