account -d and --force-delete now functional
This commit is contained in:
parent
c3dbabe42d
commit
983f1827e6
@ -11,7 +11,8 @@ set(SOURCES
|
|||||||
src/optHandlers/createOperation.cpp
|
src/optHandlers/createOperation.cpp
|
||||||
src/optHandlers/earnOperation.cpp
|
src/optHandlers/earnOperation.cpp
|
||||||
src/optHandlers/PaymentOperation.cpp
|
src/optHandlers/PaymentOperation.cpp
|
||||||
src/database.cpp)
|
src/database.cpp
|
||||||
|
src/utilities.cpp)
|
||||||
|
|
||||||
set(HEADERS
|
set(HEADERS
|
||||||
src/optHandlers/mainOptHandler.h
|
src/optHandlers/mainOptHandler.h
|
||||||
@ -22,7 +23,8 @@ set(HEADERS
|
|||||||
src/optHandlers/PaymentOperation.h
|
src/optHandlers/PaymentOperation.h
|
||||||
src/database.h
|
src/database.h
|
||||||
src/exceptions/helpRequested.h
|
src/exceptions/helpRequested.h
|
||||||
src/exceptions/badValue.h)
|
src/exceptions/badValue.h
|
||||||
|
src/utilities.h)
|
||||||
|
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
|
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
|
||||||
|
@ -10,9 +10,8 @@ bool Database::doesAccountExist(const std::string &account, sqlite3 *db) {
|
|||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
int rc = sqlite3_prepare_v2(db, "SELECT * FROM account WHERE name = ?", -1, &stmt, nullptr);
|
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");
|
throw std::runtime_error("Failed to create account existing query");
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT);
|
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);
|
||||||
|
}
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
class Database {
|
class Database {
|
||||||
public:
|
public:
|
||||||
static bool doesAccountExist(const std::string &account, sqlite3 *db);
|
static bool doesAccountExist(const std::string &account, sqlite3 *db);
|
||||||
|
|
||||||
|
static bool deleteAccount(const std::string &account, sqlite3 *db);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,6 +54,6 @@ int main(int argc, char *argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sqlite3_close_v2(db);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,23 @@
|
|||||||
|
|
||||||
#include "accountOperation.h"
|
#include "accountOperation.h"
|
||||||
#include "../database.h"
|
#include "../database.h"
|
||||||
|
#include "../exceptions/badValue.h"
|
||||||
|
#include "../utilities.h"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
using namespace Budget::OptHandlers;
|
using namespace Budget::OptHandlers;
|
||||||
|
|
||||||
void AccountOperation::commit() {
|
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)) {
|
AccountOperation::AccountOperation(sqlite3 *db, std::string account) : Operation(db), account(std::move(account)) {
|
||||||
|
26
src/utilities.cpp
Normal file
26
src/utilities.cpp
Normal 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
17
src/utilities.h
Normal 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
|
Loading…
x
Reference in New Issue
Block a user