diff --git a/Cargo.lock b/Cargo.lock index 7984140..3c9b422 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1812,6 +1812,7 @@ dependencies = [ "parking_lot", "rayon", "rstest", + "sea-orm", "serde", "serde_derive", "serde_json", diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 41a05c4..b09794d 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -39,6 +39,7 @@ pace_error = { workspace = true } pace_time = { workspace = true } parking_lot = { workspace = true, features = ["deadlock_detection"] } rayon = { workspace = true } +sea-orm = { workspace = true } serde = { workspace = true } serde_derive = { workspace = true } serde_json = { workspace = true } diff --git a/crates/core/src/domain/activity.rs b/crates/core/src/domain/activity.rs index a499718..6919dc9 100644 --- a/crates/core/src/domain/activity.rs +++ b/crates/core/src/domain/activity.rs @@ -8,7 +8,7 @@ use pace_time::{ date_time::PaceDateTime, duration::{calculate_duration, duration_to_str, PaceDuration}, }; -use strum::EnumIter; +use sea_orm::DeriveActiveEnum; use serde_derive::{Deserialize, Serialize}; use std::fmt::Display; @@ -84,28 +84,35 @@ impl From<(ActivityGuid, Activity)> for ActivityItem { PartialOrd, Ord, EnumString, - EnumIter, + sea_orm::EnumIter, strum::Display, + DeriveActiveEnum, )] #[serde(rename_all = "kebab-case")] #[strum(serialize_all = "kebab-case")] +#[sea_orm(rs_type = "i32", db_type = "Integer")] // #[serde(untagged)] pub enum ActivityKind { /// A generic activity #[default] + #[sea_orm(num_value = 0)] Activity, - /// A task - Task, - /// A break + #[sea_orm(num_value = 1)] Intermission, + /// A pomodoro break + #[sea_orm(num_value = 2)] + PomodoroIntermission, + /// A pomodoro work session + #[sea_orm(num_value = 3)] PomodoroWork, - /// A pomodoro break - PomodoroIntermission, + /// A task + #[sea_orm(num_value = 4)] + Task, } #[allow(clippy::trivially_copy_pass_by_ref)] diff --git a/crates/core/src/domain/status.rs b/crates/core/src/domain/status.rs index 7afe2e4..f452eb3 100644 --- a/crates/core/src/domain/status.rs +++ b/crates/core/src/domain/status.rs @@ -1,5 +1,6 @@ +use sea_orm::DeriveActiveEnum; use serde_derive::{Deserialize, Serialize}; -use strum::{EnumIter, EnumString}; +use strum::EnumString; #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[serde(rename_all = "kebab-case")] @@ -29,37 +30,47 @@ pub enum TaskStatus { PartialOrd, Ord, EnumString, - EnumIter, + sea_orm::EnumIter, strum::Display, + DeriveActiveEnum, )] #[serde(rename_all = "kebab-case")] #[strum(serialize_all = "kebab-case")] +#[sea_orm(rs_type = "i32", db_type = "Integer")] pub enum ActivityStatusKind { /// The initial state of an activity once it's created in the system but not yet started. #[default] + #[sea_orm(num_value = 0)] Created, /// The activity is scheduled to start at a specific time. /// It remains in this state until the activity begins. + #[sea_orm(num_value = 1)] Scheduled, /// The active state of an activity. It transitions to this state from "Scheduled" when /// the activity begins or from "Paused" when it's resumed. The start time is recorded /// upon entering this state for the first time, and the resume time is noted for /// subsequent entries. + #[sea_orm(num_value = 2)] InProgress, /// Represents an activity that has been temporarily halted. /// This could apply to tasks being paused for a break or intermission. /// The activity can move back to "InProgress" when work on it resumes. + #[sea_orm(num_value = 3)] Paused, /// The final state of an activity, indicating it has been finished. /// The end time of the activity is recorded, marking its completion. + #[sea_orm(num_value = 4)] Completed, + #[sea_orm(num_value = 98)] + Unarchived, + + #[sea_orm(num_value = 99)] Archived, - Unarchived, // TODO: Do we need this or can be unarchiving done without it? } #[allow(clippy::trivially_copy_pass_by_ref)] diff --git a/crates/storage/src/entities.rs b/crates/storage/src/entities.rs deleted file mode 100644 index 85d9e22..0000000 --- a/crates/storage/src/entities.rs +++ /dev/null @@ -1,172 +0,0 @@ -use pace_core::prelude::{ - Activity, ActivityEndOptions, ActivityGuid, ActivityKind, ActivityStatusKind, -}; -use typed_builder::TypedBuilder; -use ulid::Ulid; - -use crate::entities::activities::Activities as SQLiteActivity; - -pub mod activities; -pub mod activities_categories; -pub mod activities_tags; -pub mod activity_kinds; -pub mod activity_status; -pub mod categories; -pub mod description; -pub mod schema_migrations; -pub mod tags; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder)] -pub struct SQLiteActivityItem { - guid: Ulid, - activity: SQLiteActivity, -} - -impl SQLiteActivityItem { - #[must_use] - pub const fn new(guid: Ulid, activity: SQLiteActivity) -> Self { - Self { guid, activity } - } -} - -// impl SqliteActivity { -// pub fn to_sql_prepare_statement(&self) -> &'static str { -// "INSERT INTO activities (id, category, description, begin, end, duration, kind, status, tags, parent_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)" -// } - -// pub fn to_sql_execute_statement(&self) -> PaceResult<(ActivityGuid, Vec<&dyn ToSql>)> { -// let category = if let Some(category) = self.category() { -// category.to_sql()? -// } else { -// "NULL".to_sql()? -// }; - -// let (end, duration) = if let Some(end_opts) = self.activity_end_options().as_ref() { -// let (end, duration) = end_opts.as_tuple(); -// (end.to_sql()?, duration.to_sql()?) -// } else { -// ("NULL".to_sql()?, "NULL".to_sql()?) -// }; - -// let parent_id = if let Some(parent_id) = self.parent_id() { -// parent_id.to_sql()? -// } else { -// "NULL".to_sql()? -// }; - -// let tags = if let Some(tags) = self.tags() { -// let tags = tags -// .iter() -// .map(|tag| tag.to_string()) -// .collect::>(); - -// tags.join(",").to_sql()? -// } else { -// "NULL".to_sql()? -// }; - -// let guid = ActivityGuid::new(); - -// Ok(( -// guid, -// vec![ -// // TODO: We create a new ID here, that should probably happen -// // TODO: somewhere else and needs a refactoring -// &guid, -// &category, -// &self.description(), -// &self.begin(), -// &end, -// &duration, -// &self.kind(), -// &self.status(), -// &tags, -// &parent_id, -// ], -// )) -// } -// } - -// impl SqliteActivityFilterKind { -// pub fn to_sql_statement(&self) -> &'static str { -// match self { -// Self::Everything => "SELECT * FROM activities", -// ActivityFilterKind::OnlyActivities => todo!(), -// ActivityFilterKind::Active => "SELECT * FROM activities WHERE status = 'in-progress'", -// ActivityFilterKind::ActiveIntermission => todo!(), -// ActivityFilterKind::Archived => "SELECT * FROM activities WHERE status = 'archived'", -// ActivityFilterKind::Ended => "SELECT * FROM activities WHERE status = 'completed'", -// ActivityFilterKind::Held => "SELECT * FROM activities WHERE status = 'paused'", -// ActivityFilterKind::Intermission => todo!(), -// ActivityFilterKind::TimeRange(opts) => todo!(), -// } -// } -// } - -// impl TryFrom> for SqliteActivityEndOptions { -// type Error = rusqlite::Error; - -// fn try_from(row: &Row<'_>) -> Result { -// Ok(Self::new(row.get("end")?, row.get("duration")?)) -// } -// } - -// impl TryFrom> for SqliteActivityKindOptions { -// type Error = rusqlite::Error; - -// fn try_from(row: &Row<'_>) -> Result { -// Ok(Self::with_parent_id(row.get("parent_id")?)) -// } -// } - -// impl TryFrom> for SqliteActivity { -// type Error = rusqlite::Error; - -// fn try_from(row: &Row<'_>) -> Result { -// let begin_time: PaceDateTime = row.get("begin")?; - -// let description: String = row.get("description")?; - -// let tags_string: String = row.get("tags")?; - -// let tags = tags_string -// .split(',') -// .map(|tag| tag.to_string()) -// .collect::>(); - -// Ok(Activity::builder() -// .category(Some(row.get("category")?)) // TODO: Check for None -// .description(description) -// .begin(begin_time) -// .activity_end_options(Some(ActivityEndOptions::from_row(row)?)) // TODO: Check for None -// .kind(row.get("kind")?) -// .activity_kind_options(Some(ActivityKindOptions::from_row(row)?)) // TODO: Check for None -// .tags(tags) -// .status(row.get("status")?) -// .build()) -// } -// } - -// impl TryFrom> for SqliteActivityGuid { -// type Error = rusqlite::Error; - -// fn try_from(row: &Row<'_>) -> Result { -// Ok(row.get("guid")?) -// } -// } - -// impl TryFrom> for SqliteActivityItem { -// type Error = rusqlite::Error; - -// fn try_from(row: &Row<'_>) -> Result { -// let activity_end_opts = ActivityEndOptions::from_row(row)?; - -// let activity_kind_opts = ActivityKindOptions::from_row(row)?; - -// let activity = Activity::from_row(row)?; - -// let guid = ActivityGuid::from_row(row)?; - -// Ok(Self::builder().guid(guid).activity(activity).build()) -// } -// } diff --git a/crates/storage/src/entities/activities.rs b/crates/storage/src/entities/activities.rs deleted file mode 100644 index 3414662..0000000 --- a/crates/storage/src/entities/activities.rs +++ /dev/null @@ -1,61 +0,0 @@ -use chrono::FixedOffset; -use getset::Getters; -use pace_core::prelude::{ - ActivityGuid, ActivityItem, ActivityKindGuid, ActivityStatusGuid, DescriptionGuid, -}; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct Activities { - pub guid: ActivityGuid, - pub description_guid: DescriptionGuid, - pub begin: chrono::DateTime, - pub end: Option>, - pub kind_guid: ActivityKindGuid, - pub duration: Option, - pub status_guid: ActivityStatusGuid, - pub parent_guid: Option, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - SelfRef, - ActivitiesCategories, - ActivitiesTags, - ActivityKinds, - ActivityStatus, - Descriptions, -} - -impl SQLiteEntity for Activities { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for Activities { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - description_guid: row.get("description")?, - begin: row.get("begin")?, - end: row.get("end")?, - duration: row.get("duration")?, - kind_guid: row.get("kind")?, - status_guid: row.get("status")?, - parent_guid: row.get("parent_guid")?, - }) - } -} diff --git a/crates/storage/src/entities/activities_categories.rs b/crates/storage/src/entities/activities_categories.rs deleted file mode 100644 index a01d6ce..0000000 --- a/crates/storage/src/entities/activities_categories.rs +++ /dev/null @@ -1,44 +0,0 @@ -use getset::Getters; -use pace_core::prelude::Guid; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct ActivitiesCategories { - pub guid: Guid, - pub category_guid: Guid, - pub activity_guid: Guid, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Activities, - Categories, -} - -impl SQLiteEntity for ActivitiesCategories { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for ActivitiesCategories { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - category_guid: row.get("category_guid")?, - activity_guid: row.get("activity_guid")?, - }) - } -} diff --git a/crates/storage/src/entities/activities_tags.rs b/crates/storage/src/entities/activities_tags.rs deleted file mode 100644 index 2421153..0000000 --- a/crates/storage/src/entities/activities_tags.rs +++ /dev/null @@ -1,44 +0,0 @@ -use getset::Getters; -use pace_core::prelude::Guid; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct ActivitiesTags { - pub guid: Guid, - pub tag_guid: Guid, - pub activity_guid: Guid, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Activities, - Tags, -} - -impl SQLiteEntity for ActivitiesTags { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for ActivitiesTags { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - tag_guid: row.get("tag_guid")?, - activity_guid: row.get("activity_guid")?, - }) - } -} diff --git a/crates/storage/src/entities/activity_kinds.rs b/crates/storage/src/entities/activity_kinds.rs deleted file mode 100644 index 8716655..0000000 --- a/crates/storage/src/entities/activity_kinds.rs +++ /dev/null @@ -1,41 +0,0 @@ -use getset::Getters; -use pace_core::prelude::ActivityKindGuid; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct ActivityKinds { - pub guid: ActivityKindGuid, - pub kind: String, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Activities, -} - -impl SQLiteEntity for ActivityKinds { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for ActivityKinds { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - kind: row.get("kind")?, - }) - } -} diff --git a/crates/storage/src/entities/activity_status.rs b/crates/storage/src/entities/activity_status.rs deleted file mode 100644 index bb412f8..0000000 --- a/crates/storage/src/entities/activity_status.rs +++ /dev/null @@ -1,41 +0,0 @@ -use getset::Getters; -use pace_core::prelude::ActivityStatusGuid; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct ActivityStatus { - pub guid: ActivityStatusGuid, - pub status: String, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Activities, -} - -impl SQLiteEntity for ActivityStatus { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for ActivityStatus { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - status: row.get("status")?, - }) - } -} diff --git a/crates/storage/src/entities/categories.rs b/crates/storage/src/entities/categories.rs deleted file mode 100644 index 5bd286b..0000000 --- a/crates/storage/src/entities/categories.rs +++ /dev/null @@ -1,43 +0,0 @@ -use getset::Getters; -use pace_core::prelude::CategoryGuid; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct Categories { - pub guid: CategoryGuid, - pub category: String, - pub description: Option, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - ActivitiesCategories, -} - -impl SQLiteEntity for Categories { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for Categories { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - category: row.get("category")?, - description: row.get("description")?, - }) - } -} diff --git a/crates/storage/src/entities/description.rs b/crates/storage/src/entities/description.rs deleted file mode 100644 index c112e08..0000000 --- a/crates/storage/src/entities/description.rs +++ /dev/null @@ -1,41 +0,0 @@ -use getset::Getters; -use pace_core::prelude::{DescriptionGuid, PaceDescription}; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct Descriptions { - pub guid: DescriptionGuid, - pub description: PaceDescription, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Activities, -} - -impl SQLiteEntity for Descriptions { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for Descriptions { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - description: row.get("description")?, - }) - } -} diff --git a/crates/storage/src/entities/schema_migrations.rs b/crates/storage/src/entities/schema_migrations.rs deleted file mode 100644 index a77a5df..0000000 --- a/crates/storage/src/entities/schema_migrations.rs +++ /dev/null @@ -1,35 +0,0 @@ -use getset::Getters; -use pace_core::prelude::Guid; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct SchemaMigrations { - pub guid: Guid, - pub version: i64, -} - -impl SQLiteEntity for SchemaMigrations { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for SchemaMigrations { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - version: row.get("version")?, - }) - } -} diff --git a/crates/storage/src/entities/tags.rs b/crates/storage/src/entities/tags.rs deleted file mode 100644 index 69b7b08..0000000 --- a/crates/storage/src/entities/tags.rs +++ /dev/null @@ -1,41 +0,0 @@ -use getset::Getters; -use pace_core::prelude::TagGuid; -use rusqlite::{Error, Row}; -use sea_query::enum_def; -use strum::EnumIter; -use typed_builder::TypedBuilder; - -use crate::storage::SQLiteEntity; - -#[derive(Clone, Debug, PartialEq, Eq, TypedBuilder, Getters)] -#[getset(get = "pub")] -#[enum_def] -pub struct Tags { - pub guid: TagGuid, - pub tag: String, -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - ActivitiesTags, -} - -impl SQLiteEntity for Tags { - fn from_row(row: &Row<'_>) -> Result - where - Self: Sized, - { - Self::try_from(row) - } -} - -impl TryFrom<&Row<'_>> for Tags { - type Error = Error; - - fn try_from(row: &Row<'_>) -> Result { - Ok(Self { - guid: row.get("guid")?, - tag: row.get("tag")?, - }) - } -} diff --git a/crates/storage/src/entity/mod.rs b/crates/storage/src/entity.rs similarity index 72% rename from crates/storage/src/entity/mod.rs rename to crates/storage/src/entity.rs index 9a23065..6542341 100644 --- a/crates/storage/src/entity/mod.rs +++ b/crates/storage/src/entity.rs @@ -5,8 +5,5 @@ pub mod prelude; pub mod activities; pub mod activities_categories; pub mod activities_tags; -pub mod activity_kinds; -pub mod activity_status; pub mod categories; -pub mod descriptions; pub mod tags; diff --git a/crates/storage/src/entity/activities.rs b/crates/storage/src/entity/activities.rs index 7d41e99..e1fad05 100644 --- a/crates/storage/src/entity/activities.rs +++ b/crates/storage/src/entity/activities.rs @@ -1,33 +1,40 @@ #![allow(unused_qualifications)] //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 -use chrono::{DateTime, FixedOffset}; +use chrono::{DateTime, FixedOffset, Utc}; +use pace_core::domain::{activity::ActivityKind, status::ActivityStatusKind}; use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "activities")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub guid: String, - pub description_guid: String, + pub description: String, pub begin: DateTime, pub end: Option>, pub duration: Option, - pub kind_guid: String, - pub status_guid: String, + pub kind: ActivityKind, + pub status: ActivityStatusKind, pub parent_guid: Option, + pub created_at: DateTime, + pub updated_at: DateTime, + pub deleted_at: DateTime, } #[derive(DeriveIden)] -pub enum ActivitiesEnum { +pub enum Activities { Table, Guid, - DescriptionGuid, + Description, Begin, End, Duration, - KindGuid, - StatusGuid, + Kind, + Status, ParentGuid, + CreatedAt, + UpdatedAt, + DeletedAt, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -44,30 +51,6 @@ pub enum Relation { ActivitiesCategories, #[sea_orm(has_many = "super::activities_tags::Entity")] ActivitiesTags, - #[sea_orm( - belongs_to = "super::activity_kinds::Entity", - from = "Column::KindGuid", - to = "super::activity_kinds::Column::Guid", - on_update = "NoAction", - on_delete = "NoAction" - )] - ActivityKinds, - #[sea_orm( - belongs_to = "super::activity_status::Entity", - from = "Column::StatusGuid", - to = "super::activity_status::Column::Guid", - on_update = "NoAction", - on_delete = "NoAction" - )] - ActivityStatus, - #[sea_orm( - belongs_to = "super::descriptions::Entity", - from = "Column::DescriptionGuid", - to = "super::descriptions::Column::Guid", - on_update = "NoAction", - on_delete = "NoAction" - )] - Descriptions, } impl Related for Entity { @@ -82,22 +65,4 @@ impl Related for Entity { } } -impl Related for Entity { - fn to() -> RelationDef { - Relation::ActivityKinds.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::ActivityStatus.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Descriptions.def() - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/storage/src/entity/activities_categories.rs b/crates/storage/src/entity/activities_categories.rs index acd6f8b..e57fe09 100644 --- a/crates/storage/src/entity/activities_categories.rs +++ b/crates/storage/src/entity/activities_categories.rs @@ -12,7 +12,7 @@ pub struct Model { } #[derive(DeriveIden)] -pub enum ActivitiesCategoriesEnum { +pub enum ActivitiesCategories { Table, Guid, ActivityGuid, diff --git a/crates/storage/src/entity/activities_tags.rs b/crates/storage/src/entity/activities_tags.rs index 0ca6f84..9b16705 100644 --- a/crates/storage/src/entity/activities_tags.rs +++ b/crates/storage/src/entity/activities_tags.rs @@ -12,7 +12,7 @@ pub struct Model { } #[derive(DeriveIden)] -pub enum ActivitiesTagsEnum { +pub enum ActivitiesTags { Table, Guid, TagGuid, diff --git a/crates/storage/src/entity/activity_kinds.rs b/crates/storage/src/entity/activity_kinds.rs deleted file mode 100644 index 26222e6..0000000 --- a/crates/storage/src/entity/activity_kinds.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "activity_kinds")] -pub struct Model { - #[sea_orm(primary_key, auto_increment = false)] - pub guid: String, - pub kind: String, -} - -#[derive(DeriveIden)] -pub enum ActivityKindsEnum { - Table, - Guid, - Kind, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm(has_many = "super::activities::Entity")] - Activities, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Activities.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/storage/src/entity/activity_status.rs b/crates/storage/src/entity/activity_status.rs deleted file mode 100644 index b3bf984..0000000 --- a/crates/storage/src/entity/activity_status.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "activity_status")] -pub struct Model { - #[sea_orm(primary_key, auto_increment = false)] - pub guid: String, - pub status: String, -} - -#[derive(DeriveIden)] -pub enum ActivityStatusEnum { - Table, - Guid, - Status, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm(has_many = "super::activities::Entity")] - Activities, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Activities.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/storage/src/entity/categories.rs b/crates/storage/src/entity/categories.rs index 0d76cee..4121fa1 100644 --- a/crates/storage/src/entity/categories.rs +++ b/crates/storage/src/entity/categories.rs @@ -12,7 +12,7 @@ pub struct Model { } #[derive(DeriveIden)] -pub enum CategoriesEnum { +pub enum Categories { Table, Guid, Category, diff --git a/crates/storage/src/entity/descriptions.rs b/crates/storage/src/entity/descriptions.rs deleted file mode 100644 index 078e74a..0000000 --- a/crates/storage/src/entity/descriptions.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "descriptions")] -pub struct Model { - #[sea_orm(primary_key, auto_increment = false)] - pub guid: String, - pub description: String, -} - -#[derive(DeriveIden)] -pub enum DescriptionsEnum { - Table, - Guid, - Description, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm(has_many = "super::activities::Entity")] - Activities, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Activities.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/crates/storage/src/entity/prelude.rs b/crates/storage/src/entity/prelude.rs index 1d1eac1..cb3a293 100644 --- a/crates/storage/src/entity/prelude.rs +++ b/crates/storage/src/entity/prelude.rs @@ -3,8 +3,5 @@ pub use super::activities::Entity as Activities; pub use super::activities_categories::Entity as ActivitiesCategories; pub use super::activities_tags::Entity as ActivitiesTags; -pub use super::activity_kinds::Entity as ActivityKinds; -pub use super::activity_status::Entity as ActivityStatus; pub use super::categories::Entity as Categories; -pub use super::descriptions::Entity as Descriptions; pub use super::tags::Entity as Tags; diff --git a/crates/storage/src/entity/tags.rs b/crates/storage/src/entity/tags.rs index be6aad5..338b1cd 100644 --- a/crates/storage/src/entity/tags.rs +++ b/crates/storage/src/entity/tags.rs @@ -11,7 +11,7 @@ pub struct Model { } #[derive(DeriveIden)] -pub enum TagsEnum { +pub enum Tags { Table, Guid, Tag, diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index d3dd4d6..50a17d9 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -3,6 +3,7 @@ pub mod entity; pub mod migration; pub mod query; +pub mod repository; pub mod storage; use std::sync::OnceLock; diff --git a/crates/storage/src/migration.rs b/crates/storage/src/migration.rs index 7afb4ee..efc289e 100644 --- a/crates/storage/src/migration.rs +++ b/crates/storage/src/migration.rs @@ -1,13 +1,8 @@ mod m20240325_000001_create_activities; mod m20240326_000001_create_tags; mod m20240326_000002_create_categories; -mod m20240326_000003_create_activity_status; -mod m20240326_000004_create_activity_kinds; -mod m20240326_000005_create_activities_tags; -mod m20240326_000006_create_activities_categories; -mod m20240327_000001_create_activity_status_values; -mod m20240327_000002_create_activity_kind_values; -mod m20240329_000001_create_descriptions; +mod m20240326_000003_create_activities_tags; +mod m20240326_000004_create_activities_categories; pub use sea_orm_migration::prelude::{async_trait, MigrationTrait, MigratorTrait}; @@ -20,13 +15,8 @@ impl MigratorTrait for Migrator { Box::new(m20240325_000001_create_activities::Migration), Box::new(m20240326_000001_create_tags::Migration), Box::new(m20240326_000002_create_categories::Migration), - Box::new(m20240326_000003_create_activity_status::Migration), - Box::new(m20240326_000004_create_activity_kinds::Migration), - Box::new(m20240326_000005_create_activities_tags::Migration), - Box::new(m20240326_000006_create_activities_categories::Migration), - Box::new(m20240327_000001_create_activity_status_values::Migration), - Box::new(m20240327_000002_create_activity_kind_values::Migration), - Box::new(m20240329_000001_create_descriptions::Migration), + Box::new(m20240326_000003_create_activities_tags::Migration), + Box::new(m20240326_000004_create_activities_categories::Migration), ] } } diff --git a/crates/storage/src/migration/m20240325_000001_create_activities.rs b/crates/storage/src/migration/m20240325_000001_create_activities.rs index d4948f6..c1b8e6f 100644 --- a/crates/storage/src/migration/m20240325_000001_create_activities.rs +++ b/crates/storage/src/migration/m20240325_000001_create_activities.rs @@ -1,9 +1,6 @@ use sea_orm_migration::prelude::*; -use crate::entity::activities::ActivitiesEnum; -use crate::entity::activity_kinds::ActivityKindsEnum; -use crate::entity::activity_status::ActivityStatusEnum; -use crate::entity::descriptions::DescriptionsEnum; +use crate::entity::activities::Activities; #[derive(DeriveMigrationName)] pub struct Migration; @@ -14,56 +11,37 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(ActivitiesEnum::Table) + .table(Activities::Table) .if_not_exists() .col( - ColumnDef::new(ActivitiesEnum::Guid) + ColumnDef::new(Activities::Guid) .text() .not_null() .primary_key(), ) + .col(ColumnDef::new(Activities::Description).text().not_null()) .col( - ColumnDef::new(ActivitiesEnum::DescriptionGuid) - .text() - .not_null(), - ) - .col( - ColumnDef::new(ActivitiesEnum::Begin) + ColumnDef::new(Activities::Begin) .timestamp_with_time_zone() .not_null(), ) .col( - ColumnDef::new(ActivitiesEnum::End) + ColumnDef::new(Activities::End) .timestamp_with_time_zone() .null(), ) - .col(ColumnDef::new(ActivitiesEnum::Duration).integer().null()) - .col(ColumnDef::new(ActivitiesEnum::KindGuid).text().not_null()) - .col(ColumnDef::new(ActivitiesEnum::StatusGuid).text().not_null()) - .col(ColumnDef::new(ActivitiesEnum::ParentGuid).text().null()) + .col(ColumnDef::new(Activities::Duration).integer().null()) + .col(ColumnDef::new(Activities::Kind).integer().not_null()) + .col(ColumnDef::new(Activities::Status).integer().not_null()) + .col(ColumnDef::new(Activities::ParentGuid).text().null()) + .col(ColumnDef::new(Activities::CreatedAt).timestamp().not_null()) + .col(ColumnDef::new(Activities::UpdatedAt).timestamp().null()) + .col(ColumnDef::new(Activities::DeletedAt).timestamp().null()) .foreign_key( ForeignKey::create() .name("fk_activities_parent_guid") - .from(ActivitiesEnum::Table, ActivitiesEnum::ParentGuid) - .to(ActivitiesEnum::Table, ActivitiesEnum::Guid), - ) - .foreign_key( - ForeignKey::create() - .name("fk_activities_kind") - .from(ActivitiesEnum::Table, ActivitiesEnum::KindGuid) - .to(ActivityKindsEnum::Table, ActivityKindsEnum::Guid), - ) - .foreign_key( - ForeignKey::create() - .name("fk_activities_status") - .from(ActivitiesEnum::Table, ActivitiesEnum::StatusGuid) - .to(ActivityStatusEnum::Table, ActivityStatusEnum::Guid), - ) - .foreign_key( - ForeignKey::create() - .name("fk_activities_description") - .from(ActivitiesEnum::Table, ActivitiesEnum::DescriptionGuid) - .to(DescriptionsEnum::Table, DescriptionsEnum::Guid), + .from(Activities::Table, Activities::ParentGuid) + .to(Activities::Table, Activities::Guid), ) .to_owned(), ) @@ -74,7 +52,7 @@ impl MigrationTrait for Migration { manager .drop_table( Table::drop() - .table(ActivitiesEnum::Table) + .table(Activities::Table) .if_exists() .to_owned(), ) diff --git a/crates/storage/src/migration/m20240326_000001_create_tags.rs b/crates/storage/src/migration/m20240326_000001_create_tags.rs index 35e5bf6..b6ada7d 100644 --- a/crates/storage/src/migration/m20240326_000001_create_tags.rs +++ b/crates/storage/src/migration/m20240326_000001_create_tags.rs @@ -1,6 +1,6 @@ use sea_orm_migration::prelude::*; -use crate::entity::tags::TagsEnum; +use crate::entity::tags::Tags; #[derive(DeriveMigrationName)] pub struct Migration; @@ -12,14 +12,9 @@ impl MigrationTrait for Migration { .create_table( Table::create() .if_not_exists() - .table(TagsEnum::Table) - .col( - ColumnDef::new(TagsEnum::Guid) - .text() - .not_null() - .primary_key(), - ) - .col(ColumnDef::new(TagsEnum::Tag).text().not_null()) + .table(Tags::Table) + .col(ColumnDef::new(Tags::Guid).text().not_null().primary_key()) + .col(ColumnDef::new(Tags::Tag).text().not_null()) .to_owned(), ) .await @@ -27,7 +22,7 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(TagsEnum::Table).if_exists().to_owned()) + .drop_table(Table::drop().table(Tags::Table).if_exists().to_owned()) .await } } diff --git a/crates/storage/src/migration/m20240326_000002_create_categories.rs b/crates/storage/src/migration/m20240326_000002_create_categories.rs index cf1f26d..1c91730 100644 --- a/crates/storage/src/migration/m20240326_000002_create_categories.rs +++ b/crates/storage/src/migration/m20240326_000002_create_categories.rs @@ -1,6 +1,6 @@ use sea_orm_migration::prelude::*; -use crate::entity::categories::CategoriesEnum; +use crate::entity::categories::Categories; #[derive(DeriveMigrationName)] pub struct Migration; @@ -12,15 +12,15 @@ impl MigrationTrait for Migration { .create_table( Table::create() .if_not_exists() - .table(CategoriesEnum::Table) + .table(Categories::Table) .col( - ColumnDef::new(CategoriesEnum::Guid) + ColumnDef::new(Categories::Guid) .text() .not_null() .primary_key(), ) - .col(ColumnDef::new(CategoriesEnum::Category).text().not_null()) - .col(ColumnDef::new(CategoriesEnum::Description).text().null()) + .col(ColumnDef::new(Categories::Category).text().not_null()) + .col(ColumnDef::new(Categories::Description).text().null()) .to_owned(), ) .await @@ -30,7 +30,7 @@ impl MigrationTrait for Migration { manager .drop_table( Table::drop() - .table(CategoriesEnum::Table) + .table(Categories::Table) .if_exists() .to_owned(), ) diff --git a/crates/storage/src/migration/m20240326_000005_create_activities_tags.rs b/crates/storage/src/migration/m20240326_000003_create_activities_tags.rs similarity index 59% rename from crates/storage/src/migration/m20240326_000005_create_activities_tags.rs rename to crates/storage/src/migration/m20240326_000003_create_activities_tags.rs index afe85b6..f11d571 100644 --- a/crates/storage/src/migration/m20240326_000005_create_activities_tags.rs +++ b/crates/storage/src/migration/m20240326_000003_create_activities_tags.rs @@ -1,8 +1,8 @@ use sea_orm_migration::prelude::*; -use crate::entity::activities::ActivitiesEnum; -use crate::entity::activities_tags::ActivitiesTagsEnum; -use crate::entity::tags::TagsEnum; +use crate::entity::activities::Activities; +use crate::entity::activities_tags::ActivitiesTags; +use crate::entity::tags::Tags; #[derive(DeriveMigrationName)] pub struct Migration; @@ -14,34 +14,30 @@ impl MigrationTrait for Migration { .create_table( Table::create() .if_not_exists() - .table(ActivitiesTagsEnum::Table) + .table(ActivitiesTags::Table) .col( - ColumnDef::new(ActivitiesTagsEnum::Guid) + ColumnDef::new(ActivitiesTags::Guid) .text() .not_null() .primary_key(), ) + .col(ColumnDef::new(ActivitiesTags::TagGuid).text().not_null()) .col( - ColumnDef::new(ActivitiesTagsEnum::TagGuid) - .text() - .not_null(), - ) - .col( - ColumnDef::new(ActivitiesTagsEnum::ActivityGuid) + ColumnDef::new(ActivitiesTags::ActivityGuid) .text() .not_null(), ) .foreign_key( ForeignKey::create() .name("fk_activities_tags_tag_guid") - .from(ActivitiesTagsEnum::Table, ActivitiesTagsEnum::TagGuid) - .to(TagsEnum::Table, TagsEnum::Guid), + .from(ActivitiesTags::Table, ActivitiesTags::TagGuid) + .to(Tags::Table, Tags::Guid), ) .foreign_key( ForeignKey::create() .name("fk_activities_tags_activity_guid") - .from(ActivitiesTagsEnum::Table, ActivitiesTagsEnum::ActivityGuid) - .to(ActivitiesEnum::Table, ActivitiesEnum::Guid), + .from(ActivitiesTags::Table, ActivitiesTags::ActivityGuid) + .to(Activities::Table, Activities::Guid), ) .to_owned(), ) @@ -52,7 +48,7 @@ impl MigrationTrait for Migration { manager .drop_table( Table::drop() - .table(ActivitiesTagsEnum::Table) + .table(ActivitiesTags::Table) .if_exists() .to_owned(), ) diff --git a/crates/storage/src/migration/m20240326_000003_create_activity_status.rs b/crates/storage/src/migration/m20240326_000003_create_activity_status.rs deleted file mode 100644 index 7f63cad..0000000 --- a/crates/storage/src/migration/m20240326_000003_create_activity_status.rs +++ /dev/null @@ -1,38 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::entity::activity_status::ActivityStatusEnum; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .create_table( - Table::create() - .if_not_exists() - .table(ActivityStatusEnum::Table) - .col( - ColumnDef::new(ActivityStatusEnum::Guid) - .text() - .not_null() - .primary_key(), - ) - .col(ColumnDef::new(ActivityStatusEnum::Status).text().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(ActivityStatusEnum::Table) - .if_exists() - .to_owned(), - ) - .await - } -} diff --git a/crates/storage/src/migration/m20240326_000006_create_activities_categories.rs b/crates/storage/src/migration/m20240326_000004_create_activities_categories.rs similarity index 62% rename from crates/storage/src/migration/m20240326_000006_create_activities_categories.rs rename to crates/storage/src/migration/m20240326_000004_create_activities_categories.rs index 9268753..677524a 100644 --- a/crates/storage/src/migration/m20240326_000006_create_activities_categories.rs +++ b/crates/storage/src/migration/m20240326_000004_create_activities_categories.rs @@ -1,8 +1,8 @@ use sea_orm_migration::prelude::*; -use crate::entity::activities::ActivitiesEnum; -use crate::entity::activities_categories::ActivitiesCategoriesEnum; -use crate::entity::categories::CategoriesEnum; +use crate::entity::activities::Activities; +use crate::entity::activities_categories::ActivitiesCategories; +use crate::entity::categories::Categories; #[derive(DeriveMigrationName)] pub struct Migration; @@ -13,21 +13,21 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(ActivitiesCategoriesEnum::Table) + .table(ActivitiesCategories::Table) .if_not_exists() .col( - ColumnDef::new(ActivitiesCategoriesEnum::Guid) + ColumnDef::new(ActivitiesCategories::Guid) .text() .not_null() .primary_key(), ) .col( - ColumnDef::new(ActivitiesCategoriesEnum::ActivityGuid) + ColumnDef::new(ActivitiesCategories::ActivityGuid) .text() .not_null(), ) .col( - ColumnDef::new(ActivitiesCategoriesEnum::CategoryGuid) + ColumnDef::new(ActivitiesCategories::CategoryGuid) .text() .not_null(), ) @@ -35,19 +35,19 @@ impl MigrationTrait for Migration { ForeignKey::create() .name("fk_activities_categories_category_guid") .from( - ActivitiesCategoriesEnum::Table, - ActivitiesCategoriesEnum::CategoryGuid, + ActivitiesCategories::Table, + ActivitiesCategories::CategoryGuid, ) - .to(CategoriesEnum::Table, CategoriesEnum::Guid), + .to(Categories::Table, Categories::Guid), ) .foreign_key( ForeignKey::create() .name("fk_activities_categories_activity_guid") .from( - ActivitiesCategoriesEnum::Table, - ActivitiesCategoriesEnum::ActivityGuid, + ActivitiesCategories::Table, + ActivitiesCategories::ActivityGuid, ) - .to(ActivitiesEnum::Table, ActivitiesEnum::Guid), + .to(Activities::Table, Activities::Guid), ) .to_owned(), ) @@ -58,7 +58,7 @@ impl MigrationTrait for Migration { manager .drop_table( Table::drop() - .table(ActivitiesCategoriesEnum::Table) + .table(ActivitiesCategories::Table) .if_exists() .to_owned(), ) diff --git a/crates/storage/src/migration/m20240326_000004_create_activity_kinds.rs b/crates/storage/src/migration/m20240326_000004_create_activity_kinds.rs deleted file mode 100644 index c729a61..0000000 --- a/crates/storage/src/migration/m20240326_000004_create_activity_kinds.rs +++ /dev/null @@ -1,38 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::entity::activity_kinds::ActivityKindsEnum; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .create_table( - Table::create() - .if_not_exists() - .table(ActivityKindsEnum::Table) - .col( - ColumnDef::new(ActivityKindsEnum::Guid) - .text() - .not_null() - .primary_key(), - ) - .col(ColumnDef::new(ActivityKindsEnum::Kind).text().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(ActivityKindsEnum::Table) - .if_exists() - .to_owned(), - ) - .await - } -} diff --git a/crates/storage/src/migration/m20240327_000001_create_activity_status_values.rs b/crates/storage/src/migration/m20240327_000001_create_activity_status_values.rs deleted file mode 100644 index 165c7e0..0000000 --- a/crates/storage/src/migration/m20240327_000001_create_activity_status_values.rs +++ /dev/null @@ -1,42 +0,0 @@ -use itertools::Itertools; -use pace_core::prelude::ActivityStatusKind; -use strum::IntoEnumIterator; -use ulid::Ulid; - -use sea_orm_migration::prelude::*; - -use crate::entity::activity_status::ActivityStatusEnum; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - let activity_kinds = ActivityStatusKind::iter() - .map(|kind| { - Query::insert() - .into_table(ActivityStatusEnum::Table) - .columns([ActivityStatusEnum::Guid, ActivityStatusEnum::Status]) - .values_panic([Ulid::new().to_string().into(), kind.to_string().into()]) - .to_owned() - }) - .collect_vec(); - - for kind in activity_kinds { - manager.exec_stmt(kind).await?; - } - - Ok(()) - } - - async fn down(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .exec_stmt( - Query::delete() - .from_table(ActivityStatusEnum::Table) - .to_owned(), - ) - .await - } -} diff --git a/crates/storage/src/migration/m20240327_000002_create_activity_kind_values.rs b/crates/storage/src/migration/m20240327_000002_create_activity_kind_values.rs deleted file mode 100644 index 677f785..0000000 --- a/crates/storage/src/migration/m20240327_000002_create_activity_kind_values.rs +++ /dev/null @@ -1,41 +0,0 @@ -use itertools::Itertools; -use pace_core::prelude::ActivityKind; -use sea_orm_migration::prelude::*; -use strum::IntoEnumIterator; -use ulid::Ulid; - -use crate::entity::activity_kinds::ActivityKindsEnum; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - let activity_kinds = ActivityKind::iter() - .map(|kind| { - Query::insert() - .into_table(ActivityKindsEnum::Table) - .columns([ActivityKindsEnum::Guid, ActivityKindsEnum::Kind]) - .values_panic([Ulid::new().to_string().into(), kind.to_string().into()]) - .to_owned() - }) - .collect_vec(); - - for kind in activity_kinds { - manager.exec_stmt(kind).await?; - } - - Ok(()) - } - - async fn down(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .exec_stmt( - Query::delete() - .from_table(ActivityKindsEnum::Table) - .to_owned(), - ) - .await - } -} diff --git a/crates/storage/src/migration/m20240329_000001_create_descriptions.rs b/crates/storage/src/migration/m20240329_000001_create_descriptions.rs deleted file mode 100644 index cb4f992..0000000 --- a/crates/storage/src/migration/m20240329_000001_create_descriptions.rs +++ /dev/null @@ -1,42 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::entity::descriptions::DescriptionsEnum; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .create_table( - Table::create() - .if_not_exists() - .table(DescriptionsEnum::Table) - .col( - ColumnDef::new(DescriptionsEnum::Guid) - .text() - .not_null() - .primary_key(), - ) - .col( - ColumnDef::new(DescriptionsEnum::Description) - .text() - .not_null(), - ) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &'life1 SchemaManager<'_>) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(DescriptionsEnum::Table) - .if_exists() - .to_owned(), - ) - .await - } -} diff --git a/crates/storage/src/repository.rs b/crates/storage/src/repository.rs new file mode 100644 index 0000000..5159dc9 --- /dev/null +++ b/crates/storage/src/repository.rs @@ -0,0 +1,74 @@ +pub mod category; + +use pace_error::{PaceOptResult, PaceResult}; + +pub trait Repository { + /// Read a single entity by its id. + /// + /// # Arguments + /// + /// * `id` - The id of the entity to read. + /// + /// # Errors + /// + /// Returns an error if there was a problem reading the entity. + /// + /// # Returns + /// + /// Returns the entity if it exists or None if it does not. + fn read(&self, id: &str) -> PaceOptResult; + + /// Read all entities of a given type. + /// + /// # Errors + /// + /// Returns an error if there was a problem reading the entities. + /// + /// # Returns + /// + /// Returns a vector of all entities of the given type or an + /// empty vector if there are none. + fn read_all(&self) -> PaceOptResult>; + + /// Create a new entity of a given type. + /// + /// # Arguments + /// + /// * `entity` - The entity to create. + /// + /// # Errors + /// + /// Returns an error if there was a problem creating the entity. + /// + /// # Returns + /// + /// Returns the id of the created entity. + fn create(&self, entity: &T) -> PaceResult; + + /// Update an existing entity of a given type. + /// + /// # Arguments + /// + /// * `id` - The id of the entity to update. + /// * `entity` - The entity to update. + /// + /// # Errors + /// + /// Returns an error if there was a problem updating the entity. + /// + /// # Returns + /// + /// Returns nothing if the entity was updated successfully. + fn update(&self, id: &str, entity: &T) -> PaceResult<()>; + + /// Delete an existing entity of a given type. + /// + /// # Arguments + /// + /// * `id` - The id of the entity to delete. + /// + /// # Errors + /// + /// Returns an error if there was a problem deleting the entity. + fn delete(&self, id: &str) -> PaceResult<()>; +} diff --git a/crates/storage/src/repository/category.rs b/crates/storage/src/repository/category.rs new file mode 100644 index 0000000..5d064af --- /dev/null +++ b/crates/storage/src/repository/category.rs @@ -0,0 +1,32 @@ +use pace_error::{PaceOptResult, PaceResult}; +use sea_orm::{sea_query::Query, DatabaseConnection}; + +use crate::entity::categories::{Entity as CategoryEntity, Model as CategoryModel}; +use crate::entity::tags::Entity as TagEntity; +use crate::repository::Repository; + +pub struct CategoryRepository { + connection: DatabaseConnection, +} + +impl Repository for CategoryRepository { + fn read(&self, id: &str) -> PaceOptResult { + unimplemented!() + } + + fn read_all(&self) -> PaceOptResult> { + unimplemented!() + } + + fn create(&self, entity: &CategoryEntity) -> PaceResult { + unimplemented!() + } + + fn update(&self, id: &str, entity: &CategoryEntity) -> PaceResult<()> { + unimplemented!() + } + + fn delete(&self, id: &str) -> PaceResult<()> { + unimplemented!() + } +} diff --git a/crates/storage/src/storage/sqlite.rs b/crates/storage/src/storage/sqlite.rs index 55bc61a..3beb275 100644 --- a/crates/storage/src/storage/sqlite.rs +++ b/crates/storage/src/storage/sqlite.rs @@ -19,7 +19,7 @@ use pace_error::{DatabaseStorageErrorKind, PaceOptResult, PaceResult}; use pace_time::{date::PaceDate, duration::PaceDurationRange, time_range::TimeRangeOptions}; use crate::{ - entity::{descriptions, prelude::Activities}, + entity::prelude::Activities, migration::{Migrator, MigratorTrait}, runtime, }; @@ -123,10 +123,10 @@ impl ActivityReadOps for DatabaseActivityStorage { .into()); }; - let _description = activity - .find_related(descriptions::Entity) - .one(&self.connection) - .await; + // let _description = activity + // .find_related(descriptions::Entity) + // .one(&self.connection) + // .await; todo!("implement read_activity for sqlite"); diff --git a/db/Entity-Relationship.png b/db/Entity-Relationship.png index c14e97d..ef5d6c8 100644 Binary files a/db/Entity-Relationship.png and b/db/Entity-Relationship.png differ