diff --git a/CMakeLists.txt b/CMakeLists.txt index d3f8f68..e063dee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,11 +36,11 @@ set(HEADERS add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS}) -IF(CMAKE_BUILD_TYPE STREQUAL "Debug") +IF (CMAKE_BUILD_TYPE STREQUAL "Debug") add_compile_definitions(DEBUG=1) -ELSE() +ELSE () add_compile_definitions(DEBUG=0) -ENDIF() +ENDIF () message(STATUS "Build type is " ${CMAKE_BUILD_TYPE}) diff --git a/src/database.cpp b/src/database.cpp index 83a0ce4..4469411 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -19,13 +19,13 @@ Budget::Models::Account Database::getAccount(const std::string &accountName, sql if (rc == SQLITE_ROW) { long long id = sqlite3_column_int64(stmt, 0); - char* name = (char*)(sqlite3_column_text(stmt, 1)); - char* description = (char*)sqlite3_column_text(stmt, 2); + char *name = (char *) (sqlite3_column_text(stmt, 1)); + char *description = (char *) sqlite3_column_text(stmt, 2); if (sqlite3_column_type(stmt, 3) == SQLITE_NULL || sqlite3_column_type(stmt, 4) == SQLITE_NULL) { sqlite3_finalize(stmt); return {id, name, description, nullptr}; } - auto* money = new Budget::Models::Money(sqlite3_column_int64(stmt, 3), sqlite3_column_int64(stmt, 4)); + auto *money = new Budget::Models::Money(sqlite3_column_int64(stmt, 3), sqlite3_column_int64(stmt, 4)); sqlite3_finalize(stmt); return {id, name, description, money}; } else if (rc == SQLITE_DONE) { @@ -55,10 +55,12 @@ void Database::deleteAccount(Budget::Models::Account *account, sqlite3 *db) { void Database::cacheAccountValue(Budget::Models::Account *account, sqlite3 *db) { sqlite3_stmt *stmt; - int rc = sqlite3_prepare_v2(db, "SELECT SUM(dollars * 100 + cents) / 100 as nDollars, SUM(dollars * 100 + cents) % 100 as nCents FROM(" - "SELECT SUM(dollars * 100 + cents) / 100 AS dollars, SUM(dollars * 100 + cents) % 100 AS cents FROM earning WHERE accountId = ? " - "UNION ALL " - "SELECT SUM(dollars * 100 + cents) / 100 * -1 AS dollars, SUM(dollars * 100 + cents) % 100 * -1 AS cents FROM payment WHERE accountId = ?);", -1, &stmt, nullptr); + int rc = sqlite3_prepare_v2(db, + "SELECT SUM(dollars * 100 + cents) / 100 as nDollars, SUM(dollars * 100 + cents) % 100 as nCents FROM(" + "SELECT SUM(dollars * 100 + cents) / 100 AS dollars, SUM(dollars * 100 + cents) % 100 AS cents FROM earning WHERE accountId = ? " + "UNION ALL " + "SELECT SUM(dollars * 100 + cents) / 100 * -1 AS dollars, SUM(dollars * 100 + cents) % 100 * -1 AS cents FROM payment WHERE accountId = ?);", + -1, &stmt, nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Failed preparing get cashedValue " + account->name); @@ -73,16 +75,15 @@ void Database::cacheAccountValue(Budget::Models::Account *account, sqlite3 *db) if (rc == SQLITE_ROW) { dollars = sqlite3_column_int64(stmt, 0); cents = sqlite3_column_int64(stmt, 1); - } - else if (rc == SQLITE_DONE) { + } else if (rc == SQLITE_DONE) { dollars = 0; cents = 0; - } - else + } else throw std::runtime_error("Failed get cashedValue " + account->name); sqlite3_reset(stmt); - rc = sqlite3_prepare_v2(db, "UPDATE account SET cachedDollars = ?, cachedCents = ? WHERE id = ?", -1, &stmt, nullptr); + rc = sqlite3_prepare_v2(db, "UPDATE account SET cachedDollars = ?, cachedCents = ? WHERE id = ?", -1, &stmt, + nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Failed preparing set cashedValue " + account->name); @@ -115,7 +116,8 @@ void Database::accountDescription(Budget::Models::Account *account, sqlite3 *db) throw std::runtime_error("Failed to set description on " + account->name); } -long long int Database::createAccount(Budget::OptHandlers::CreateOperation::Flags *flags, std::string &account, sqlite3 *db) { +long long int +Database::createAccount(Budget::OptHandlers::CreateOperation::Flags *flags, std::string &account, sqlite3 *db) { sqlite3_stmt *stmt; int rc = sqlite3_prepare_v2(db, "INSERT INTO account (name, description) VALUES (?, ?)", -1, &stmt, nullptr); @@ -123,7 +125,8 @@ long long int Database::createAccount(Budget::OptHandlers::CreateOperation::Flag throw std::runtime_error("Failed preparing createAccount statement"); sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_TRANSIENT); - sqlite3_bind_text(stmt, 2, (flags->description.empty() ? nullptr : flags->description.c_str()), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 2, (flags->description.empty() ? nullptr : flags->description.c_str()), -1, + SQLITE_TRANSIENT); rc = sqlite3_step(stmt); @@ -136,18 +139,21 @@ long long int Database::createAccount(Budget::OptHandlers::CreateOperation::Flag return sqlite3_last_insert_rowid(db); } -long long int Database::earn(Budget::Models::Account *account, Budget::OptHandlers::EarnOperation::Flags *flags, sqlite3 *db) { +long long int +Database::earn(Budget::Models::Account *account, Budget::OptHandlers::EarnOperation::Flags *flags, sqlite3 *db) { sqlite3_stmt *stmt; - int rc = sqlite3_prepare_v2(db, "INSERT INTO earning (dollars, cents, description, receipt, accountId, date) VALUES " - "(?, ?, ?, ?, ?, ?);", - -1, &stmt, nullptr); + int rc = sqlite3_prepare_v2(db, + "INSERT INTO earning (dollars, cents, description, receipt, accountId, date) VALUES " + "(?, ?, ?, ?, ?, ?);", + -1, &stmt, nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Failed preparing earn statement"); sqlite3_bind_int64(stmt, 1, flags->dollars); sqlite3_bind_int64(stmt, 2, flags->cents); - sqlite3_bind_text(stmt, 3, (flags->description.empty() ? nullptr : flags->description.c_str()), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 3, (flags->description.empty() ? nullptr : flags->description.c_str()), -1, + SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 4, (flags->receipt.empty() ? nullptr : flags->receipt.c_str()), -1, SQLITE_TRANSIENT); sqlite3_bind_int64(stmt, 5, account->id); sqlite3_bind_int64(stmt, 6, flags->date); @@ -162,18 +168,21 @@ long long int Database::earn(Budget::Models::Account *account, Budget::OptHandle return sqlite3_last_insert_rowid(db); } -long long int Database::pay(Budget::Models::Account *account, Budget::OptHandlers::PaymentOperation::Flags *flags, sqlite3 *db) { +long long int +Database::pay(Budget::Models::Account *account, Budget::OptHandlers::PaymentOperation::Flags *flags, sqlite3 *db) { sqlite3_stmt *stmt; - int rc = sqlite3_prepare_v2(db, "INSERT INTO payment (dollars, cents, description, receipt, accountId, date) VALUES " - "(?, ?, ?, ?, ?, ?);", - -1, &stmt, nullptr); + int rc = sqlite3_prepare_v2(db, + "INSERT INTO payment (dollars, cents, description, receipt, accountId, date) VALUES " + "(?, ?, ?, ?, ?, ?);", + -1, &stmt, nullptr); if (rc != SQLITE_OK) throw std::runtime_error("Failed preparing pay statement"); sqlite3_bind_int64(stmt, 1, flags->dollars); sqlite3_bind_int64(stmt, 2, flags->cents); - sqlite3_bind_text(stmt, 3, (flags->description.empty() ? nullptr : flags->description.c_str()), -1, SQLITE_TRANSIENT); + sqlite3_bind_text(stmt, 3, (flags->description.empty() ? nullptr : flags->description.c_str()), -1, + SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 4, (flags->receipt.empty() ? nullptr : flags->receipt.c_str()), -1, SQLITE_TRANSIENT); sqlite3_bind_int64(stmt, 5, account->id); sqlite3_bind_int64(stmt, 6, flags->date); diff --git a/src/database.h b/src/database.h index 55cdd6c..de2d966 100644 --- a/src/database.h +++ b/src/database.h @@ -71,7 +71,8 @@ public: * * @throws std::runtime_error If the accountName could not be created in the database */ - static long long int createAccount(Budget::OptHandlers::CreateOperation::Flags *flags, std::string &account, sqlite3 *db); + static long long int + createAccount(Budget::OptHandlers::CreateOperation::Flags *flags, std::string &account, sqlite3 *db); /** * @brief The function records an earning in the database diff --git a/src/main.cpp b/src/main.cpp index ad2b766..5821aa4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ using namespace Budget; -const char* createTables = "create table if not exists account(id INTEGER constraint account_pk primary key autoincrement, name TEXT, description TEXT, cachedDollars INT, cachedCents INT);" +const char *createTables = "create table if not exists account(id INTEGER constraint account_pk primary key autoincrement, name TEXT, description TEXT, cachedDollars INT, cachedCents INT);" "create unique index if not exists account_name_uindex on account (name);" "create table if not exists earning(id INTEGER constraint earning_pk primary key autoincrement, dollars INT not null, cents INT not null, description TEXT, receipt TEXT, accountId INT not null references account on update cascade on delete cascade, date INTEGER not null);" "create index if not exists earning_date_index on earning (date desc);" diff --git a/src/models/account.h b/src/models/account.h index 73e3b3f..297090b 100644 --- a/src/models/account.h +++ b/src/models/account.h @@ -12,9 +12,9 @@ namespace Budget::Models { class Account { public: - Account(long long int id, std::string name, std::string description, Money* cachedValue) + Account(long long int id, std::string name, std::string description, Money *cachedValue) : id(id), name(std::move(name)), description(std::move(description)), cachedValue(cachedValue) {} - + // ~Account() { // delete cachedValue; // } @@ -22,7 +22,7 @@ namespace Budget::Models { long long id; std::string name; std::string description; - Money* cachedValue; + Money *cachedValue; }; } diff --git a/src/models/earning.h b/src/models/earning.h index 90ed231..2cb4619 100644 --- a/src/models/earning.h +++ b/src/models/earning.h @@ -8,7 +8,8 @@ #include "transaction.h" namespace Budget::Models { - class Earning : Transaction {}; + class Earning : Transaction { + }; } #endif //BUDGET_EARNING_H diff --git a/src/models/money.cpp b/src/models/money.cpp index 331915d..2ae2cf2 100644 --- a/src/models/money.cpp +++ b/src/models/money.cpp @@ -17,8 +17,6 @@ Money operator+(const Money &lhs, const Money &rhs) { return {total_dollars, total_cents}; } - - Money operator-(const Money &lhs, const Money &rhs) { long long total_cents = lhs.getDollars() * 100 + lhs.getCents() - rhs.getDollars() * 100 - rhs.getCents(); return {total_cents / 100, std::abs(total_cents % 100)}; diff --git a/src/models/money.h b/src/models/money.h index cb180a2..363cbdf 100644 --- a/src/models/money.h +++ b/src/models/money.h @@ -33,6 +33,7 @@ namespace Budget::Models { }; } + std::ostream &operator<<(std::ostream &os, const Budget::Models::Money &money); #endif //BUDGET_MONEY_H diff --git a/src/models/payment.h b/src/models/payment.h index ce4914d..a8cdd18 100644 --- a/src/models/payment.h +++ b/src/models/payment.h @@ -8,7 +8,8 @@ #include "transaction.h" namespace Budget::Models { - class Payment : Transaction {}; + class Payment : Transaction { + }; } #endif //BUDGET_EARNING_H diff --git a/src/optHandlers/earnOperation.cpp b/src/optHandlers/earnOperation.cpp index 4ba0ea6..4937ab5 100644 --- a/src/optHandlers/earnOperation.cpp +++ b/src/optHandlers/earnOperation.cpp @@ -22,12 +22,12 @@ void EarnOperation::commit() { extension = flags.receipt.substr(pos); } - std::ofstream dest(storageD + "receipts/receipt/" + std::to_string(account.id) + extension, std::ios::binary); + std::ofstream dest(storageD + "receipts/receipt/" + std::to_string(account.id) + extension, + std::ios::binary); dest << source.rdbuf(); source.close(); dest.close(); - } - else { + } else { throw Exceptions::BadValue("File " + flags.receipt + " does not exist"); } } diff --git a/src/optHandlers/mainOptHandler.cpp b/src/optHandlers/mainOptHandler.cpp index 0a38da3..32316ed 100644 --- a/src/optHandlers/mainOptHandler.cpp +++ b/src/optHandlers/mainOptHandler.cpp @@ -17,11 +17,11 @@ using namespace Budget::OptHandlers; MainOptHandler::MainOptHandler(const std::vector &_argv, sqlite3 *db) : argv(_argv), db(db) { struct option actionLongOpts[] = { - {"help", no_argument, nullptr, 'h'}, + {"help", no_argument, nullptr, 'h'}, {"accountName", required_argument, nullptr, 'a'}, - {"create", required_argument, nullptr, 'c'}, - {"earn", required_argument, nullptr, 'e'}, - {"payment", required_argument, nullptr, 'p'} + {"create", required_argument, nullptr, 'c'}, + {"earn", required_argument, nullptr, 'e'}, + {"payment", required_argument, nullptr, 'p'} }; while (true) { @@ -57,7 +57,7 @@ MainOptHandler::MainOptHandler(const std::vector &_argv, sqlite3 *db) : void MainOptHandler::accountOptHandler(std::string account) { struct option accountLongOpts[] = { {"help", no_argument, nullptr, 'h'}, - {"accountName", required_argument, nullptr, 'a'}, + {"accountName", required_argument, nullptr, 'a'}, {"create", required_argument, nullptr, 'c'}, {"earn", required_argument, nullptr, 'e'}, {"payment", required_argument, nullptr, 'p'}, @@ -112,7 +112,7 @@ void MainOptHandler::accountOptHandler(std::string account) { void MainOptHandler::createOptHandler(std::string account) { struct option createLongOpts[] = { {"help", no_argument, nullptr, 'h'}, - {"accountName", required_argument, nullptr, 'a'}, + {"accountName", required_argument, nullptr, 'a'}, {"create", required_argument, nullptr, 'c'}, {"earn", required_argument, nullptr, 'e'}, {"payment", required_argument, nullptr, 'p'}, @@ -154,7 +154,7 @@ void MainOptHandler::createOptHandler(std::string account) { void MainOptHandler::earnOptHandler(std::string account) { struct option earnLongOpts[] = { {"help", no_argument, nullptr, 'h'}, - {"accountName", required_argument, nullptr, 'a'}, + {"accountName", required_argument, nullptr, 'a'}, {"create", required_argument, nullptr, 'c'}, {"earn", required_argument, nullptr, 'e'}, {"payment", required_argument, nullptr, 'p'}, @@ -219,7 +219,7 @@ void MainOptHandler::earnOptHandler(std::string account) { void MainOptHandler::paymentOptHandler(std::string account) { struct option paymentLongOpts[] = { {"help", no_argument, nullptr, 'h'}, - {"accountName", required_argument, nullptr, 'a'}, + {"accountName", required_argument, nullptr, 'a'}, {"create", required_argument, nullptr, 'c'}, {"earn", required_argument, nullptr, 'e'}, {"payment", required_argument, nullptr, 'p'}, diff --git a/src/optHandlers/paymentOperation.cpp b/src/optHandlers/paymentOperation.cpp index 9289166..3a09f7e 100644 --- a/src/optHandlers/paymentOperation.cpp +++ b/src/optHandlers/paymentOperation.cpp @@ -22,12 +22,12 @@ void PaymentOperation::commit() { extension = flags.receipt.substr(pos); } - std::ofstream dest(storageD + "receipts/receipt/" + std::to_string(account.id) + extension, std::ios::binary); + std::ofstream dest(storageD + "receipts/receipt/" + std::to_string(account.id) + extension, + std::ios::binary); dest << source.rdbuf(); source.close(); dest.close(); - } - else { + } else { throw Exceptions::BadValue("File " + flags.receipt + " does not exist"); } } diff --git a/src/utilities.h b/src/utilities.h index fcaaa37..3d1e8d7 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -19,9 +19,9 @@ public: */ static bool confirm(const std::string &question); - static long long int extractDollars(const std::string& string); + static long long int extractDollars(const std::string &string); - static long long int extractCents(const std::string& string); + static long long int extractCents(const std::string &string); static bool hasNumber(const std::string &string); };