-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> {
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)
+ }
}
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);
}