Account earn functional
This commit is contained in:
parent
2c3436ee94
commit
c610a0572c
@ -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");
|
||||
}
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
|
||||
|
@ -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)) {
|
||||
|
||||
}
|
||||
PaymentOperation::PaymentOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}
|
@ -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)) {}
|
||||
|
@ -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)) {}
|
||||
|
@ -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)) {
|
||||
|
||||
}
|
||||
EarnOperation::EarnOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {}
|
Loading…
x
Reference in New Issue
Block a user