account -d and --force-delete now functional

This commit is contained in:
Quentin Snow 2023-01-21 18:27:03 -06:00
parent c3dbabe42d
commit 983f1827e6
7 changed files with 77 additions and 6 deletions

View File

@ -11,7 +11,8 @@ set(SOURCES
src/optHandlers/createOperation.cpp
src/optHandlers/earnOperation.cpp
src/optHandlers/PaymentOperation.cpp
src/database.cpp)
src/database.cpp
src/utilities.cpp)
set(HEADERS
src/optHandlers/mainOptHandler.h
@ -22,7 +23,8 @@ set(HEADERS
src/optHandlers/PaymentOperation.h
src/database.h
src/exceptions/helpRequested.h
src/exceptions/badValue.h)
src/exceptions/badValue.h
src/utilities.h)
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

View File

@ -10,9 +10,8 @@ bool Database::doesAccountExist(const std::string &account, sqlite3 *db) {
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, "SELECT * FROM account WHERE name = ?", -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
if (rc != SQLITE_OK)
throw std::runtime_error("Failed to create account existing query");
}
sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT);
@ -28,3 +27,18 @@ bool Database::doesAccountExist(const std::string &account, sqlite3 *db) {
}
}
bool Database::deleteAccount(const std::string &account, sqlite3 *db) {
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, "DELETE FROM account WHERE name = ?", -1, &stmt, nullptr);
if (rc != SQLITE_OK)
throw std::runtime_error("Failed to delete account " + account);
sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return (rc == SQLITE_DONE);
}

View File

@ -12,6 +12,8 @@
class Database {
public:
static bool doesAccountExist(const std::string &account, sqlite3 *db);
static bool deleteAccount(const std::string &account, sqlite3 *db);
};

View File

@ -54,6 +54,6 @@ int main(int argc, char *argv[]) {
return 1;
}
sqlite3_close_v2(db);
return 0;
}

View File

@ -4,13 +4,23 @@
#include "accountOperation.h"
#include "../database.h"
#include "../exceptions/badValue.h"
#include "../utilities.h"
#include <utility>
using namespace Budget::OptHandlers;
void AccountOperation::commit() {
Database::doesAccountExist(account, db);
if (!Database::doesAccountExist(account, db))
throw Budget::Exceptions::BadValue("Account " + account + " doesn't exist");
if (flags.del) {
if (flags.forceDel || Utilities::confirm("Are you sure you'd like to delete " + account)) {
Database::deleteAccount(account, db);
return;
}
}
}
AccountOperation::AccountOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {

26
src/utilities.cpp Normal file
View File

@ -0,0 +1,26 @@
//
// Created by quentin on 1/21/23.
//
#include <iostream>
#include "utilities.h"
bool Utilities::confirm(const std::string &question) {
std::string input;
std::cout << question << std::endl;
std::cout << "Please enter 'y' or 'n': ";
std::cin >> input;
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
while (input != "y" && input != "n" && input != "yes" && input != "no") {
std::cout << "Invalid input. Please enter 'y' or 'n': ";
std::cin >> input;
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
}
if (input == "y" || input == "yes") {
return true;
} else {
return false;
}
}

17
src/utilities.h Normal file
View File

@ -0,0 +1,17 @@
//
// Created by quentin on 1/21/23.
//
#ifndef BUDGET_UTILITIES_H
#define BUDGET_UTILITIES_H
#include <string>
class Utilities {
public:
static bool confirm(const std::string &question);
};
#endif //BUDGET_UTILITIES_H