add existing profile activate feature

This commit is contained in:
yggverse 2024-11-13 12:55:36 +02:00
parent aa076b370b
commit 84926a4e8c
2 changed files with 36 additions and 9 deletions

View File

@ -25,15 +25,16 @@ impl Welcome {
move |value| { move |value| {
match value { match value {
Some(id) => { Some(id) => {
// Select profile by record ID @TODO // Activate selected profile by record ID
let _ = profile.database.activate(id);
} }
None => { None => {
// Create and select new profile // Create and select new profile
let _ = profile let _ = profile
.database .database
.add(true, &DateTime::now_local().unwrap(), None); .add(true, DateTime::now_local().unwrap(), None);
} }
} } // @TODO handle result
} }
}); });

View File

@ -43,7 +43,8 @@ impl Database {
// Setters // Setters
pub fn add(&self, is_active: bool, time: &DateTime, name: Option<&str>) -> Result<i64, ()> { /// Create new record in `Self` database connected
pub fn add(&self, is_active: bool, time: DateTime, name: Option<&str>) -> Result<i64, ()> {
// Begin new transaction // Begin new transaction
let mut writable = self.connection.write().unwrap(); let mut writable = self.connection.write().unwrap();
let tx = writable.transaction().unwrap(); let tx = writable.transaction().unwrap();
@ -56,7 +57,7 @@ impl Database {
&tx, &tx,
record.id, record.id,
false, false,
&record.time, record.time,
record.name.as_ref().map(|x| x.as_str()), record.name.as_ref().map(|x| x.as_str()),
); );
} }
@ -74,6 +75,31 @@ impl Database {
Err(_) => Err(()), // @TODO 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 // Low-level DB API
@ -94,7 +120,7 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
pub fn insert( pub fn insert(
tx: &Transaction, tx: &Transaction,
is_active: bool, is_active: bool,
time: &DateTime, time: DateTime,
name: Option<&str>, name: Option<&str>,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
tx.execute( tx.execute(
@ -111,11 +137,11 @@ pub fn update(
tx: &Transaction, tx: &Transaction,
id: i64, id: i64,
is_active: bool, is_active: bool,
time: &DateTime, time: DateTime,
name: Option<&str>, name: Option<&str>,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
tx.execute( 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), (is_active, time.to_unix(), name, id),
) )
} }
@ -141,7 +167,7 @@ pub fn select(tx: &Transaction) -> Result<Vec<Table>, Error> {
Ok(records) Ok(records)
} }
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> { pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `profile` WHERE `id` = ?", [id]) tx.execute("DELETE FROM `profile` WHERE `id` = ?", [id])
} }