keep working on the like button
This commit is contained in:
1
build.sh
1
build.sh
@@ -3,3 +3,4 @@
|
|||||||
bin2cpp --file=client/footer.html --output=server/src/generated
|
bin2cpp --file=client/footer.html --output=server/src/generated
|
||||||
bin2cpp --file=client/header.html --output=server/src/generated
|
bin2cpp --file=client/header.html --output=server/src/generated
|
||||||
bin2cpp --file=client/style.css --output=server/src/generated
|
bin2cpp --file=client/style.css --output=server/src/generated
|
||||||
|
bin2cpp --file=client/script.js --output=server/src/generated
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<title>posts</title>
|
<title>posts</title>
|
||||||
<link rel="stylesheet" type="text/css" href="/style.css">
|
<link rel="stylesheet" type="text/css" href="/style.css">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<script src="/script.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="postbox">
|
<div class="postbox">
|
||||||
|
|||||||
15
client/script.js
Normal file
15
client/script.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
function like(id) {
|
||||||
|
|
||||||
|
const usernameBox = document.getElementById('username');
|
||||||
|
const passwordBox = document.getElementById('password');
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('username', usernameBox.value);
|
||||||
|
formData.append('password', passwordBox.value);
|
||||||
|
formData.append('post', id);
|
||||||
|
|
||||||
|
fetch('/like', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -55,6 +55,11 @@ img {
|
|||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.post-buttons {
|
||||||
|
display: flex;
|
||||||
|
gap: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
@media (max-width: 600px) {
|
@media (max-width: 600px) {
|
||||||
input, textarea, button {
|
input, textarea, button {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ sources = [
|
|||||||
'src/generated/header.cpp',
|
'src/generated/header.cpp',
|
||||||
'src/generated/footer.cpp',
|
'src/generated/footer.cpp',
|
||||||
'src/generated/style.cpp',
|
'src/generated/style.cpp',
|
||||||
|
'src/generated/script.cpp',
|
||||||
|
|
||||||
# bcrypt
|
# bcrypt
|
||||||
'src/bcrypt/bcrypt.cpp',
|
'src/bcrypt/bcrypt.cpp',
|
||||||
|
|||||||
@@ -154,6 +154,22 @@ void Database::addPost(const Post& post) {
|
|||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Database::addLike(uint64_t postId, uint64_t userId) {
|
||||||
|
std::stringstream sql;
|
||||||
|
sql << R"(
|
||||||
|
UPDATE posts
|
||||||
|
SET likes = likes + 1
|
||||||
|
WHERE id =
|
||||||
|
)";
|
||||||
|
sql << postId << ";";
|
||||||
|
std::string sqlstr = sql.str();
|
||||||
|
|
||||||
|
char* errmsg = nullptr;
|
||||||
|
if (sqlite3_exec(db, sqlstr.c_str(), nullptr, nullptr, &errmsg) != SQLITE_OK) {
|
||||||
|
throw std::runtime_error("sqlite3 error: " + std::string(errmsg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ class Database {
|
|||||||
std::vector<Post> getUserPosts(uint64_t userId);
|
std::vector<Post> getUserPosts(uint64_t userId);
|
||||||
std::vector<Post> getTopPosts(uint64_t amount);
|
std::vector<Post> getTopPosts(uint64_t amount);
|
||||||
void addPost(const Post& post);
|
void addPost(const Post& post);
|
||||||
|
void addLike(uint64_t postId, uint64_t userId);
|
||||||
|
|
||||||
std::optional<User> getUser(uint64_t id);
|
std::optional<User> getUser(uint64_t id);
|
||||||
std::optional<User> getUserByName(const std::string& name);
|
std::optional<User> getUserByName(const std::string& name);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -14,15 +15,18 @@
|
|||||||
#include "generated/header.h"
|
#include "generated/header.h"
|
||||||
#include "generated/footer.h"
|
#include "generated/footer.h"
|
||||||
#include "generated/style.h"
|
#include "generated/style.h"
|
||||||
|
#include "generated/script.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
const bin2cpp::File& headerfile = bin2cpp::getHeaderHtmlFile();
|
const bin2cpp::File& headerfile = bin2cpp::getHeaderHtmlFile();
|
||||||
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
|
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
|
||||||
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
|
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
|
||||||
|
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
|
||||||
|
|
||||||
std::string header{headerfile.getBuffer(), headerfile.getSize()};
|
std::string header{headerfile.getBuffer(), headerfile.getSize()};
|
||||||
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
|
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
|
||||||
std::string style{stylefile.getBuffer(), stylefile.getSize()};
|
std::string style{stylefile.getBuffer(), stylefile.getSize()};
|
||||||
|
std::string script{scriptfile.getBuffer(), scriptfile.getSize()};
|
||||||
|
|
||||||
Database database{"chookchat.db"};
|
Database database{"chookchat.db"};
|
||||||
|
|
||||||
@@ -34,6 +38,10 @@ int main() {
|
|||||||
response.set_content(style, "text/css");
|
response.set_content(style, "text/css");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
svr.Get("/script.js", [&script](const httplib::Request& request, httplib::Response& response) {
|
||||||
|
response.set_content(script, "text/css");
|
||||||
|
});
|
||||||
|
|
||||||
svr.Get("/", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
|
svr.Get("/", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@@ -57,7 +65,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.Post("/make_post", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
|
svr.Post("/make_post", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
|
||||||
|
|
||||||
std::string username = request.get_param_value("username");
|
std::string username = request.get_param_value("username");
|
||||||
std::string password = request.get_param_value("password");
|
std::string password = request.get_param_value("password");
|
||||||
@@ -90,11 +98,26 @@ int main() {
|
|||||||
database.addPost(Post(post, userId));
|
database.addPost(Post(post, userId));
|
||||||
response.set_redirect("/");
|
response.set_redirect("/");
|
||||||
} 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");
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
svr.Post("/like", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
|
||||||
|
std::string username = request.get_param_value("username");
|
||||||
|
std::string password = request.get_param_value("password");
|
||||||
|
std::string post = request.get_param_value("post");
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(data_mutex);
|
||||||
|
|
||||||
|
try {
|
||||||
|
uint64_t postNum = std::stoull(post);
|
||||||
|
database.addLike(postNum, 0);
|
||||||
|
} catch (const std::runtime_error& e) {
|
||||||
|
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
svr.listen("0.0.0.0", 8080);
|
svr.listen("0.0.0.0", 8080);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ std::string Post::genHtml() const {
|
|||||||
|
|
||||||
ss << "<p class='postinfo'>" << likes << " likes</p>";
|
ss << "<p class='postinfo'>" << likes << " likes</p>";
|
||||||
ss << "<div class='post-buttons'>";
|
ss << "<div class='post-buttons'>";
|
||||||
ss << "<button onclick='like(" << id << ")'>Like</button>";
|
ss << "<button onclick='like(\"" << id << "\")' id='likebutton" << id << "'>Like</button>";
|
||||||
ss << "<button onclick='" << id << "'>Open</button>";
|
ss << "<button onclick='window.location.href = \"/posts/" << id << "\"'>Open</button>";
|
||||||
ss << "</div>";
|
ss << "</div>";
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
|
|||||||
Reference in New Issue
Block a user