From ab74da0f1d7ad70f6bee8d4c77cf17d93024d306 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 10 May 2026 13:12:32 +0200 Subject: [PATCH] add collection entity --- application/src/backend.rs | 25 ++++++++-------- application/src/main.rs | 2 +- entity/src/collection.rs | 29 +++++++++++++++++++ entity/src/lib.rs | 5 ++-- entity/src/{usersettings.rs => profile.rs} | 4 +-- .../src/m20260502_184134_create_settings.rs | 29 +++++++++++++++++-- ui-rs/src/ui/application.rs | 4 +-- 7 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 entity/src/collection.rs rename entity/src/{usersettings.rs => profile.rs} (84%) diff --git a/application/src/backend.rs b/application/src/backend.rs index fa8c4d8..7691741 100644 --- a/application/src/backend.rs +++ b/application/src/backend.rs @@ -4,14 +4,14 @@ use std::sync::{Arc}; use migration::{Migrator, MigratorTrait}; use ui_rs::ui; -use entity::usersettings; -use entity::usersettings::Entity as UserSettings; +use entity::profile; +use entity::profile::Entity as UserSettings; pub struct Backend { rt: Arc, db: DatabaseConnection, - pub current_profile: Option, + pub current_profile: Option, } impl Backend { @@ -50,12 +50,12 @@ impl Backend { profile_id: Option, host: &str, user: &str, - ) -> Result { + ) -> Result { let host = host.to_string(); let user = user.to_string(); - let profile: usersettings::Model = self.rt.block_on(async { + let profile: profile::Model = self.rt.block_on(async { // If a profile id was specified, we are trying to use that, // but if it doesn't exist, an error is returned if let Some(id) = profile_id { @@ -63,37 +63,36 @@ impl Backend { .one(&self.db) .await? { - return Ok::(model); + return Ok::(model); } else { let err = format!("user settings {} not found", id); - return Err::(DbErr::RecordNotFound(err)); + return Err::(DbErr::RecordNotFound(err)); } } // Try to find a profile for the specified host/user if let Some(model) = UserSettings::find() - .filter(usersettings::Column::Host.eq(host.clone())) - .filter(usersettings::Column::User.eq(user.clone())) + .filter(profile::Column::Host.eq(host.clone())) + .filter(profile::Column::User.eq(user.clone())) .one(&self.db) .await? { - return Ok::(model); + return Ok::(model); } // Create new profile let profile_name = format!("{}/{}", host, user); - let active = usersettings::ActiveModel { + let active = profile::ActiveModel { host: Set(host), user: Set(user), profile: Set(profile_name), - root_id: Set(0), ..Default::default() }; let inserted = active.insert(&self.db).await?; - Ok::(inserted) + Ok::(inserted) })?; Ok(profile) diff --git a/application/src/main.rs b/application/src/main.rs index 537c18d..c055206 100644 --- a/application/src/main.rs +++ b/application/src/main.rs @@ -41,7 +41,7 @@ fn main() { let backend = match init_backend() { Ok(backend) => backend, Err(e) => { - app_run_startup_error(e.title.as_str(), e.message.as_str()); + ui::app_run_startup_error(e.title.as_str(), e.message.as_str()); return; } }; diff --git a/entity/src/collection.rs b/entity/src/collection.rs new file mode 100644 index 0000000..ecc0c1a --- /dev/null +++ b/entity/src/collection.rs @@ -0,0 +1,29 @@ +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "collection")] +pub struct Model { + #[sea_orm(primary_key)] + pub collection_id: i32, + pub profile_id: i32, + + pub name: String, + pub path: String, + pub icon: String, + pub kind: CollectionType, + +} + +#[derive(EnumIter, DeriveActiveEnum, Clone, Debug, PartialEq)] +#[sea_orm(rs_type = "i32", db_type = "Integer")] +pub enum CollectionType { + Notebook = 0, + Files = 1, + Feed = 2, + Mail = 3, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/entity/src/lib.rs b/entity/src/lib.rs index 082e97c..8a998bb 100644 --- a/entity/src/lib.rs +++ b/entity/src/lib.rs @@ -1,2 +1,3 @@ -pub mod usersettings; -mod prelude; \ No newline at end of file +mod prelude; +pub mod profile; +pub mod collection; \ No newline at end of file diff --git a/entity/src/usersettings.rs b/entity/src/profile.rs similarity index 84% rename from entity/src/usersettings.rs rename to entity/src/profile.rs index ec1aba7..660ebc2 100644 --- a/entity/src/usersettings.rs +++ b/entity/src/profile.rs @@ -1,7 +1,7 @@ use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] -#[sea_orm(table_name = "user_settings")] +#[sea_orm(table_name = "profile")] pub struct Model { #[sea_orm(primary_key)] pub id: i32, @@ -9,8 +9,6 @@ pub struct Model { pub host: String, pub user: String, pub profile: String, - - pub root_id: i32 } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/migration/src/m20260502_184134_create_settings.rs b/migration/src/m20260502_184134_create_settings.rs index 442746b..5f66a18 100644 --- a/migration/src/m20260502_184134_create_settings.rs +++ b/migration/src/m20260502_184134_create_settings.rs @@ -9,13 +9,34 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table("user_settings") + .table("profile") .if_not_exists() .col(pk_auto("id")) .col(string("host")) .col(string("user")) .col(string("profile")) - .col(integer("root_id")) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table("collection") + .if_not_exists() + .col(pk_auto("collection_id")) + .col(integer("profile_id")) + .col(string("name")) + .col(string("path")) + .col(string("icon")) + .col(integer("kind")) + .foreign_key( + ForeignKey::create() + .name("fk-collection-profile") + .from("collection", "profile_id") + .to("Profile", "id") + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade)) .to_owned(), ) .await @@ -23,7 +44,9 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table("user_settings").to_owned()) + .drop_table(Table::drop().table("collection").to_owned()).await?; + manager + .drop_table(Table::drop().table("profile").to_owned()) .await } } diff --git a/ui-rs/src/ui/application.rs b/ui-rs/src/ui/application.rs index af70f65..fea2cba 100644 --- a/ui-rs/src/ui/application.rs +++ b/ui-rs/src/ui/application.rs @@ -48,7 +48,7 @@ pub struct AppContext { pub struct NoAppData {} impl UiModel for NoAppData { - fn init(&mut self, ctx: &UiContext) {} + fn init(&mut self, _ctx: &UiContext) {} } impl UiActions for NoAppData { @@ -108,7 +108,7 @@ struct ErrApp<'a> { } impl<'a> Application for ErrApp<'a> { - fn on_startup(&mut self, app: &AppContext) { + fn on_startup(&mut self, _app: &AppContext) { dialog(|d| { d.title(self.title); d.content(self.message); -- 2.47.3