157 lines
4.4 KiB
D
157 lines
4.4 KiB
D
module transaction.api;
|
|
|
|
import handy_http_primitives;
|
|
import handy_http_data.json;
|
|
import handy_http_handlers.path_handler;
|
|
import slf4d;
|
|
import std.typecons;
|
|
|
|
import transaction.model;
|
|
import transaction.data;
|
|
import transaction.service;
|
|
import profile.data;
|
|
import profile.service;
|
|
import account.api;
|
|
import util.money;
|
|
import util.pagination;
|
|
import util.data;
|
|
|
|
// Transactions API
|
|
|
|
immutable DEFAULT_TRANSACTION_PAGE = PageRequest(1, 10, [Sort("timestamp", SortDir.DESC)]);
|
|
|
|
struct TransactionsListItemAccount {
|
|
ulong id;
|
|
string name;
|
|
string type;
|
|
string numberSuffix;
|
|
}
|
|
|
|
struct TransactionsListItemVendor {
|
|
ulong id;
|
|
string name;
|
|
}
|
|
|
|
struct TransactionsListItemCategory {
|
|
ulong id;
|
|
string name;
|
|
string color;
|
|
}
|
|
|
|
/// The transaction data provided when a list of transactions is requested.
|
|
struct TransactionsListItem {
|
|
import asdf : serdeTransformOut;
|
|
|
|
ulong id;
|
|
string timestamp;
|
|
string addedAt;
|
|
ulong amount;
|
|
Currency currency;
|
|
string description;
|
|
|
|
@serdeTransformOut!serializeOptional
|
|
Optional!TransactionsListItemVendor vendor;
|
|
@serdeTransformOut!serializeOptional
|
|
Optional!TransactionsListItemCategory category;
|
|
@serdeTransformOut!serializeOptional
|
|
Optional!TransactionsListItemAccount creditedAccount;
|
|
@serdeTransformOut!serializeOptional
|
|
Optional!TransactionsListItemAccount debitedAccount;
|
|
|
|
string[] tags;
|
|
}
|
|
|
|
void handleGetTransactions(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
PageRequest pr = PageRequest.parse(request, DEFAULT_TRANSACTION_PAGE);
|
|
auto responsePage = getTransactions(ds, pr);
|
|
writeJsonBody(response, responsePage);
|
|
}
|
|
|
|
void handleGetTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
// TODO
|
|
}
|
|
|
|
struct AddTransactionPayloadLineItem {
|
|
long valuePerItem;
|
|
ulong quantity;
|
|
string description;
|
|
Nullable!ulong categoryId;
|
|
}
|
|
|
|
struct AddTransactionPayload {
|
|
string timestamp;
|
|
ulong amount;
|
|
string currencyCode;
|
|
string description;
|
|
Nullable!ulong vendorId;
|
|
Nullable!ulong categoryId;
|
|
Nullable!ulong creditedAccountId;
|
|
Nullable!ulong debitedAccountId;
|
|
string[] tags;
|
|
AddTransactionPayloadLineItem[] lineItems;
|
|
}
|
|
|
|
void handleAddTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
auto payload = readJsonBodyAs!AddTransactionPayload(request);
|
|
addTransaction2(ds, payload);
|
|
}
|
|
|
|
void handleUpdateTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
// TODO
|
|
}
|
|
|
|
void handleDeleteTransaction(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
// TODO
|
|
}
|
|
|
|
private ulong getTransactionIdOrThrow(in ServerHttpRequest request) {
|
|
return getPathParamOrThrow(request, "transactionId");
|
|
}
|
|
|
|
// Vendors API
|
|
|
|
void handleGetVendors(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
TransactionVendor[] vendors = getAllVendors(ds);
|
|
writeJsonBody(response, vendors);
|
|
}
|
|
|
|
void handleGetVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
TransactionVendor vendor = getVendor(ds, getVendorId(request));
|
|
writeJsonBody(response, vendor);
|
|
}
|
|
|
|
struct VendorPayload {
|
|
string name;
|
|
string description;
|
|
}
|
|
|
|
void handleCreateVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
VendorPayload payload = readJsonBodyAs!VendorPayload(request);
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
TransactionVendor vendor = createVendor(ds, payload);
|
|
writeJsonBody(response, vendor);
|
|
}
|
|
|
|
void handleUpdateVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
VendorPayload payload = readJsonBodyAs!VendorPayload(request);
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
TransactionVendor updated = updateVendor(ds, getVendorId(request), payload);
|
|
writeJsonBody(response, updated);
|
|
}
|
|
|
|
void handleDeleteVendor(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
ProfileDataSource ds = getProfileDataSource(request);
|
|
deleteVendor(ds, getVendorId(request));
|
|
}
|
|
|
|
private ulong getVendorId(in ServerHttpRequest request) {
|
|
return getPathParamOrThrow(request, "vendorId");
|
|
}
|
|
|
|
// Categories API
|