// // Created by quentin on 1/18/23. // #ifndef BUDGET_DATABASE_H #define BUDGET_DATABASE_H #include "models/account.h" #include "optHandlers/createOperation.h" #include "optHandlers/earnOperation.h" #include "optHandlers/paymentOperation.h" #include #include class Database { public: /** * @brief Checks if an accountName exists in the database * * @param accountName The name of the accountName to check for * @param db The database connection to use * * @return True if the accountName exists, false otherwise * * @throws std::runtime_error If the database query fails */ static Budget::Models::Account getAccount(const std::string &accountName, sqlite3 *db); /** * @brief Deletes an accountName from the database * * @param account The name of the accountName to delete * @param db The database connection to use * * @return True if the accountName was successfully deleted, false otherwise * * @throws std::runtime_error If the database query fails */ static void deleteAccount(Budget::Models::Account *account, sqlite3 *db); /** * @brief Caches the value of an accountName by calculating the sum of all payments and earnings * * @param accountId The ID of the accountName to cache the value of * @param db The database connection to use * * @return The cached value of the accountName * * @throws std::runtime_error If the database query fails */ static void cacheAccountValue(Budget::Models::Account *account, sqlite3 *db); /** * @brief Sets a new description for an accountName * * @param account The accountName name * @param description The new description * @param db The database connection to use * * @throws std::runtime_error If the statement fails to prepare or the step fails to execute */ static void accountDescription(Budget::Models::Account *account, sqlite3 *db); /** * @brief Creates a new accountName in the database with the given name and description * * @param account The name of the accountName to create * @param description The description of the accountName to create * @param db The sqlite3 database connection to use * * @throws std::runtime_error If the accountName could not be created in the database */ static long long int createAccount(Budget::OptHandlers::CreateOperation::Flags *flags, std::string &account, sqlite3 *db); /** * @brief The function records an earning in the database * * @param account The name of the accountName 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 * * @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 'accountName' table using the provided accountName name. * If the query fails to prepare or execute, the function throws a runtime_error. */ static long long int earn(Budget::Models::Account *account, Budget::OptHandlers::EarnOperation::Flags *flags, sqlite3 *db); /** * @brief The function records an payment in the database * * @param account The name of the accountName 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 'accountName' table using the provided accountName name. * If the query fails to prepare or execute, the function throws a runtime_error. */ static long long int pay(Budget::Models::Account *account, Budget::OptHandlers::PaymentOperation::Flags *flags, sqlite3 *db); }; #endif //BUDGET_DATABASE_H