diff --git a/client/script.js b/client/script.js index 763c74f..f29aba3 100644 --- a/client/script.js +++ b/client/script.js @@ -8,7 +8,7 @@ async function like(id) { }); if (result.status === 401) { - alert("you need to login before you like a post"); + window.location.href = "/login"; return; } diff --git a/server/src/db.cpp b/server/src/db.cpp index 29bae95..aa5fe94 100644 --- a/server/src/db.cpp +++ b/server/src/db.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,11 @@ Database::Database(const std::string& path) { userid INTEGER NOT NULL, expiry INTEGER NOT NULL ); + + CREATE TABLE IF NOT EXISTS likes ( + postid INTEGER NOT NULL, + userid INTEGER NOT NULL + ); )"; char* errmsg = nullptr; @@ -183,13 +189,9 @@ void Database::addPost(const Post& post) { } void Database::addLike(uint64_t postId, uint64_t userId) { + // check if the post has already been liked std::stringstream sql; - sql << R"( - UPDATE posts - SET likes = COALESCE(likes, 0) + 1 - WHERE id = - )"; - sql << postId << ";"; + sql << "SELECT * FROM likes WHERE postid = " << postId << " AND userId = " << userId << ";"; std::string sqlstr = sql.str(); sqlite3_stmt* stmt; @@ -198,11 +200,38 @@ void Database::addLike(uint64_t postId, uint64_t userId) { } if (sqlite3_step(stmt) != SQLITE_DONE) { - throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db))); + sqlite3_finalize(stmt); + throw std::runtime_error("you lowkey already liked the post"); } - sqlite3_finalize(stmt); + // add like to the post + sql.str(""); + sql.clear(); + sql << "UPDATE posts SET likes = COALESCE(likes, 0) + 1 WHERE id = " << postId << ";"; + sqlstr = sql.str(); + + if (sqlite3_prepare_v2(db, sqlstr.c_str(), -1, &stmt, nullptr) != SQLITE_OK) { + throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db))); + } + if (sqlite3_step(stmt) != SQLITE_DONE) { + throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db))); + } + sqlite3_finalize(stmt); + + // add like to the likes table + sql.str(""); + sql.clear(); + sql << "INSERT INTO likes (postid, userid) VALUES (" << postId << ", " << userId << ");"; + sqlstr = sql.str(); + + if (sqlite3_prepare_v2(db, sqlstr.c_str(), -1, &stmt, nullptr) != SQLITE_OK) { + throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db))); + } + if (sqlite3_step(stmt) != SQLITE_DONE) { + throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db))); + } + sqlite3_finalize(stmt); } diff --git a/server/src/main.cpp b/server/src/main.cpp index 6634cfc..1110223 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -282,14 +282,18 @@ int main() { svr.Post("/like", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) { try { - std::string username = request.form.get_field("username"); - std::string password = request.form.get_field("password"); std::string post = request.form.get_field("post"); - std::lock_guard lock(data_mutex); + std::optional user = getLoggedInUser(request, database); + if (!user.has_value()) { + response.status = 401; + response.set_content("i dunno that user", "text/plain"); + return; + } + uint64_t postNum = std::stoull(post); - database.addLike(postNum, 0); + database.addLike(postNum, user->id); } catch (const std::runtime_error& e) { response.status = 500; response.set_content("

there was an error :( it is: " + std::string(e.what()) + "

", "text/html");