From b02994d0418d97913ffe1e92f4fcd7b3bcaf715c Mon Sep 17 00:00:00 2001 From: Quentin Snow Date: Fri, 27 Jan 2023 20:47:33 -0600 Subject: [PATCH] Account pay functional --- src/database.cpp | 22 ++++++++++++++++++++++ src/database.h | 23 +++++++++++++++++++++++ src/optHandlers/PaymentOperation.cpp | 7 ++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/database.cpp b/src/database.cpp index b81b233..3f1ca90 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -179,3 +179,25 @@ void Database::earn(const std::string &account, long double &value, std::string if (rc != SQLITE_DONE) throw std::runtime_error("Failed to create earning"); } + +void Database::pay(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 payment (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 pay 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 payment"); +} \ No newline at end of file diff --git a/src/database.h b/src/database.h index 6c12975..34929de 100644 --- a/src/database.h +++ b/src/database.h @@ -101,6 +101,8 @@ public: * @param date The date of the earning in UNIX time * @param db The sqlite3 database connection * + * @throws std::runtime_error If the earning could not be created in the database + * * 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. @@ -109,6 +111,27 @@ public: earn(const std::string &account, long double &value, std::string &description, std::string &receipt, long long int date, sqlite3 *db); + + /** + * @brief The function records an payment in the database + * + * @param account The name of the account to record the payment in + * @param value The value of the payment + * @param description A description of the payment, can be an empty string + * @param receipt A receipt of the payment, can be an empty string + * @param date The date of the payment in UNIX time + * @param db The sqlite3 database connection + * + * @throws std::runtime_error If the payment could not be created in the database + * + * This function records an payment in the database by inserting a new row in the 'payment' 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 + pay(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 054ce65..7415936 100644 --- a/src/optHandlers/PaymentOperation.cpp +++ b/src/optHandlers/PaymentOperation.cpp @@ -3,11 +3,16 @@ // #include "PaymentOperation.h" +#include "../database.h" +#include "../exceptions/badValue.h" using namespace Budget::OptHandlers; void PaymentOperation::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::pay(account, flags.value, flags.description, flags.receipt, flags.date, db); } PaymentOperation::PaymentOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {} \ No newline at end of file