forked from chookspace/chookchat
likes!
This commit is contained in:
@@ -23,7 +23,7 @@ Database::Database(const std::string& path) {
|
|||||||
userid INTEGER NOT NULL,
|
userid INTEGER NOT NULL,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
timestamp INTEGER NOT NULL,
|
timestamp INTEGER NOT NULL,
|
||||||
likes INTEGER
|
likes INTEGER NOT NULL DEFAULT 0
|
||||||
);
|
);
|
||||||
)";
|
)";
|
||||||
|
|
||||||
@@ -158,16 +158,23 @@ void Database::addLike(uint64_t postId, uint64_t userId) {
|
|||||||
std::stringstream sql;
|
std::stringstream sql;
|
||||||
sql << R"(
|
sql << R"(
|
||||||
UPDATE posts
|
UPDATE posts
|
||||||
SET likes = likes + 1
|
SET likes = COALESCE(likes, 0) + 1
|
||||||
WHERE id =
|
WHERE id =
|
||||||
)";
|
)";
|
||||||
sql << postId << ";";
|
sql << postId << ";";
|
||||||
std::string sqlstr = sql.str();
|
std::string sqlstr = sql.str();
|
||||||
|
|
||||||
char* errmsg = nullptr;
|
sqlite3_stmt* stmt;
|
||||||
if (sqlite3_exec(db, sqlstr.c_str(), nullptr, nullptr, &errmsg) != SQLITE_OK) {
|
if (sqlite3_prepare_v2(db, sqlstr.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||||
throw std::runtime_error("sqlite3 error: " + std::string(errmsg));
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -104,17 +104,19 @@ int main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
svr.Post("/like", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
|
svr.Post("/like", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
|
||||||
std::string username = request.get_param_value("username");
|
try {
|
||||||
std::string password = request.get_param_value("password");
|
std::string username = request.form.get_field("username");
|
||||||
std::string post = request.get_param_value("post");
|
std::string password = request.form.get_field("password");
|
||||||
|
std::string post = request.form.get_field("post");
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(data_mutex);
|
std::lock_guard<std::mutex> lock(data_mutex);
|
||||||
|
|
||||||
try {
|
|
||||||
uint64_t postNum = std::stoull(post);
|
uint64_t postNum = std::stoull(post);
|
||||||
database.addLike(postNum, 0);
|
database.addLike(postNum, 0);
|
||||||
} catch (const std::runtime_error& e) {
|
} catch (const std::runtime_error& e) {
|
||||||
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
|
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user