diff --git a/.gitignore b/.gitignore index e1a8a55..5e75a0c 100644 --- a/.gitignore +++ b/.gitignore @@ -25,5 +25,3 @@ dist-ssr *.env *.db* - -*backup* diff --git a/server/migrations/0003_comments_table.sql b/server/migrations/0003_comments_table.sql index 5eb8049..bbb261a 100644 --- a/server/migrations/0003_comments_table.sql +++ b/server/migrations/0003_comments_table.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS comments ( - id SERIAL PRIMARY KEY NOT NULL, + id BIGSERIAL PRIMARY KEY, parent_post_id BIGINT NOT NULL, - -- parent_comment_id BIGINT, + parent_comment_id BIGINT, author_user_id BIGINT NOT NULL, content TEXT NOT NULL, upvotes INTEGER NOT NULL DEFAULT 0, @@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS comments ( created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (parent_post_id) REFERENCES posts (id), - -- FOREIGN KEY (parent_comment_id) REFERENCES comments (id), + FOREIGN KEY (parent_comment_id) REFERENCES comments (id), FOREIGN KEY (author_user_id) REFERENCES users (id) ); @@ -43,5 +43,5 @@ FOR EACH ROW EXECUTE FUNCTION comments_set_updated_at(); CREATE INDEX comments_parent_post_id_index ON comments (parent_post_id); --- CREATE INDEX comments_parent_comment_id_index ON comments (parent_comment_id); +CREATE INDEX comments_parent_comment_id_index ON comments (parent_comment_id); CREATE INDEX comments_user_id_index ON comments (author_user_id); \ No newline at end of file diff --git a/server/migrations/0004_procedures.sql b/server/migrations/0004_procedures.sql deleted file mode 100644 index 0708073..0000000 --- a/server/migrations/0004_procedures.sql +++ /dev/null @@ -1,62 +0,0 @@ --- Description: This file creates the procedures and functions for adding users, posts, and comments. --- Functions are commonly used for SELECT queries, while procedures are used for INSERT, UPDATE, and DELETE queries. --- None of these seem to play very nice with sqlx for now, but they will surely be useful in the future. - --- Procedure for adding a user -CREATE OR REPLACE PROCEDURE add_user( - IN username_param TEXT, - IN password_param TEXT -) -LANGUAGE plpgsql -AS $$ -BEGIN - INSERT INTO users (username, password) - VALUES (username_param, password_param); -END; -$$; - --- Procedure for adding a post -CREATE OR REPLACE PROCEDURE add_post( - IN user_id_param BIGINT, - IN content_param TEXT -) -LANGUAGE plpgsql -AS $$ -BEGIN - INSERT INTO posts (user_id, content) - VALUES (user_id_param, content_param); -END; -$$; - --- Procedure for adding a comment -CREATE OR REPLACE PROCEDURE add_comment( - IN parent_post_id_param BIGINT, - -- IN parent_comment_id_param BIGINT, - IN author_user_id_param BIGINT, - IN content_param TEXT -) -LANGUAGE plpgsql -AS $$ -BEGIN - INSERT INTO comments (parent_post_id, author_user_id, content) - VALUES (parent_post_id_param, author_user_id_param, content_param); -END; -$$; - --- Function for getting comments -CREATE OR REPLACE FUNCTION get_comments( - IN parent_post_id_param BIGINT, - IN limit_param BIGINT, - IN offset_param BIGINT -) -RETURNS SETOF comments AS $$ -BEGIN - RETURN QUERY - SELECT * - FROM comments - WHERE parent_post_id = parent_post_id_param - ORDER BY created_at DESC - LIMIT limit_param - OFFSET offset_param; -END; -$$ LANGUAGE plpgsql; diff --git a/server/src/db.rs b/server/src/db.rs index 1c83492..d9d49e4 100644 --- a/server/src/db.rs +++ b/server/src/db.rs @@ -9,13 +9,14 @@ use sqlx::PgPool; pub async fn db_new_comment( pool: &PgPool, parent_post_id: i64, - // parent_comment_id: Option, + parent_comment_id: Option, user_id: i64, content: &str, ) -> bool { let insert_query = sqlx::query!( - "INSERT INTO comments (parent_post_id, author_user_id, content) VALUES ($1, $2, $3)", + "INSERT INTO comments (parent_post_id, parent_comment_id, author_user_id, content) VALUES ($1, $2, $3, $4)", parent_post_id, + parent_comment_id, user_id, content ) @@ -39,8 +40,7 @@ pub async fn db_get_comments( ) -> Vec { sqlx::query_as!( PublicComment, - "SELECT id, parent_post_id, upvotes, downvotes, content, created_at, updated_at - FROM comments WHERE parent_post_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3", + "SELECT id, parent_post_id, parent_comment_id, upvotes, downvotes, content, created_at, updated_at FROM comments WHERE parent_post_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3", parent_post_id, limit, offset diff --git a/server/src/main.rs b/server/src/main.rs index 96aa8fe..d3dbde2 100755 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -20,11 +20,7 @@ use util::hex_string; #[actix_web::main] async fn main() -> std::io::Result<()> { - let mut builder = env_logger::Builder::new(); - builder - .filter(None, log::LevelFilter::Debug) - .filter_module("sqlx", log::LevelFilter::Warn) - .init(); + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init(); let data = ServerState::new().await; let capt_db = CaptchaState::new(); @@ -53,7 +49,7 @@ async fn main() -> std::io::Result<()> { App::new() .wrap(cors) .wrap(middleware::Compress::default()) - .wrap(middleware::Logger::new("%s %r")) + .wrap(middleware::Logger::default()) .wrap(middleware::NormalizePath::trim()) .service( scope("/api") diff --git a/server/src/routes/comment.rs b/server/src/routes/comment.rs index acd8cf7..e7098f1 100644 --- a/server/src/routes/comment.rs +++ b/server/src/routes/comment.rs @@ -24,11 +24,6 @@ pub async fn get_comments( let comments = db_get_comments(&state.pool, post_id, limit, offset).await; - if comments.is_empty() { - info!("No comments found for post {}", post_id); - return Ok(HttpResponse::NotFound().json("No comments found")); - } - Ok(HttpResponse::Ok().json(comments)) } @@ -65,7 +60,14 @@ pub async fn new_comment( info!("Creating a new comment {:?}", &data); - let success = db_new_comment(&state.pool, data.parent_post_id, userid, &content).await; + let success = db_new_comment( + &state.pool, + data.parent_post_id, + data.parent_comment_id, + userid, + &content, + ) + .await; match success { true => { diff --git a/server/src/state.rs b/server/src/state.rs index c4748c2..1a363db 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -105,19 +105,6 @@ async fn lipsum_setup(pool: &PgPool) -> Result<(), sqlx::Error> { .execute(pool) .await?; } - - // Insert a bunch of comments - for i in 1..101 { - for _ in 0..rng.gen_range(3..30) { - query!( - "INSERT INTO comments (author_user_id, parent_post_id, content) VALUES (1, $1, $2)", - i, - lipsum(rng.gen_range(10..100)) - ) - .execute(pool) - .await?; - } - } } Ok(()) diff --git a/server/src/types/comment.rs b/server/src/types/comment.rs index 70fe32a..0fcdc7c 100644 --- a/server/src/types/comment.rs +++ b/server/src/types/comment.rs @@ -16,8 +16,8 @@ pub struct Comment { pub parent_post_id: i64, pub parent_comment_id: Option, pub author_user_id: i64, - pub upvotes: i32, - pub downvotes: i32, + pub upvotes: i64, + pub downvotes: i64, pub content: String, pub created_at: chrono::NaiveDateTime, pub updated_at: chrono::NaiveDateTime, @@ -28,8 +28,9 @@ pub struct Comment { pub struct PublicComment { pub id: i64, pub parent_post_id: i64, - pub upvotes: i32, - pub downvotes: i32, + pub parent_comment_id: Option, + pub upvotes: i64, + pub downvotes: i64, pub content: String, pub created_at: chrono::NaiveDateTime, pub updated_at: chrono::NaiveDateTime,