diff --git a/src/app/browser/welcome.rs b/src/app/browser/welcome.rs index b41046a3..2e7f7f46 100644 --- a/src/app/browser/welcome.rs +++ b/src/app/browser/welcome.rs @@ -25,15 +25,16 @@ impl Welcome { move |value| { match value { Some(id) => { - // Select profile by record ID @TODO + // Activate selected profile by record ID + let _ = profile.database.activate(id); } None => { // Create and select new profile let _ = profile .database - .add(true, &DateTime::now_local().unwrap(), None); + .add(true, DateTime::now_local().unwrap(), None); } - } + } // @TODO handle result } }); diff --git a/src/profile/database.rs b/src/profile/database.rs index ad50f5b1..421d6f15 100644 --- a/src/profile/database.rs +++ b/src/profile/database.rs @@ -43,7 +43,8 @@ impl Database { // Setters - pub fn add(&self, is_active: bool, time: &DateTime, name: Option<&str>) -> Result { + /// Create new record in `Self` database connected + pub fn add(&self, is_active: bool, time: DateTime, name: Option<&str>) -> Result { // Begin new transaction let mut writable = self.connection.write().unwrap(); let tx = writable.transaction().unwrap(); @@ -56,7 +57,7 @@ impl Database { &tx, record.id, false, - &record.time, + record.time, record.name.as_ref().map(|x| x.as_str()), ); } @@ -74,6 +75,31 @@ impl Database { Err(_) => Err(()), // @TODO } } + + /// Set `is_active` status `true` for the record with given profile ID + /// * reset other records to `false` + pub fn activate(&self, id: i64) -> Result<(), ()> { + // Begin new transaction + let mut writable = self.connection.write().unwrap(); + let tx = writable.transaction().unwrap(); + + // Deactivate other records as only one profile should be active + for record in select(&tx).unwrap() { + let _ = update( + &tx, + record.id, + if record.id == id { true } else { false }, + record.time, + record.name.as_ref().map(|x| x.as_str()), + ); + } + + // Done + match tx.commit() { + Ok(_) => Ok(()), + Err(_) => Err(()), + } // @TODO make sure ID exist and was changed + } } // Low-level DB API @@ -94,7 +120,7 @@ pub fn init(tx: &Transaction) -> Result { pub fn insert( tx: &Transaction, is_active: bool, - time: &DateTime, + time: DateTime, name: Option<&str>, ) -> Result { tx.execute( @@ -111,11 +137,11 @@ pub fn update( tx: &Transaction, id: i64, is_active: bool, - time: &DateTime, + time: DateTime, name: Option<&str>, ) -> Result { tx.execute( - "UPDATE `profile` SET `is_active` = ?, `time` = ?, `name` = ? WHERE `id` = ? LIMIT 1", + "UPDATE `profile` SET `is_active` = ?, `time` = ?, `name` = ? WHERE `id` = ?", (is_active, time.to_unix(), name, id), ) } @@ -141,7 +167,7 @@ pub fn select(tx: &Transaction) -> Result, Error> { Ok(records) } -pub fn delete(tx: &Transaction, id: &i64) -> Result { +pub fn delete(tx: &Transaction, id: i64) -> Result { tx.execute("DELETE FROM `profile` WHERE `id` = ?", [id]) }