]> uap-core.de Git - note.git/commitdiff
load profile when starting the application main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 9 May 2026 18:59:47 +0000 (20:59 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 9 May 2026 18:59:47 +0000 (20:59 +0200)
application/src/backend.rs
application/src/main.rs
entity/src/lib.rs
entity/src/usersettings.rs
migration/src/m20260502_184134_create_settings.rs

index acacc1f9f302409848159363247d854fb488347e..8cff0884431b9afd1749918661ef89df88b04ff6 100644 (file)
@@ -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<Runtime>,
     db: DatabaseConnection,
+
+    pub current_profile: Option<usersettings::Model>,
 }
 
 impl Backend {
-    pub fn new() -> Self {
+    pub fn new() -> Result<Self, DbErr> {
         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<usersettings::Model, DbErr> {
+
+        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::<usersettings::Model, DbErr>(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::<usersettings::Model, DbErr>(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::<usersettings::Model, DbErr>(inserted)
+
+        })?;
+
+        Ok(profile)
+    }
 }
index a2d2e48683a6f86e1a3002475fcabb964ae8d247..270962178596f609c66bb13da82d9f3dcd48c9f9 100644 (file)
@@ -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::<MainWindow>(&mut app);
 }
index 5db23e3d5545ff1f37a4a6a23d8bd5de2e61a9be..082e97c0dde47049dcf0c06338274ad2ed8a900d 100644 (file)
@@ -1,2 +1,2 @@
-mod usersettings;
+pub mod usersettings;
 mod prelude;
\ No newline at end of file
index 48b4035f6c4d7cb6af68301acd980d02935a28a0..ec1aba70215ca87e72e68fb6b97ea9b5f59a3e7b 100644 (file)
@@ -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)]
index ee9d0792fd9254dce374f5af5ae3ad1ab3b1bfd3..442746bd8c2162ae514c54a40cc4018b7b146e03 100644 (file)
@@ -15,6 +15,7 @@ impl MigrationTrait for Migration {
                     .col(string("host"))
                     .col(string("user"))
                     .col(string("profile"))
+                    .col(integer("root_id"))
                     .to_owned(),
             )
             .await