Settings
This commit is contained in:
@@ -38,7 +38,8 @@ Database::Database(const std::string& path) {
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
password TEXT NOT NULL,
|
||||
bio TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
@@ -283,10 +284,17 @@ std::optional<User> Database::getUser(uint64_t id) {
|
||||
|
||||
std::string name{reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1))};
|
||||
std::string password{reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2))};
|
||||
const char* bioText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
|
||||
std::string bio;
|
||||
if (bioText == NULL) {
|
||||
bio = "";
|
||||
} else {
|
||||
bio = std::string(bioText);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return User(id, name, password);
|
||||
return User(id, name, password, bio);
|
||||
}
|
||||
|
||||
std::optional<User> Database::getUserByName(const std::string& name) {
|
||||
@@ -307,9 +315,16 @@ std::optional<User> Database::getUserByName(const std::string& name) {
|
||||
|
||||
uint64_t id = sqlite3_column_int64(stmt, 0);
|
||||
std::string password{reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2))};
|
||||
const char* bioText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
|
||||
std::string bio;
|
||||
if (bioText == NULL) {
|
||||
bio = "";
|
||||
} else {
|
||||
bio = std::string(bioText);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return User(id, name, password);
|
||||
return User(id, name, password, bio);
|
||||
}
|
||||
|
||||
std::optional<std::string> Database::createNewToken(uint64_t id) {
|
||||
@@ -388,3 +403,48 @@ void Database::addUser(User& user) {
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void Database::updateUser(const User& user) {
|
||||
const char* sql = R"(
|
||||
UPDATE users
|
||||
SET
|
||||
name = ?,
|
||||
bio = ?,
|
||||
password = ?
|
||||
WHERE id = ?;
|
||||
)";
|
||||
|
||||
sqlite3_stmt* stmt;
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, user.name.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, user.bio.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, user.passwordHash.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int64(stmt, 4, user.id);
|
||||
|
||||
if (sqlite3_step(stmt) != SQLITE_DONE) {
|
||||
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void Database::invalidateUserSessions(const User& user) {
|
||||
const char* sql = R"(
|
||||
DELETE FROM sessions WHERE userid = ?;
|
||||
)";
|
||||
sqlite3_stmt* stmt;
|
||||
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
|
||||
}
|
||||
|
||||
sqlite3_bind_int64(stmt, 1, user.id);
|
||||
|
||||
if (sqlite3_step(stmt) != SQLITE_DONE) {
|
||||
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user