diff --git a/finnow-api/source/account/data_impl_sqlite.d b/finnow-api/source/account/data_impl_sqlite.d index e37b2b4..c68f5a9 100644 --- a/finnow-api/source/account/data_impl_sqlite.d +++ b/finnow-api/source/account/data_impl_sqlite.d @@ -53,7 +53,32 @@ SQL"); } Account update(ulong id, in Account newData) { - return newData; // TODO: + return doTransaction(db, () { + Account oldAccount = this.findById(id).orElseThrow("Account doesn't exist."); + bool typeDiff = oldAccount.type != newData.type; + bool numberSuffixDiff = oldAccount.numberSuffix != newData.numberSuffix; + bool nameDiff = oldAccount.name != newData.name; + bool currencyDiff = oldAccount.currency != newData.currency; + bool descriptionDiff = oldAccount.description != newData.description; + util.sqlite.update( + db, + q"SQL + UPDATE account + SET type = ?, + number_suffix = ?, + name = ?, + currency = ?, + description = ? + WHERE id = ? +SQL", + newData.type.id, + newData.numberSuffix, + newData.name, + newData.currency.code, + newData.description + ); + return this.findById(id).orElseThrow("Account doesn't exist"); + }); } void deleteById(ulong id) { @@ -76,14 +101,45 @@ SQL"); } AccountCreditCardProperties getCreditCardProperties(ulong id) { - Statement stmt = db.prepare("SELECT * FROM account_credit_card_properties WHERE account_id = ?"); - stmt.bind(1, id); - ResultRange result = stmt.execute(); - return parseCreditCardProperties(result.front); + Account account = findById(id).orElseThrow("Account doesn't exist."); + if (account.type != AccountTypes.CREDIT_CARD) throw new Exception("Account is not credit card."); + auto optionalProps = findOne( + db, + "SELECT * FROM account_credit_card_properties WHERE account_id = ?", + &parseCreditCardProperties, + id + ); + if (!optionalProps.isNull) return optionalProps.value; + // No properties exist, so set them and return the new data. + AccountCreditCardProperties props; + props.account_id = account.id; + props.creditLimit = -1; + util.sqlite.update( + db, + "INSERT INTO account_credit_card_properties (account_id, credit_limit) VALUES (?, ?)", + props.account_id, + props.creditLimit + ); + return props; } void setCreditCardProperties(ulong id, in AccountCreditCardProperties props) { - // TODO: + bool hasProps = exists(db, "SELECT * FROM account_credit_card_properties WHERE account_id = ?", id); + if (hasProps) { + util.sqlite.update( + db, + "UPDATE account_credit_card_properties SET credit_limit = ? WHERE account_id = ?", + props.creditLimit, + id + ); + } else { + util.sqlite.update( + db, + "INSERT INTO account_credit_card_properties (account_id, credit_limit) VALUES (?, ?)", + id, + props.creditLimit + ); + } } History getHistory(ulong id) { diff --git a/finnow-api/source/util/sqlite.d b/finnow-api/source/util/sqlite.d index e866383..4e5b164 100644 --- a/finnow-api/source/util/sqlite.d +++ b/finnow-api/source/util/sqlite.d @@ -74,7 +74,8 @@ int update(Args...)(Database db, string query, Args args) { * Params: * db = The database to use. * dg = The delegate block of code to run in the transaction. - * Returns: The return value of the delegate. + * Returns: The return value of the delegate, if the delegate does indeed + * return something. */ T doTransaction(T)(Database db, T delegate() dg) { try {