implemented transaction propagation via event bus
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
description = "Umbrella : Accounting"
|
||||
|
||||
dependencies{
|
||||
implementation(project(":bus"))
|
||||
implementation(project(":core"))
|
||||
}
|
||||
@@ -10,6 +10,9 @@ import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidField;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingField;
|
||||
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
|
||||
import static de.srsoftware.umbrella.messagebus.events.Event.EventType.CREATE;
|
||||
import static de.srsoftware.umbrella.messagebus.events.Event.EventType.UPDATE;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
@@ -27,6 +30,9 @@ import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
import de.srsoftware.umbrella.messagebus.events.Event;
|
||||
import de.srsoftware.umbrella.messagebus.events.TransactionEvent;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -177,6 +183,14 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
|
||||
private boolean getAccount(UmbrellaUser user, long accountId, HttpExchange ex) throws IOException {
|
||||
LOG.log(WARNING,"Missing authorization check in AccountingModule.getAccount(…)!");
|
||||
return sendContent(ex, loadAccount(accountId));
|
||||
}
|
||||
|
||||
private boolean getAccounts(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
return sendContent(ex,accountDb.listAccounts(user.id()).stream().map(Account::toMap));
|
||||
}
|
||||
|
||||
public AccountData loadAccount(long accountId){
|
||||
var account = accountDb.loadAccount(accountId);
|
||||
var transactions = accountDb.loadTransactions(account);
|
||||
var userMap = new HashMap<Long,UmbrellaUser>();
|
||||
@@ -193,20 +207,9 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
if (!userMap.containsKey(userId)) userMap.put(destination.id(),userService().loadUser(userId));
|
||||
}
|
||||
}
|
||||
|
||||
return sendContent(ex, Map.of(
|
||||
Field.ACCOUNT,account.toMap(),
|
||||
Field.TRANSACTIONS,transactions.stream().map(Transaction::toMap).toList(),
|
||||
Field.USER_LIST,mapValues(userMap)
|
||||
));
|
||||
return AccountData.of(account, transactions, userMap);
|
||||
}
|
||||
|
||||
private boolean getAccounts(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
return sendContent(ex,accountDb.listAccounts(user.id()).stream().map(Account::toMap));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static boolean noNumbers(String s){
|
||||
try {
|
||||
Long.parseLong(s);
|
||||
@@ -226,7 +229,9 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
if (json.has(Field.PURPOSE)) transaction.purpose(json.getString(Field.PURPOSE));
|
||||
if (json.has(Field.SOURCE)) transaction.source(IdOrString.of(json.getString(Field.SOURCE)));
|
||||
if (json.has(Field.TAG)) transaction.tags().add(json.getString(Field.TAG));
|
||||
return sendContent(ex,accountDb.save(transaction));
|
||||
var patched = accountDb.save(transaction);
|
||||
messageBus().dispatch(new TransactionEvent(user,patched,UPDATE));
|
||||
return sendContent(ex,patched);
|
||||
}
|
||||
|
||||
private boolean postEntry(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
@@ -291,7 +296,7 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
|
||||
|
||||
var transaction = accountDb.save(new Transaction(0,accountId,dateTime,source,destination,amount.doubleValue(),purpose,tags));
|
||||
|
||||
messageBus().dispatch(new TransactionEvent(user,transaction, CREATE));
|
||||
return sendContent(ex,newAccount != null ? newAccount : transaction);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import static de.srsoftware.umbrella.accounting.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
|
||||
import static de.srsoftware.umbrella.messagebus.events.Event.EventType.UPDATE;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Condition;
|
||||
@@ -19,6 +21,8 @@ import de.srsoftware.umbrella.core.constants.Field;
|
||||
import de.srsoftware.umbrella.core.constants.Text;
|
||||
import de.srsoftware.umbrella.core.model.Account;
|
||||
import de.srsoftware.umbrella.core.model.Transaction;
|
||||
import de.srsoftware.umbrella.messagebus.events.TransactionEvent;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
Reference in New Issue
Block a user