Account creation with and without description

This commit is contained in:
Quentin Snow 2023-01-27 19:02:18 -06:00
parent d3f3ebac3f
commit 5a88299f9e
3 changed files with 65 additions and 2 deletions

View File

@ -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);
}

View File

@ -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);
};

View File

@ -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)) {