first working version where transactions can be stored
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -10,10 +10,7 @@ import de.srsoftware.umbrella.core.api.AccountingService;
|
||||
import de.srsoftware.umbrella.core.constants.Field;
|
||||
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 de.srsoftware.umbrella.core.model.*;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -30,10 +27,8 @@ import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
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;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingField;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
|
||||
public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
private final AccountDb accountDb;
|
||||
@@ -85,32 +80,27 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
}
|
||||
|
||||
private boolean getAccount(UmbrellaUser user, long accountId, HttpExchange ex) throws IOException {
|
||||
var account = accountDb.loadAccount(accountId);
|
||||
var account = accountDb.loadAccount(accountId);
|
||||
var transactions = accountDb.loadTransactions(account);
|
||||
var userMap = new HashMap<Long,UmbrellaUser>();
|
||||
var foundRequestingUser = false;
|
||||
for (var i=0; i<transactions.size();i++){
|
||||
var transaction = transactions.get(i);
|
||||
try {
|
||||
var userId = Long.parseLong(transaction.source());
|
||||
var u = userMap.get(userId);
|
||||
if (u == null) userMap.put(userId,u=userService().loadUser(userId));
|
||||
if (!foundRequestingUser) foundRequestingUser = user.equals(u);
|
||||
transaction = new Transaction(transaction.accountId(),transaction.date(),u.name(),transaction.destination(),transaction.amount(),transaction.purpose());
|
||||
transactions.set(i,transaction);
|
||||
} catch (NumberFormatException ignored){}
|
||||
try {
|
||||
var userId = Long.parseLong(transaction.destination());
|
||||
var u = userMap.get(userId);
|
||||
if (u == null) userMap.put(userId,u=userService().loadUser(userId));
|
||||
if (!foundRequestingUser) foundRequestingUser = user.equals(u);
|
||||
transaction = new Transaction(transaction.accountId(),transaction.date(),transaction.source(),u.name(),transaction.amount(),transaction.purpose());
|
||||
transactions.set(i,transaction);
|
||||
} catch (NumberFormatException ignored){}
|
||||
var userMap = new HashMap<Long,UmbrellaUser>();
|
||||
|
||||
for (var transaction : transactions){
|
||||
var source = transaction.source();
|
||||
if (source.isId()) {
|
||||
var userId = source.id();
|
||||
if (!userMap.containsKey(userId)) userMap.put(source.id(),userService().loadUser(userId));
|
||||
}
|
||||
var destination = transaction.destination();
|
||||
if (destination.isId()) {
|
||||
var userId = destination.id();
|
||||
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.TRANSACTIONS,transactions.stream().map(Transaction::toMap).toList(),
|
||||
Field.USER_LIST,mapValues(userMap)
|
||||
));
|
||||
}
|
||||
|
||||
@@ -138,26 +128,24 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
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;
|
||||
IdOrString 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();
|
||||
if (sourceData.has(Field.ID)) {
|
||||
if (!(sourceData.get(Field.ID) instanceof Number uid)) throw invalidField(String.join(".",Field.SOURCE,Field.ID),Text.NUMBER);
|
||||
source = IdOrString.of(userService().loadUser(uid.longValue()));
|
||||
} 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);
|
||||
source = IdOrString.of(sourceData.getString(Field.DISPLAY));
|
||||
if (source.value().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;
|
||||
if (destinationData.has(Field.ID)) {
|
||||
if (!(destinationData.get(Field.ID) instanceof Number uid)) throw invalidField(String.join(".",Field.DESTINATION,Field.ID),Text.NUMBER);
|
||||
destination = IdOrString.of(userService().loadUser(uid.longValue()));
|
||||
} 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);
|
||||
destination = IdOrString.of(destinationData.getString(Field.DISPLAY));
|
||||
if (destination.value().isBlank()) throw invalidField(String.join(".",Field.DESTINATION,Field.DISPLAY),Text.STRING);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@ public class SqliteDb extends BaseDb implements AccountDb {
|
||||
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())
|
||||
.values(transaction.accountId(),timestamp,transaction.source().value(),transaction.destination().value(),transaction.amount(),transaction.purpose())
|
||||
.execute(db).close();
|
||||
return transaction;
|
||||
} catch (SQLException e) {
|
||||
|
||||
Reference in New Issue
Block a user