Account earn functional

This commit is contained in:
Quentin Snow 2023-01-27 19:47:50 -06:00
parent 2c3436ee94
commit c610a0572c
6 changed files with 51 additions and 13 deletions

View File

@ -157,3 +157,25 @@ void Database::createAccount(const std::string &account, sqlite3 *db) {
if (rc != SQLITE_DONE) if (rc != SQLITE_DONE)
throw std::runtime_error("Failed to create account " + account); throw std::runtime_error("Failed to create account " + account);
} }
void Database::earn(const std::string &account, long double &value, std::string &description, std::string &receipt,
long long date, sqlite3 *db) {
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, "INSERT INTO earning (value, description, receipt, accountId, date) VALUES "
"(?, ?, ?, (SELECT id FROM account WHERE name = ?), ?)", -1, &stmt, nullptr);
if (rc != SQLITE_OK)
throw std::runtime_error("Failed preparing earn statement");
sqlite3_bind_double(stmt, 1, value);
sqlite3_bind_text(stmt, 2, (description.empty() ? nullptr : description.c_str()), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, (receipt.empty() ? nullptr : receipt.c_str()), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, (account.empty() ? nullptr : account.c_str()), -1, SQLITE_TRANSIENT);
sqlite3_bind_int64(stmt, 5, date);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE)
throw std::runtime_error("Failed to create earning");
}

View File

@ -90,6 +90,25 @@ public:
* @throws std::runtime_error If the account could not be created in the database * @throws std::runtime_error If the account could not be created in the database
*/ */
static void createAccount(const std::string &account, sqlite3 *db); static void createAccount(const std::string &account, sqlite3 *db);
/**
* @brief The function records an earning in the database
*
* @param account The name of the account to record the earning in
* @param value The value of the earning
* @param description A description of the earning, can be an empty string
* @param receipt A receipt of the earning, can be an empty string
* @param date The date of the earning in UNIX time
* @param db The sqlite3 database connection
*
* This function records an earning in the database by inserting a new row in the 'earning' table with the provided information.
* The accountId is retrieved from the 'account' table using the provided account name.
* If the query fails to prepare or execute, the function throws a runtime_error.
*/
static void
earn(const std::string &account, long double &value, std::string &description, std::string &receipt,
long long int date,
sqlite3 *db);
}; };

View File

@ -10,6 +10,4 @@ void PaymentOperation::commit() {
//TODO This function will be called when the action needs to be done //TODO This function will be called when the action needs to be done
} }
PaymentOperation::PaymentOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) { PaymentOperation::PaymentOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}
}

View File

@ -32,6 +32,4 @@ void AccountOperation::commit() {
} }
} }
AccountOperation::AccountOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) { AccountOperation::AccountOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}
}

View File

@ -19,6 +19,4 @@ void CreateOperation::commit() {
Database::createAccount(account, flags.description, db); Database::createAccount(account, flags.description, db);
} }
CreateOperation::CreateOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) { CreateOperation::CreateOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}
}

View File

@ -3,13 +3,16 @@
// //
#include "earnOperation.h" #include "earnOperation.h"
#include "../database.h"
#include "../exceptions/badValue.h"
using namespace Budget::OptHandlers; using namespace Budget::OptHandlers;
void EarnOperation::commit() { void EarnOperation::commit() {
//TODO This function will be called when the action needs to be done if (!Database::doesAccountExist(account, db))
throw Budget::Exceptions::BadValue("Account " + account + " doesn't exist");
Database::earn(account, flags.value, flags.description, flags.receipt, flags.date, db);
} }
EarnOperation::EarnOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) { EarnOperation::EarnOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}
}