implemented storing of new transaction

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-04-02 13:17:14 +02:00
parent 677d6c9797
commit c0ab71d71d
7 changed files with 125 additions and 7 deletions

View File

@@ -1,7 +1,9 @@
package de.srsoftware.umbrella.accounting;
import de.srsoftware.umbrella.core.model.Account;
import de.srsoftware.umbrella.core.model.Transaction;
public interface AccountDb {
Account save(Account account);
Transaction save(Transaction transaction);
}

View File

@@ -12,15 +12,20 @@ import de.srsoftware.umbrella.core.constants.Text;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Account;
import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.Transaction;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import org.json.JSONObject;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
import static de.srsoftware.tools.Optionals.nullIfEmpty;
import static de.srsoftware.umbrella.accounting.Constants.CONFIG_DATABASE;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
import static de.srsoftware.umbrella.core.constants.Path.JSON;
import static de.srsoftware.umbrella.core.constants.Path.SEARCH;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidField;
@@ -42,7 +47,7 @@ public class AccountingModule extends BaseHandler implements AccountingService {
addCors(ex);
try {
Optional<Token> token = SessionToken.from(ex).map(Token::of);
var user = ModuleRegistry.userService().loadUser(token);
var user = userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head) {
@@ -60,13 +65,49 @@ public class AccountingModule extends BaseHandler implements AccountingService {
var json = json(ex);
if (!json.has(Field.ACCOUNT)) throw missingField(Field.ACCOUNT);
if (!(json.get(Field.ACCOUNT) instanceof JSONObject acc)) throw invalidField(Field.ACCOUNT,JSON);
// TODO more tests
if (!json.has(Field.DATE)) throw missingField(Field.DATE);
var date = LocalDate.parse(json.getString(Field.DATE));
var dateTime = LocalDateTime.now().withYear(date.getYear()).withMonth(date.getMonthValue()).withDayOfMonth(date.getDayOfMonth());
if (!json.has(Field.AMOUNT)) throw missingField(Field.AMOUNT);
if (!(json.get(Field.AMOUNT) instanceof Number amount)) throw invalidField(Field.AMOUNT,Text.NUMBER);
var purpose = json.has(Field.PURPOSE) ? json.getString(Field.PURPOSE) : null;
if (!json.has(Field.SOURCE)) throw missingField(Field.SOURCE);
if (!(json.get(Field.SOURCE) instanceof JSONObject sourceData)) throw invalidField(Field.SOURCE,JSON);
if (!json.has(Field.DESTINATION)) throw missingField(Field.DESTINATION);
if (!(json.get(Field.DESTINATION) instanceof JSONObject destinationData)) throw invalidField(Field.DESTINATION,JSON);
String source = null, destination = null;
if (sourceData.has(Field.USER_ID)) {
if (!(sourceData.get(Field.USER_ID) instanceof Number uid)) throw invalidField(String.join(".",Field.SOURCE,Field.USER_ID),Text.NUMBER);
var u = userService().loadUser(uid.longValue());
source = ""+u.id();
} else {
if (!sourceData.has(Field.DISPLAY)) throw missingField(String.join(".",Field.SOURCE,Field.DISPLAY));
source = sourceData.getString(Field.DISPLAY);
if (source.isBlank()) throw invalidField(String.join(".",Field.SOURCE,Field.DISPLAY),Text.STRING);
}
if (destinationData.has(Field.USER_ID)) {
if (!(destinationData.get(Field.USER_ID) instanceof Number uid)) throw invalidField(String.join(".",Field.DESTINATION,Field.USER_ID),Text.NUMBER);
var u = userService().loadUser(uid.longValue());
destination = ""+u;
} else {
if (!destinationData.has(Field.DISPLAY)) throw missingField(String.join(".",Field.DESTINATION,Field.DISPLAY));
destination = destinationData.getString(Field.DISPLAY);
if (destination.isBlank()) throw invalidField(String.join(".",Field.DESTINATION,Field.DISPLAY),Text.STRING);
}
Long accountId = null;
if (acc.has(Field.ID)) {
if (!(acc.get(Field.ID) instanceof Number accId)) throw invalidField(String.join(".",Field.ACCOUNT,Field.ID), Text.NUMBER);
if (accId.longValue() != 0) accountId = accId.longValue();
}
Account newAccount = null;
if (accountId == null){
if (!acc.has(Field.NAME)) throw missingField(String.join(".",Field.ACCOUNT,Field.NAME));
var accountName = acc.getString(Field.NAME);
@@ -74,12 +115,12 @@ public class AccountingModule extends BaseHandler implements AccountingService {
var currency = acc.has(Field.CURRENCY) ? nullIfEmpty(acc.getString(Field.CURRENCY)) : null;
var account = accountDb.save(new Account(0, accountName, currency, user.id()));
accountId = account.id();
newAccount = accountDb.save(new Account(0, accountName, currency, user.id()));
accountId = newAccount.id();
}
// TODO: save entry
var transaction = accountDb.save(new Transaction(accountId,dateTime,source,destination,amount.doubleValue(),purpose));
return notFound(ex);
return sendContent(ex,newAccount != null ? newAccount : transaction);
}
}

View File

@@ -4,9 +4,11 @@ import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.constants.Field;
import de.srsoftware.umbrella.core.model.Account;
import de.srsoftware.umbrella.core.model.Transaction;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.ZoneOffset;
import static de.srsoftware.tools.NotImplemented.notImplemented;
import static de.srsoftware.umbrella.accounting.Constants.TABLE_ACCOUNTS;
@@ -85,4 +87,17 @@ public class SqliteDb extends BaseDb implements AccountDb {
throw notImplemented(this,"save(account)");
}
}
@Override
public Transaction save(Transaction transaction) {
try {
var timestamp = transaction.date().toEpochSecond(ZoneOffset.UTC);
Query.replaceInto(TABLE_TRANSACTIONS,Field.ACCOUNT,Field.TIMESTAMP,Field.SOURCE,Field.DESTINATION, Field.AMOUNT,Field.DESCRIPTION)
.values(transaction.accountId(),timestamp,transaction.source(),transaction.destination(),transaction.amount(),transaction.purpose())
.execute(db).close();
return transaction;
} catch (SQLException e) {
throw failedToStoreObject(transaction);
}
}
}