likes!
This commit is contained in:
@@ -23,7 +23,7 @@ Database::Database(const std::string& path) {
|
||||
userid INTEGER NOT NULL,
|
||||
content TEXT 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;
|
||||
sql << R"(
|
||||
UPDATE posts
|
||||
SET likes = likes + 1
|
||||
SET likes = COALESCE(likes, 0) + 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));
|
||||
sqlite3_stmt* stmt;
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user