summaryrefslogtreecommitdiff
path: root/src/database/posts.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/database/posts.rs')
-rw-r--r--src/database/posts.rs54
1 files changed, 11 insertions, 43 deletions
diff --git a/src/database/posts.rs b/src/database/posts.rs
index 8ca9b2d..7da3bf0 100644
--- a/src/database/posts.rs
+++ b/src/database/posts.rs
@@ -1,4 +1,3 @@
-use std::collections::HashSet;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use rusqlite::{OptionalExtension, Row};
@@ -7,14 +6,14 @@ use tracing::instrument;
use crate::database;
use crate::types::post::Post;
+use super::{comments, likes};
+
pub fn init() -> Result<(), rusqlite::Error> {
let sql = "
CREATE TABLE IF NOT EXISTS posts (
post_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
content VARCHAR(500) NOT NULL,
- likes TEXT NOT NULL,
- comments TEXT NOT NULL,
date INTEGER NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(user_id)
);
@@ -28,25 +27,20 @@ fn post_from_row(row: &Row) -> Result<Post, rusqlite::Error> {
let post_id = row.get(0)?;
let user_id = row.get(1)?;
let content = row.get(2)?;
- let likes_json: String = row.get(3)?;
- let comments_json: String = row.get(4)?;
- let date = row.get(5)?;
-
- let Ok(likes) = serde_json::from_str(&likes_json) else {
- return Err(rusqlite::Error::InvalidQuery)
- };
+ let date = row.get(3)?;
- let Ok(comments) = serde_json::from_str(&comments_json) else {
- return Err(rusqlite::Error::InvalidQuery)
- };
+ let comments = comments::get_comments_page(0, post_id).unwrap_or_else(|_| Vec::new());
+ let likes = likes::get_like_count(post_id).unwrap_or(None).unwrap_or(0);
+ let liked = likes::get_liked(user_id, post_id).unwrap_or(false);
Ok(Post {
post_id,
user_id,
content,
+ date,
likes,
+ liked,
comments,
- date,
})
}
@@ -106,14 +100,6 @@ pub fn get_users_post_page(user_id: u64, page: u64) -> Result<Vec<Post>, rusqlit
#[instrument()]
pub fn add_post(user_id: u64, content: &str) -> Result<Post, rusqlite::Error> {
tracing::trace!("Adding post");
- let likes: HashSet<u64> = HashSet::new();
- let comments: Vec<(u64, String)> = Vec::new();
- let Ok(likes_json) = serde_json::to_string(&likes) else {
- return Err(rusqlite::Error::InvalidQuery)
- };
- let Ok(comments_json) = serde_json::to_string(&comments) else {
- return Err(rusqlite::Error::InvalidQuery)
- };
let date = u64::try_from(
SystemTime::now()
.duration_since(UNIX_EPOCH)
@@ -122,29 +108,11 @@ pub fn add_post(user_id: u64, content: &str) -> Result<Post, rusqlite::Error> {
)
.unwrap_or(0);
let conn = database::connect()?;
- let mut stmt = conn.prepare("INSERT INTO posts (user_id, content, likes, comments, date) VALUES(?,?,?,?,?) RETURNING *;")?;
- let post = stmt.query_row((user_id, content, likes_json, comments_json, date), |row| {
+ let mut stmt =
+ conn.prepare("INSERT INTO posts (user_id, content, date) VALUES(?,?,?) RETURNING *;")?;
+ let post = stmt.query_row((user_id, content, date), |row| {
let row = post_from_row(row)?;
Ok(row)
})?;
Ok(post)
}
-
-#[instrument()]
-pub fn update_post(
- post_id: u64,
- likes: &HashSet<u64>,
- comments: &Vec<(u64, String)>,
-) -> Result<(), rusqlite::Error> {
- tracing::trace!("Updating post");
- let Ok(likes_json) = serde_json::to_string(&likes) else {
- return Err(rusqlite::Error::InvalidQuery)
- };
- let Ok(comments_json) = serde_json::to_string(&comments) else {
- return Err(rusqlite::Error::InvalidQuery)
- };
- let conn = database::connect()?;
- let sql = "UPDATE posts SET likes = ?, comments = ? WHERE post_id = ?";
- conn.execute(sql, (likes_json, comments_json, post_id))?;
- Ok(())
-}