implemented creation of new account
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -19,6 +19,7 @@ import java.io.IOException;
|
||||
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.constants.Path.JSON;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.SEARCH;
|
||||
@@ -27,7 +28,6 @@ import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFi
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
|
||||
public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.accounting.database";
|
||||
private final AccountDb accountDb;
|
||||
|
||||
public AccountingModule(Configuration config) throws UmbrellaException {
|
||||
@@ -74,7 +74,7 @@ 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));
|
||||
var account = accountDb.save(new Account(0, accountName, currency, user.id()));
|
||||
accountId = account.id();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.srsoftware.umbrella.accounting;
|
||||
|
||||
public class Constants {
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.accounting.database";
|
||||
|
||||
public static final String TABLE_ACCOUNTS = "accounts";
|
||||
public static final String TABLE_TRANSACTIONS = "transactions";
|
||||
}
|
||||
@@ -1,12 +1,88 @@
|
||||
package de.srsoftware.umbrella.accounting;
|
||||
|
||||
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 java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static de.srsoftware.tools.NotImplemented.notImplemented;
|
||||
import static de.srsoftware.umbrella.accounting.Constants.TABLE_ACCOUNTS;
|
||||
import static de.srsoftware.umbrella.accounting.Constants.TABLE_TRANSACTIONS;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.failedToCreateTable;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.failedToStoreObject;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
public class SqliteDb extends BaseDb implements AccountDb {
|
||||
public SqliteDb(Connection connection) {
|
||||
super(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int createTables() {
|
||||
var version = createSettingsTable();
|
||||
switch (version){
|
||||
case 0:
|
||||
createAccountsTable();
|
||||
createTransactionsTable();
|
||||
}
|
||||
return setCurrentVersion(1);
|
||||
}
|
||||
|
||||
private void createAccountsTable() {
|
||||
var sql = """
|
||||
CREATE TABLE IF NOT EXISTS {0} (
|
||||
{1} INTEGER PRIMARY KEY,
|
||||
{2} VARCHAR(255) NOT NULL,
|
||||
{3} LONG NOT NULL,
|
||||
{4} CURRENCY VARCHAR(16)
|
||||
);""";
|
||||
|
||||
try {
|
||||
sql = format(sql,TABLE_ACCOUNTS, Field.ID, Field.NAME, Field.OWNER, Field.CURRENCY);
|
||||
var stmt = db.prepareStatement(sql);
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw failedToCreateTable(TABLE_ACCOUNTS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void createTransactionsTable() {
|
||||
var sql = """
|
||||
CREATE TABLE IF NOT EXISTS {0} (
|
||||
{1} LONG NOT NULL,
|
||||
{2} LONG NOT NULL,
|
||||
{3} VARCHAR(255) NOT NULL,
|
||||
{4} VARCHAR(255) NOT NULL,
|
||||
{5} DOUBLE NOT NULL,
|
||||
{6} TEXT
|
||||
);""";
|
||||
try {
|
||||
sql = format(sql,TABLE_TRANSACTIONS,Field.ACCOUNT,Field.TIMESTAMP,Field.SOURCE,Field.DESTINATION, Field.AMOUNT,Field.DESCRIPTION);
|
||||
var stmt = db.prepareStatement(sql);
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw failedToCreateTable(TABLE_TRANSACTIONS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account save(Account account) {
|
||||
if (account.id() == 0) try { // new account
|
||||
var rs = Query.insertInto(TABLE_ACCOUNTS,Field.NAME, Field.CURRENCY, Field.OWNER).values(account.name(),account.currency(),account.ownerId()).execute(db).getGeneratedKeys();
|
||||
Long newId = null;
|
||||
if (rs.next()) newId = rs.getLong(1);
|
||||
rs.close();
|
||||
if (newId == null) throw failedToStoreObject(account);
|
||||
return account.withId(newId);
|
||||
} catch (SQLException e) {
|
||||
throw failedToStoreObject(account).causedBy(e);
|
||||
} else {
|
||||
throw notImplemented(this,"save(account)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user