budget/src/database.h

119 lines
4.2 KiB
C
Raw Normal View History

//
// 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 <string>
#include <sqlite3.h>
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);
2023-01-22 17:05:29 -06:00
/**
* @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);
2023-01-26 18:54:38 -06:00
/**
* @brief Sets a new description for an accountName
*
* @param account The accountName name
2023-01-26 18:54:38 -06:00
* @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
*/
2023-02-05 22:20:34 -06:00
static long long int
createAccount(Budget::OptHandlers::CreateOperation::Flags *flags, std::string &account, sqlite3 *db);
2023-01-27 19:47:50 -06:00
/**
* @brief The function records an earning in the database
*
* @param account The name of the accountName to record the earning in
2023-01-27 19:47:50 -06:00
* @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
*
2023-01-27 20:47:33 -06:00
* @throws std::runtime_error If the earning could not be created in the database
*
2023-01-27 19:47:50 -06:00
* 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.
2023-01-27 19:47:50 -06:00
* 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,
2023-01-27 19:47:50 -06:00
sqlite3 *db);
2023-01-27 20:47:33 -06:00
/**
* @brief The function records an payment in the database
*
* @param account The name of the accountName to record the payment in
2023-01-27 20:47:33 -06:00
* @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.
2023-01-27 20:47:33 -06:00
* 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