feat: added date to golden book
This commit is contained in:
parent
af3dfba510
commit
cf93579d97
4 changed files with 17 additions and 2 deletions
|
|
@ -2,5 +2,6 @@
|
||||||
CREATE TABLE comments (
|
CREATE TABLE comments (
|
||||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
author TEXT NOT NULL,
|
author TEXT NOT NULL,
|
||||||
content TEXT NOT NULL
|
content TEXT NOT NULL,
|
||||||
|
date_posted BigInt NOT NULL
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
use crate::models::{Comment, CreateCommentRequest, NewComment};
|
use crate::models::{Comment, CreateCommentRequest, NewComment};
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
@ -23,7 +25,16 @@ pub fn create_comment(
|
||||||
) -> Result<Comment, diesel::result::Error> {
|
) -> Result<Comment, diesel::result::Error> {
|
||||||
use crate::schema::comments;
|
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)
|
diesel::insert_into(comments::table)
|
||||||
.values(&new_comment)
|
.values(&new_comment)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ pub struct Comment {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub author: String,
|
pub author: String,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
|
pub date_posted: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable, Serialize, Deserialize)]
|
#[derive(Insertable, Serialize, Deserialize)]
|
||||||
|
|
@ -16,6 +17,7 @@ pub struct Comment {
|
||||||
pub struct NewComment<'a> {
|
pub struct NewComment<'a> {
|
||||||
pub author: &'a str,
|
pub author: &'a str,
|
||||||
pub content: &'a str,
|
pub content: &'a str,
|
||||||
|
pub date_posted: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,6 @@ diesel::table! {
|
||||||
id -> Integer,
|
id -> Integer,
|
||||||
author -> Text,
|
author -> Text,
|
||||||
content -> Text,
|
content -> Text,
|
||||||
|
date_posted -> BigInt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue