From 983f1827e6192d2f79fdeb4e905519447b47d845 Mon Sep 17 00:00:00 2001 From: Quentin Snow Date: Sat, 21 Jan 2023 18:27:03 -0600 Subject: [PATCH] account -d and --force-delete now functional --- CMakeLists.txt | 6 ++++-- src/database.cpp | 18 ++++++++++++++++-- src/database.h | 2 ++ src/main.cpp | 2 +- src/optHandlers/accountOperation.cpp | 12 +++++++++++- src/utilities.cpp | 26 ++++++++++++++++++++++++++ src/utilities.h | 17 +++++++++++++++++ 7 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/utilities.cpp create mode 100644 src/utilities.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 039712a..bb25559 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/src/database.cpp b/src/database.cpp index 94037d5..aa98bb2 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -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); +} diff --git a/src/database.h b/src/database.h index b264552..aeb0de5 100644 --- a/src/database.h +++ b/src/database.h @@ -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); }; diff --git a/src/main.cpp b/src/main.cpp index 26a281d..7e83a6d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,6 +54,6 @@ int main(int argc, char *argv[]) { return 1; } - + sqlite3_close_v2(db); return 0; } diff --git a/src/optHandlers/accountOperation.cpp b/src/optHandlers/accountOperation.cpp index a2f7db1..c4efa4c 100644 --- a/src/optHandlers/accountOperation.cpp +++ b/src/optHandlers/accountOperation.cpp @@ -4,13 +4,23 @@ #include "accountOperation.h" #include "../database.h" +#include "../exceptions/badValue.h" +#include "../utilities.h" #include 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)) { diff --git a/src/utilities.cpp b/src/utilities.cpp new file mode 100644 index 0000000..0d6c7b6 --- /dev/null +++ b/src/utilities.cpp @@ -0,0 +1,26 @@ +// +// Created by quentin on 1/21/23. +// + +#include +#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; + } +} diff --git a/src/utilities.h b/src/utilities.h new file mode 100644 index 0000000..14ad667 --- /dev/null +++ b/src/utilities.h @@ -0,0 +1,17 @@ +// +// Created by quentin on 1/21/23. +// + +#ifndef BUDGET_UTILITIES_H +#define BUDGET_UTILITIES_H + + +#include + +class Utilities { +public: + static bool confirm(const std::string &question); +}; + + +#endif //BUDGET_UTILITIES_H