feat: added date to golden book

This commit is contained in:
Alzalia 2025-09-26 21:29:10 +02:00
parent af3dfba510
commit cf93579d97
4 changed files with 17 additions and 2 deletions

View file

@ -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
);

View file

@ -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<Comment, diesel::result::Error> {
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)

View file

@ -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)]

View file

@ -5,5 +5,6 @@ diesel::table! {
id -> Integer,
author -> Text,
content -> Text,
date_posted -> BigInt,
}
}