diff --git a/README.md b/README.md index 7e7b98a..a0fc156 100644 --- a/README.md +++ b/README.md @@ -21,5 +21,4 @@ CREATE EXTENSION pg_trgm; ``` $ diesel run -$ python3 tools/migdb.py ``` diff --git a/migrations/postgres/2022-03-21-155550_create_posts/up.sql b/migrations/postgres/2022-03-21-155550_create_posts/up.sql index d7d0eba..66c128b 100644 --- a/migrations/postgres/2022-03-21-155550_create_posts/up.sql +++ b/migrations/postgres/2022-03-21-155550_create_posts/up.sql @@ -8,8 +8,8 @@ CREATE TABLE posts ( is_tmp BOOLEAN NOT NULL DEFAULT FALSE, n_attentions INTEGER NOT NULL DEFAULT 0, n_comments INTEGER NOT NULL DEFAULT 0, - create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - last_comment_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + create_time TIMESTAMPTZ NOT NULL DEFAULT now(), + last_comment_time TIMESTAMPTZ NOT NULL DEFAULT now(), is_deleted BOOLEAN NOT NULL DEFAULT FALSE, is_reported BOOLEAN NOT NULL DEFAULT FALSE, hot_score INTEGER NOT NULL DEFAULT 0, diff --git a/migrations/postgres/2022-03-21-164718_create_comments/up.sql b/migrations/postgres/2022-03-21-164718_create_comments/up.sql index 5b1558a..5bc8973 100644 --- a/migrations/postgres/2022-03-21-164718_create_comments/up.sql +++ b/migrations/postgres/2022-03-21-164718_create_comments/up.sql @@ -5,7 +5,7 @@ CREATE TABLE comments ( author_title VARCHAR NOT NULL DEFAULT '', is_tmp BOOLEAN NOT NULL DEFAULT FALSE, content TEXT NOT NULL, - create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + create_time TIMESTAMPTZ NOT NULL DEFAULT now(), is_deleted BOOLEAN NOT NULL DEFAULT FALSE, allow_search BOOLEAN NOT NULL DEFAULT FALSE, post_id INTEGER NOT NULL REFERENCES posts(id) diff --git a/src/api/comment.rs b/src/api/comment.rs index b1d372e..da7f9ed 100644 --- a/src/api/comment.rs +++ b/src/api/comment.rs @@ -3,7 +3,7 @@ use crate::db_conn::Db; use crate::models::*; use crate::rds_conn::RdsConn; use crate::rds_models::*; -use chrono::NaiveDateTime; +use chrono::{offset::Utc, DateTime}; use rocket::form::Form; use rocket::serde::{ json::{json, Value}, @@ -27,7 +27,7 @@ pub struct CommentOutput { can_del: bool, name_id: i32, is_tmp: bool, - create_time: NaiveDateTime, + create_time: DateTime, // for old version frontend timestamp: i64, } diff --git a/src/api/post.rs b/src/api/post.rs index 3f6b4f0..175f0e9 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -4,7 +4,7 @@ use crate::db_conn::Db; use crate::models::*; use crate::rds_conn::RdsConn; use crate::rds_models::*; -use chrono::NaiveDateTime; +use chrono::{offset::Utc, DateTime}; use rocket::form::Form; use rocket::futures::future; use rocket::serde::{json::json, Serialize}; @@ -29,8 +29,8 @@ pub struct PostOutput { is_tmp: bool, n_attentions: i32, n_comments: i32, - create_time: NaiveDateTime, - last_comment_time: NaiveDateTime, + create_time: DateTime, + last_comment_time: DateTime, allow_search: bool, is_reported: Option, comments: Option>, diff --git a/src/models.rs b/src/models.rs index 54722cc..0e0d4e4 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,6 +1,5 @@ #![allow(clippy::all)] -use chrono::NaiveDateTime; use diesel::{insert_into, ExpressionMethods, QueryDsl, QueryResult, RunQueryDsl}; use crate::db_conn::Db; @@ -21,8 +20,13 @@ macro_rules! get_multi { ($table:ident) => { pub async fn get_multi(db: &Db, ids: Vec) -> QueryResult> { // can use eq(any()) for postgres - db.run(move |c| $table::table.filter($table::id.eq_any(ids)).load(c)) - .await + db.run(move |c| { + $table::table + .filter($table::id.eq_any(ids)) + .order($table::id.desc()) + .load(c) + }) + .await } }; } @@ -41,7 +45,21 @@ macro_rules! set_deleted { }; } -#[derive(Queryable, Identifiable)] +use chrono::{offset::Utc, DateTime}; +#[derive(Queryable, Insertable)] +pub struct Comment { + pub id: i32, + pub author_hash: String, + pub author_title: String, + pub is_tmp: bool, + pub content: String, + pub create_time: DateTime, + pub is_deleted: bool, + pub allow_search: bool, + pub post_id: i32, +} + +#[derive(Queryable, Insertable)] pub struct Post { pub id: i32, pub author_hash: String, @@ -51,14 +69,22 @@ pub struct Post { pub is_tmp: bool, pub n_attentions: i32, pub n_comments: i32, - pub create_time: NaiveDateTime, - pub last_comment_time: NaiveDateTime, + pub create_time: DateTime, + pub last_comment_time: DateTime, pub is_deleted: bool, pub is_reported: bool, pub hot_score: i32, pub allow_search: bool, } +#[derive(Queryable, Insertable)] +pub struct User { + pub id: i32, + pub name: String, + pub token: String, + pub is_admin: bool, +} + #[derive(Insertable)] #[table_name = "posts"] pub struct NewPost { @@ -144,14 +170,6 @@ impl Post { } } -#[derive(Queryable, Identifiable)] -pub struct User { - pub id: i32, - pub name: String, - pub token: String, - pub is_admin: bool, -} - impl User { pub async fn get_by_token(db: &Db, token: &str) -> Option { let token = token.to_string(); @@ -161,19 +179,6 @@ impl User { } } -#[derive(Queryable, Identifiable)] -pub struct Comment { - pub id: i32, - pub author_hash: String, - pub author_title: String, - pub is_tmp: bool, - pub content: String, - pub create_time: NaiveDateTime, - pub is_deleted: bool, - pub allow_search: bool, - pub post_id: i32, -} - #[derive(Insertable)] #[table_name = "comments"] pub struct NewComment { @@ -200,7 +205,12 @@ impl Comment { pub async fn gets_by_post_id(db: &Db, post_id: i32) -> QueryResult> { let pid = post_id; - db.run(move |c| comments::table.filter(comments::post_id.eq(pid)).load(c)) - .await + db.run(move |c| { + comments::table + .filter(comments::post_id.eq(pid)) + .order(comments::id) + .load(c) + }) + .await } } diff --git a/src/schema.rs b/src/schema.rs index 0fe625d..db6da45 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -5,7 +5,7 @@ table! { author_title -> Varchar, is_tmp -> Bool, content -> Text, - create_time -> Timestamp, + create_time -> Timestamptz, is_deleted -> Bool, allow_search -> Bool, post_id -> Int4, @@ -22,8 +22,8 @@ table! { is_tmp -> Bool, n_attentions -> Int4, n_comments -> Int4, - create_time -> Timestamp, - last_comment_time -> Timestamp, + create_time -> Timestamptz, + last_comment_time -> Timestamptz, is_deleted -> Bool, is_reported -> Bool, hot_score -> Int4,