diff --git a/src/database.cpp b/src/database.cpp index 6e9408f..77a3414 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -124,3 +124,36 @@ void Database::accountDescription(const std::string& account, const std::string& if (rc != SQLITE_DONE) throw std::runtime_error("Failed to set description on " + account); } + +void Database::createAccount(const std::string &account, const std::string &description, sqlite3 *db) { + sqlite3_stmt *stmt; + int rc = sqlite3_prepare_v2(db, "INSERT INTO account (name, description) VALUES (?, ?)", -1, &stmt, nullptr); + + if (rc != SQLITE_OK) + throw std::runtime_error("Failed preparing createAccount statement"); + + sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 2, description.c_str(), -1, SQLITE_TRANSIENT); + + rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); + + if (rc != SQLITE_DONE) + throw std::runtime_error("Failed to create account " + account); +} + +void Database::createAccount(const std::string &account, sqlite3 *db) { + sqlite3_stmt *stmt; + int rc = sqlite3_prepare_v2(db, "INSERT INTO account (name) VALUES (?)", -1, &stmt, nullptr); + + if (rc != SQLITE_OK) + throw std::runtime_error("Failed preparing createAccount statement"); + + sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT); + + rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); + + if (rc != SQLITE_DONE) + throw std::runtime_error("Failed to create account " + account); +} diff --git a/src/database.h b/src/database.h index 9a71ed8..6d7374e 100644 --- a/src/database.h +++ b/src/database.h @@ -68,7 +68,28 @@ public: * * @throws std::runtime_error If the statement fails to prepare or the step fails to execute */ - static void accountDescription(const std::string& account, const std::string& description, sqlite3 *db); + static void accountDescription(const std::string &account, const std::string &description, sqlite3 *db); + + /** + * @brief Creates a new account in the database with the given name and description + * + * @param account The name of the account to create + * @param description The description of the account to create + * @param db The sqlite3 database connection to use + * + * @throws std::runtime_error If the account could not be created in the database + */ + static void createAccount(const std::string &account, const std::string &description, sqlite3 *db); + + /** + * @brief Creates a new account in the database with the given name + * + * @param account The name of the account to create + * @param db The sqlite3 database connection to use + * + * @throws std::runtime_error If the account could not be created in the database + */ + static void createAccount(const std::string &account, sqlite3 *db); }; diff --git a/src/optHandlers/createOperation.cpp b/src/optHandlers/createOperation.cpp index 2f194e0..05a2728 100644 --- a/src/optHandlers/createOperation.cpp +++ b/src/optHandlers/createOperation.cpp @@ -3,11 +3,20 @@ // #include "createOperation.h" +#include "../database.h" +#include "../exceptions/badValue.h" using namespace Budget::OptHandlers; void CreateOperation::commit() { - //TODO This function will be called when the action needs to be done + if (Database::doesAccountExist(account, db)) { + throw Budget::Exceptions::BadValue("Account already exists, cant create " + account); + } + if (flags.description.empty()) { + Database::createAccount(account, db); + return; + } + Database::createAccount(account, flags.description, db); } CreateOperation::CreateOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {