diff --git a/migrations/2025-09-23-083540-0000_create_comments/up.sql b/migrations/2025-09-23-083540-0000_create_comments/up.sql index d324707..a05bf6e 100644 --- a/migrations/2025-09-23-083540-0000_create_comments/up.sql +++ b/migrations/2025-09-23-083540-0000_create_comments/up.sql @@ -2,5 +2,6 @@ CREATE TABLE comments ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, author TEXT NOT NULL, - content TEXT NOT NULL + content TEXT NOT NULL, + date_posted BigInt NOT NULL ); diff --git a/src/comments.rs b/src/comments.rs index f4005fe..849bd5d 100644 --- a/src/comments.rs +++ b/src/comments.rs @@ -1,3 +1,5 @@ +use std::time::SystemTime; + use crate::models::{Comment, CreateCommentRequest, NewComment}; use axum::Json; use diesel::prelude::*; @@ -23,7 +25,16 @@ pub fn create_comment( ) -> Result { use crate::schema::comments; - let new_comment = NewComment { author, content }; + let date_posted: i64 = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { + Ok(n) => n.as_secs().try_into().unwrap(), + Err(_) => 0, + }; + + let new_comment = NewComment { + author, + content, + date_posted, + }; diesel::insert_into(comments::table) .values(&new_comment) diff --git a/src/models.rs b/src/models.rs index 53f653a..736cf0b 100644 --- a/src/models.rs +++ b/src/models.rs @@ -9,6 +9,7 @@ pub struct Comment { pub id: i32, pub author: String, pub content: String, + pub date_posted: i64, } #[derive(Insertable, Serialize, Deserialize)] @@ -16,6 +17,7 @@ pub struct Comment { pub struct NewComment<'a> { pub author: &'a str, pub content: &'a str, + pub date_posted: i64, } #[derive(Deserialize)] diff --git a/src/schema.rs b/src/schema.rs index d4a79a6..0d9389f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -5,5 +5,6 @@ diesel::table! { id -> Integer, author -> Text, content -> Text, + date_posted -> BigInt, } }