2024-08-01 17:01:50 +00:00
|
|
|
module account.api;
|
|
|
|
|
|
|
|
import handy_httpd;
|
|
|
|
|
|
|
|
import profile.service;
|
|
|
|
import account.model;
|
|
|
|
import money.currency;
|
2024-09-11 20:15:53 +00:00
|
|
|
import util.json;
|
2024-08-01 17:01:50 +00:00
|
|
|
|
|
|
|
struct AccountResponse {
|
|
|
|
ulong id;
|
|
|
|
string createdAt;
|
|
|
|
bool archived;
|
|
|
|
string type;
|
|
|
|
string numberSuffix;
|
|
|
|
string name;
|
|
|
|
string currency;
|
|
|
|
string description;
|
|
|
|
|
|
|
|
static AccountResponse of(in Account account) {
|
|
|
|
AccountResponse r;
|
|
|
|
r.id = account.id;
|
|
|
|
r.createdAt = account.createdAt.toISOExtString();
|
|
|
|
r.archived = account.archived;
|
|
|
|
r.type = account.type.id;
|
|
|
|
r.numberSuffix = account.numberSuffix;
|
|
|
|
r.name = account.name;
|
|
|
|
r.currency = account.currency.code.dup;
|
|
|
|
r.description = account.description;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleGetAccounts(ref HttpRequestContext ctx) {
|
|
|
|
import std.algorithm;
|
|
|
|
auto ds = getProfileDataSource(ctx);
|
|
|
|
auto accounts = ds.getAccountRepository().findAll()
|
|
|
|
.map!(a => AccountResponse.of(a));
|
2024-09-11 20:15:53 +00:00
|
|
|
writeJsonBody(ctx, accounts);
|
2024-08-01 17:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleGetAccount(ref HttpRequestContext ctx) {
|
|
|
|
ulong accountId = ctx.request.getPathParamAs!ulong("accountId");
|
|
|
|
auto ds = getProfileDataSource(ctx);
|
|
|
|
auto account = ds.getAccountRepository().findById(accountId)
|
|
|
|
.orElseThrow(() => new HttpStatusException(HttpStatus.NOT_FOUND));
|
2024-09-11 20:15:53 +00:00
|
|
|
writeJsonBody(ctx, AccountResponse.of(account));
|
2024-08-01 17:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AccountCreationPayload {
|
|
|
|
string type;
|
|
|
|
string numberSuffix;
|
|
|
|
string name;
|
|
|
|
string currency;
|
|
|
|
string description;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleCreateAccount(ref HttpRequestContext ctx) {
|
|
|
|
auto ds = getProfileDataSource(ctx);
|
2024-09-11 20:15:53 +00:00
|
|
|
AccountCreationPayload payload = readJsonPayload!AccountCreationPayload(ctx);
|
2024-08-01 17:01:50 +00:00
|
|
|
AccountType type = AccountType.fromId(payload.type);
|
|
|
|
Currency currency = Currency.ofCode(payload.currency);
|
|
|
|
Account account = ds.getAccountRepository().insert(
|
|
|
|
type,
|
|
|
|
payload.numberSuffix,
|
|
|
|
payload.name,
|
|
|
|
currency,
|
|
|
|
payload.description
|
|
|
|
);
|
2024-09-11 20:15:53 +00:00
|
|
|
writeJsonBody(ctx, AccountResponse.of(account));
|
2024-08-01 17:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleDeleteAccount(ref HttpRequestContext ctx) {
|
|
|
|
ulong accountId = ctx.request.getPathParamAs!ulong("accountId");
|
|
|
|
auto ds = getProfileDataSource(ctx);
|
|
|
|
ds.getAccountRepository().deleteById(accountId);
|
|
|
|
}
|