From c610a0572cb2b2951770e3071147ab2400f95294 Mon Sep 17 00:00:00 2001 From: Quentin Snow Date: Fri, 27 Jan 2023 19:47:50 -0600 Subject: [PATCH] Account earn functional --- src/database.cpp | 22 ++++++++++++++++++++++ src/database.h | 19 +++++++++++++++++++ src/optHandlers/PaymentOperation.cpp | 4 +--- src/optHandlers/accountOperation.cpp | 4 +--- src/optHandlers/createOperation.cpp | 4 +--- src/optHandlers/earnOperation.cpp | 11 +++++++---- 6 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index 8225abd..b81b233 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -157,3 +157,25 @@ void Database::createAccount(const std::string &account, sqlite3 *db) { if (rc != SQLITE_DONE) 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"); +} diff --git a/src/database.h b/src/database.h index 6d7374e..6c12975 100644 --- a/src/database.h +++ b/src/database.h @@ -90,6 +90,25 @@ public: * @throws std::runtime_error If the account could not be created in the database */ 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); }; diff --git a/src/optHandlers/PaymentOperation.cpp b/src/optHandlers/PaymentOperation.cpp index 83fa4c8..054ce65 100644 --- a/src/optHandlers/PaymentOperation.cpp +++ b/src/optHandlers/PaymentOperation.cpp @@ -10,6 +10,4 @@ void PaymentOperation::commit() { //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)) { - -} \ No newline at end of file +PaymentOperation::PaymentOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {} \ No newline at end of file diff --git a/src/optHandlers/accountOperation.cpp b/src/optHandlers/accountOperation.cpp index 93be52f..1855db3 100644 --- a/src/optHandlers/accountOperation.cpp +++ b/src/optHandlers/accountOperation.cpp @@ -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)) {} diff --git a/src/optHandlers/createOperation.cpp b/src/optHandlers/createOperation.cpp index 05a2728..8055f73 100644 --- a/src/optHandlers/createOperation.cpp +++ b/src/optHandlers/createOperation.cpp @@ -19,6 +19,4 @@ void CreateOperation::commit() { 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)) {} diff --git a/src/optHandlers/earnOperation.cpp b/src/optHandlers/earnOperation.cpp index 91ec28d..12c871a 100644 --- a/src/optHandlers/earnOperation.cpp +++ b/src/optHandlers/earnOperation.cpp @@ -3,13 +3,16 @@ // #include "earnOperation.h" +#include "../database.h" +#include "../exceptions/badValue.h" using namespace Budget::OptHandlers; 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)) { - -} \ No newline at end of file +EarnOperation::EarnOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {} \ No newline at end of file