2026-07-20 10:57:07 +10:00
|
|
|
#ifndef DB_H
|
|
|
|
|
#define DB_H
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <optional>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "post.h"
|
|
|
|
|
|
2026-07-20 20:53:33 +10:00
|
|
|
void sanitize(std::string& str);
|
|
|
|
|
|
2026-07-21 12:06:46 +10:00
|
|
|
std::string generateSecureToken(size_t numBytes);
|
|
|
|
|
|
2026-07-20 10:57:07 +10:00
|
|
|
struct User {
|
|
|
|
|
uint64_t id = 0;
|
|
|
|
|
std::string name = "";
|
|
|
|
|
std::string passwordHash = "";
|
|
|
|
|
|
2026-07-20 20:53:33 +10:00
|
|
|
User(uint64_t id, std::string namein, const std::string& passwordHash) :
|
|
|
|
|
id(id), passwordHash(passwordHash) {
|
|
|
|
|
sanitize(namein);
|
|
|
|
|
name = namein;
|
|
|
|
|
}
|
2026-07-20 10:57:07 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Database {
|
|
|
|
|
|
|
|
|
|
sqlite3* db;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Database() = delete;
|
|
|
|
|
Database(const std::string& path);
|
|
|
|
|
|
|
|
|
|
~Database();
|
|
|
|
|
|
|
|
|
|
std::optional<Post> getPost(uint64_t id);
|
|
|
|
|
std::vector<Post> getUserPosts(uint64_t userId);
|
|
|
|
|
std::vector<Post> getTopPosts(uint64_t amount);
|
|
|
|
|
void addPost(const Post& post);
|
2026-07-20 19:02:04 +10:00
|
|
|
void addLike(uint64_t postId, uint64_t userId);
|
2026-07-20 10:57:07 +10:00
|
|
|
|
|
|
|
|
std::optional<User> getUser(uint64_t id);
|
|
|
|
|
std::optional<User> getUserByName(const std::string& name);
|
2026-07-21 12:06:46 +10:00
|
|
|
std::optional<User> getUserByToken(const std::string& token);
|
|
|
|
|
std::optional<std::string> createNewToken(uint64_t id);
|
2026-07-20 10:57:07 +10:00
|
|
|
|
|
|
|
|
// this will modify the user to have their user ID
|
|
|
|
|
void addUser(User& user);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|