From 8af2ff051936124b21d4fb03b0a62ffcd3482709 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sat, 9 May 2026 20:59:47 +0200 Subject: [PATCH] load profile when starting the application --- application/src/backend.rs | 82 +++++++++++++++++-- application/src/main.rs | 15 +++- entity/src/lib.rs | 2 +- entity/src/usersettings.rs | 4 +- .../src/m20260502_184134_create_settings.rs | 1 + 5 files changed, 91 insertions(+), 13 deletions(-) diff --git a/application/src/backend.rs b/application/src/backend.rs index acacc1f..8cff088 100644 --- a/application/src/backend.rs +++ b/application/src/backend.rs @@ -1,33 +1,40 @@ -use sea_orm::{Database, DatabaseConnection, DbErr}; +use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, DbErr, EntityTrait, QueryFilter, ColumnTrait, Set}; use tokio::runtime::Runtime; -use std::sync::Arc; +use std::sync::{Arc}; use migration::{Migrator, MigratorTrait}; use ui_rs::ui; +use entity::usersettings; +use entity::usersettings::Entity as UserSettings; + pub struct Backend { rt: Arc, db: DatabaseConnection, + + pub current_profile: Option, } impl Backend { - pub fn new() -> Self { + pub fn new() -> Result { let rt = Arc::new( tokio::runtime::Builder::new_multi_thread() .enable_all() .build() - .unwrap() + .unwrap(), ); let db_file = ui::app_configfile("note.db"); let connection_str = format!("sqlite://{}?mode=rwc", db_file); let db = rt.block_on(async { - Database::connect(connection_str) - .await - .unwrap() // TODO: check error - }); + Database::connect(connection_str).await + })?; - Self { rt, db } + Ok(Self { + rt, + db, + current_profile: None, + }) } pub fn migrate(&self) -> Result<(), DbErr> { @@ -37,4 +44,61 @@ impl Backend { Ok(()) } + + pub fn load_profile( + &self, + profile_id: i32, + host: &str, + user: &str, + ) -> Result { + + let host = host.to_string(); + let user = user.to_string(); + + let profile: usersettings::Model = self.rt.block_on(async { + + // ------------------------------------------------- + // explicit profile id + // ------------------------------------------------- + if profile_id != 0 { + if let Some(model) = UserSettings::find_by_id(profile_id) + .one(&self.db) + .await? + { + return Ok::(model); + } + } + + // ------------------------------------------------- + // existing host/user profile + // ------------------------------------------------- + if let Some(model) = UserSettings::find() + .filter(usersettings::Column::Host.eq(host.clone())) + .filter(usersettings::Column::User.eq(user.clone())) + .one(&self.db) + .await? + { + return Ok::(model); + } + + // ------------------------------------------------- + // create new profile + // ------------------------------------------------- + let active = usersettings::ActiveModel { + host: Set(host), + user: Set(user), + profile: Set("default".to_string()), + root_id: Set(0), + + ..Default::default() + }; + + let inserted = active.insert(&self.db).await?; + + Ok::(inserted) + + })?; + + Ok(profile) + } } diff --git a/application/src/main.rs b/application/src/main.rs index a2d2e48..2709621 100644 --- a/application/src/main.rs +++ b/application/src/main.rs @@ -37,9 +37,20 @@ use crate::window::*; fn main() { ui::app_init("note"); - let backend = Backend::new(); + let backend_result = Backend::new(); + if !backend_result.is_ok() { + println!("Error while initializing backend"); + return; + } + let mut backend = backend_result.unwrap(); backend.migrate().unwrap(); // todo: check error - + let settings_result = backend.load_profile(0, "testhost", "testuser"); + if !settings_result.is_ok() { + println!("Error while loading profile"); + return; + } + backend.current_profile = Some(settings_result.unwrap()); + let mut app = App { backend: backend }; ui::app_run::(&mut app); } diff --git a/entity/src/lib.rs b/entity/src/lib.rs index 5db23e3..082e97c 100644 --- a/entity/src/lib.rs +++ b/entity/src/lib.rs @@ -1,2 +1,2 @@ -mod usersettings; +pub mod usersettings; mod prelude; \ No newline at end of file diff --git a/entity/src/usersettings.rs b/entity/src/usersettings.rs index 48b4035..ec1aba7 100644 --- a/entity/src/usersettings.rs +++ b/entity/src/usersettings.rs @@ -8,7 +8,9 @@ pub struct Model { pub host: String, pub user: String, - pub profile: 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 ee9d079..442746b 100644 --- a/migration/src/m20260502_184134_create_settings.rs +++ b/migration/src/m20260502_184134_create_settings.rs @@ -15,6 +15,7 @@ impl MigrationTrait for Migration { .col(string("host")) .col(string("user")) .col(string("profile")) + .col(integer("root_id")) .to_owned(), ) .await -- 2.47.3