diff --git a/migrations/2024-09-06-100207_add_created_updated_at_to_projects/down.sql b/migrations/2024-09-06-100207_add_created_updated_at_to_projects/down.sql new file mode 100644 index 0000000..59ef2ff --- /dev/null +++ b/migrations/2024-09-06-100207_add_created_updated_at_to_projects/down.sql @@ -0,0 +1,5 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE "projects" DROP COLUMN "created_at"; +ALTER TABLE "projects" DROP COLUMN "updated_at"; + + diff --git a/migrations/2024-09-06-100207_add_created_updated_at_to_projects/up.sql b/migrations/2024-09-06-100207_add_created_updated_at_to_projects/up.sql new file mode 100644 index 0000000..840a988 --- /dev/null +++ b/migrations/2024-09-06-100207_add_created_updated_at_to_projects/up.sql @@ -0,0 +1,7 @@ +-- Your SQL goes here +ALTER TABLE "projects" ADD COLUMN "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; +ALTER TABLE "projects" ADD COLUMN "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; + +SELECT diesel_manage_updated_at('projects'); + + diff --git a/src/models/project.rs b/src/models/project.rs index 44898e6..6e700d3 100644 --- a/src/models/project.rs +++ b/src/models/project.rs @@ -1,3 +1,4 @@ +use chrono::NaiveDateTime; use crate::schema::projects; use diesel::prelude::*; use serde::{Deserialize, Serialize}; @@ -12,6 +13,8 @@ const TITLE_LENGTH_MAX: u64 = 255; pub struct Project { id: i32, title: String, + created_at: NaiveDateTime, + updated_at: NaiveDateTime, } impl Project { @@ -22,6 +25,14 @@ impl Project { pub fn title(&self) -> &str { &self.title } + + pub fn created_at(&self) -> NaiveDateTime { + self.created_at + } + + pub fn updated_at(&self) -> NaiveDateTime { + self.updated_at + } } #[derive(Insertable, Serialize, Deserialize, Validate, Clone, Debug)] diff --git a/src/schema/mod.rs b/src/schema/mod.rs index a87d07d..66a8cba 100644 --- a/src/schema/mod.rs +++ b/src/schema/mod.rs @@ -4,6 +4,8 @@ diesel::table! { projects (id) { id -> Int4, title -> Text, + created_at -> Timestamp, + updated_at -> Timestamp, } }