From f498328249bf77bf96f5a0bed3a2a9299038a810 Mon Sep 17 00:00:00 2001 From: Lukas Forsberg Date: Fri, 23 Jan 2026 22:19:01 +0100 Subject: [PATCH] cleaned the code --- src/database/database.cpp | 111 ------------------------------------ src/database/database.hpp | 70 +---------------------- src/database/databasepool.h | 1 - src/login/login.cpp | 4 +- src/login/login.hpp | 4 +- src/login/loginDb.cpp | 1 - src/login/loginDb.hpp | 6 +- src/main.cpp | 7 +-- 8 files changed, 6 insertions(+), 198 deletions(-) diff --git a/src/database/database.cpp b/src/database/database.cpp index 6feba3b..4680b36 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -1,114 +1,3 @@ -#include "utils.hpp" #include "databasepool.h" -#include - -using namespace std; - -Database::Database() : - m_db(nullptr) -{} - -Database::~Database() { - sqlite3_close(m_db); -} - -sqlite3_stmt* Database::prepareStmt(const string& sql){ - sqlite3_stmt* stmt = nullptr; - if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) { - CROW_LOG_ERROR << "Failed to prepare statement: " << sqlite3_errmsg(m_db); - return nullptr; - } - return stmt; -} - -bool Database::exec(const char* sqlQuery) { - char* errmsg = nullptr; - int rc = sqlite3_exec(m_db, sqlQuery, nullptr, nullptr, &errmsg); - if (rc != SQLITE_OK) { - CROW_LOG_ERROR << "SQL error: " << errmsg; - sqlite3_free(errmsg); - return false; - } - return true; -} - -bool Database::exec(const std::string& sqlQuery){ - return exec(sqlQuery.c_str()); -} - -sqlite3_stmt* Database::bind(const std::string sql, const Database::QueryData& data) { - sqlite3_stmt* stmt = nullptr; - if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) { - CROW_LOG_ERROR << "Failed to prepare statement: " << sqlite3_errmsg(m_db); - return nullptr; - } - for (int i = 0; i < data.size(); i++) { - std::visit([stmt, i](auto&& val) { - using T = std::decay_t; - if constexpr (std::is_same_v) - sqlite3_bind_int64(stmt, i + 1, val); - else if constexpr (std::is_same_v) - sqlite3_bind_double(stmt, i + 1, val); - else if constexpr (std::is_same_v) - sqlite3_bind_text(stmt, i + 1, val.c_str(), -1, SQLITE_TRANSIENT); - }, data[i]); - } - return stmt; -} - -map Database::getStrMap(const std::string sql, const Database::QueryData& data){ - map map; - sqlite3_stmt* stmt = bind(sql, data); - if (stmt == nullptr) - return map; - - while (sqlite3_step(stmt) == SQLITE_ROW) { - string key = reinterpret_cast(sqlite3_column_text(stmt, 0)); - string value = reinterpret_cast(sqlite3_column_text(stmt, 1)); - map[key] = utils::urlDecode(value); - } - - sqlite3_finalize(stmt); - return map; -} - -set Database::getStrSet(const string& sql){ - sqlite3_stmt* stmt = prepareStmt(sql); - set vec; - if (stmt == nullptr) - return vec; - - while (sqlite3_step(stmt) == SQLITE_ROW) { - string s = reinterpret_cast(sqlite3_column_text(stmt, 0)); - vec.insert(s); - } - - sqlite3_finalize(stmt); - return vec; -} - -std::optional Database::insert(const std::string& sql) { - sqlite3_stmt* stmt = prepareStmt(sql); - if (stmt == nullptr) - return {}; - - if (sqlite3_step(stmt) != SQLITE_DONE) { - CROW_LOG_ERROR << "Insert failed: " << sqlite3_errmsg(m_db); - sqlite3_finalize(stmt); - return {}; - } - - sqlite3_finalize(stmt); - return sqlite3_last_insert_rowid(m_db); -} - -bool Database::open(){ - int rc = sqlite3_open("app.db", &m_db); - if (rc) { - CROW_LOG_ERROR << "Can't open database: " << sqlite3_errmsg(m_db); - return false; - } - return true; -} DatabasePool dbpool(std::thread::hardware_concurrency()); \ No newline at end of file diff --git a/src/database/database.hpp b/src/database/database.hpp index 9cb03ae..e9ae2ef 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -13,75 +13,9 @@ #include "ShadowrunDb.hpp" #include "loginDb.hpp" -class Database { - - typedef std::vector> QueryData; - -public: +namespace Database { static constexpr std::string dbFile = "test.db"; - - Database(); - ~Database(); - - bool open(); - bool exec(const char* sqlQuery); - bool exec(const std::string& sqlQuery); - - std::optional insert(const std::string& sql); - - std::set getStrSet(const std::string& sql); - - template - std::optional getSqlData(sqlite3_stmt* stmt, int i) - { - if (stmt == nullptr) - return {}; - - if constexpr (std::is_same_v) { - return sqlite3_column_int64(stmt, i); - } - else if constexpr (std::is_same_v){ - return reinterpret_cast(sqlite3_column_text(stmt, i)); - } - } - - template - std::optional get(const std::string sql, const QueryData& data){ - sqlite3_stmt* stmt = bind(sql, data); - - std::optional ret = getSqlData(stmt, 0); - sqlite3_finalize(stmt); - return ret; - } - - template - std::optional> get(const std::string sql, const QueryData& data){ - sqlite3_stmt* stmt = bind(sql, data); - if ( (stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW ) ) { - CROW_LOG_ERROR << "Failed to run statement: " << sqlite3_errmsg(m_db); - return {}; - } - std::optional v1 = getSqlData(stmt, 0); - std::optional v2 = getSqlData(stmt, 1); - - if (!v1.has_value() || !v2.has_value()) - return {}; - - sqlite3_finalize(stmt); - return std::make_pair(v1.value(), v2.value()); - } - - sqlite3_stmt* bind(const std::string sql, const QueryData& data); - - std::map getStrMap(const std::string sql, const QueryData& data); - -private: - sqlite3_stmt* prepareStmt(const std::string& sql); - - sqlite3* m_db; - -}; - +} inline auto make_database() { auto storage = sqlite_orm::make_storage(Database::dbFile, diff --git a/src/database/databasepool.h b/src/database/databasepool.h index 83e70d6..87a1108 100644 --- a/src/database/databasepool.h +++ b/src/database/databasepool.h @@ -1,6 +1,5 @@ #include #include -#include #include #include #include "database.hpp" diff --git a/src/login/login.cpp b/src/login/login.cpp index 85f9538..07099b0 100644 --- a/src/login/login.cpp +++ b/src/login/login.cpp @@ -34,7 +34,7 @@ std::optional loginUser(const std::string& username, const std::str return {}; } -bool initLogin(crow::SimpleApp& app) +void initLogin(crow::SimpleApp& app) { // createUser("lukas", "Trollar4928"); @@ -70,7 +70,5 @@ bool initLogin(crow::SimpleApp& app) res.body = "Logged in"; return res; }); - - return true; } } \ No newline at end of file diff --git a/src/login/login.hpp b/src/login/login.hpp index 203248b..ad245b4 100644 --- a/src/login/login.hpp +++ b/src/login/login.hpp @@ -4,12 +4,10 @@ #pragma once #include -#include "unordered_map" -#include "string.h" namespace login { -bool initLogin(crow::SimpleApp& app); +void initLogin(crow::SimpleApp& app); bool isLoggedIn(const crow::request& req); diff --git a/src/login/loginDb.cpp b/src/login/loginDb.cpp index aea5683..fc939f4 100644 --- a/src/login/loginDb.cpp +++ b/src/login/loginDb.cpp @@ -1,6 +1,5 @@ #include "loginDb.hpp" #include "databasepool.h" -#include #include #include #include diff --git a/src/login/loginDb.hpp b/src/login/loginDb.hpp index 1a552bb..4719bd4 100644 --- a/src/login/loginDb.hpp +++ b/src/login/loginDb.hpp @@ -1,15 +1,11 @@ #ifndef __LOGINDB_H__ #define __LOGINDB_H__ -#include #include #include #include #include "json.hpp" -#include "utils.hpp" -#include "sqlite_orm.h" -#include "magic_enum.hpp" - +#include "utils.hpp" namespace login { diff --git a/src/main.cpp b/src/main.cpp index 84daf50..bd0d501 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include #include #include "json_settings.h" -#include "systemd.h" #include "utils.hpp" #include "login.hpp" #include "ShadowrunApi.hpp" @@ -67,6 +66,7 @@ int main() { } shadowrun::initApi(app); + login::initLogin(app); // asssets is not svelte generated CROW_ROUTE(app, "/assets/") @@ -109,11 +109,6 @@ int main() { return crow::response(404, "text/html", data); }); -/* if(!login::initLogin(app)) - { - CROW_LOG_ERROR << "Failed to init Login API"; - } -*/ app.loglevel(crow::LogLevel::INFO); app.bindaddr("0.0.0.0").port(httpPort).multithreaded().run(); }