From 7beeac2ac46fa802c0b182cd68358ccb52328146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Volf?= <66163112+matous-volf@users.noreply.github.com> Date: Fri, 6 Sep 2024 17:56:01 +0200 Subject: [PATCH] feat: add times of creation and update to the task model --- .../down.sql | 5 +++++ .../up.sql | 7 +++++++ src/models/task.rs | 11 +++++++++++ src/schema/mod.rs | 2 ++ 4 files changed, 25 insertions(+) create mode 100644 migrations/2024-09-06-155031_add_created_updated_at_to_tasks/down.sql create mode 100644 migrations/2024-09-06-155031_add_created_updated_at_to_tasks/up.sql diff --git a/migrations/2024-09-06-155031_add_created_updated_at_to_tasks/down.sql b/migrations/2024-09-06-155031_add_created_updated_at_to_tasks/down.sql new file mode 100644 index 0000000..f7bdc9f --- /dev/null +++ b/migrations/2024-09-06-155031_add_created_updated_at_to_tasks/down.sql @@ -0,0 +1,5 @@ +-- This file should undo anything in `up.sql` + +ALTER TABLE "tasks" DROP COLUMN "created_at"; +ALTER TABLE "tasks" DROP COLUMN "updated_at"; + diff --git a/migrations/2024-09-06-155031_add_created_updated_at_to_tasks/up.sql b/migrations/2024-09-06-155031_add_created_updated_at_to_tasks/up.sql new file mode 100644 index 0000000..434c941 --- /dev/null +++ b/migrations/2024-09-06-155031_add_created_updated_at_to_tasks/up.sql @@ -0,0 +1,7 @@ +-- Your SQL goes here + +ALTER TABLE "tasks" ADD COLUMN "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; +ALTER TABLE "tasks" ADD COLUMN "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; + +SELECT diesel_manage_updated_at('tasks'); + diff --git a/src/models/task.rs b/src/models/task.rs index 4799ff2..4aeb34d 100644 --- a/src/models/task.rs +++ b/src/models/task.rs @@ -1,3 +1,4 @@ +use chrono::NaiveDateTime; use crate::models::category::Category; use crate::schema::tasks; use diesel::prelude::*; @@ -16,6 +17,8 @@ pub struct Task { deadline: Option, category: Category, project_id: Option, + created_at: NaiveDateTime, + updated_at: NaiveDateTime, } impl Task { @@ -38,6 +41,14 @@ impl Task { pub fn project_id(&self) -> Option { self.project_id } + + 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 66a8cba..7078264 100644 --- a/src/schema/mod.rs +++ b/src/schema/mod.rs @@ -16,6 +16,8 @@ diesel::table! { deadline -> Nullable, category -> Jsonb, project_id -> Nullable, + created_at -> Timestamp, + updated_at -> Timestamp, } }