-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
mod file_repository; | ||
mod prop_repository; | ||
mod training_repository; | ||
mod tree_repository; | ||
pub use file_repository::*; | ||
pub use prop_repository::*; | ||
pub use training_repository::*; | ||
pub use tree_repository::*; |
54 changes: 54 additions & 0 deletions
54
backend/src/common/database/repositories/prop_repository.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::common::database::queries::*; | ||
use crate::services::*; | ||
use crate::types::*; | ||
use crate::utils::{get_timestamp, get_unique_id}; | ||
use rusqlite::types::Value; | ||
use std::sync::Arc; | ||
|
||
const TABLE: &str = "trees_props"; | ||
|
||
pub struct PropRepository { | ||
db: Arc<dyn DatabaseInterface>, | ||
} | ||
|
||
impl PropRepository { | ||
#[allow(unused)] | ||
pub async fn get(&self, id: u64) -> Result<PropRecord> { | ||
let query = SelectQuery { | ||
table_name: TABLE.to_string(), | ||
conditions: Attributes::from(&[("id".to_string(), Value::from(id as i64))]), | ||
..Default::default() | ||
}; | ||
|
||
match self.db.get_record(query).await { | ||
Ok(Some(props)) => PropRecord::from_attributes(&props), | ||
Ok(None) => Err(Error::FileNotFound), | ||
Err(err) => Err(err), | ||
} | ||
} | ||
|
||
pub async fn add(&self, prop: &PropRecord) -> Result<()> { | ||
let prop = PropRecord { | ||
id: get_unique_id()?, | ||
added_at: get_timestamp(), | ||
..prop.clone() | ||
}; | ||
|
||
let query = InsertQuery { | ||
table_name: TABLE.to_string(), | ||
attributes: prop.to_attributes(), | ||
}; | ||
|
||
match self.db.add_record(query).await { | ||
Ok(_) => Ok(()), | ||
Err(err) => Err(err), | ||
} | ||
} | ||
} | ||
|
||
impl Locatable for PropRepository { | ||
fn create(locator: &Locator) -> Result<Self> { | ||
let db = locator.get::<PreferredDatabase>()?.driver(); | ||
Ok(Self { db }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::types::*; | ||
use rusqlite::types::Value; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct PropRecord { | ||
pub id: u64, | ||
pub tree_id: u64, | ||
pub added_at: u64, | ||
pub added_by: u64, | ||
pub name: String, | ||
pub value: String, | ||
} | ||
|
||
impl PropRecord { | ||
pub fn from_attributes(attributes: &Attributes) -> Result<Self> { | ||
Ok(Self { | ||
id: attributes.require_u64("id")?, | ||
tree_id: attributes.require_u64("tree_id")?, | ||
added_at: attributes.require_u64("added_at")?, | ||
added_by: attributes.require_u64("added_by")?, | ||
name: attributes.require_string("name")?, | ||
value: attributes.require_string("value")?, | ||
}) | ||
} | ||
|
||
pub fn to_attributes(&self) -> Attributes { | ||
Attributes::from(&[ | ||
("id".to_string(), Value::from(self.id as i64)), | ||
("tree_id".to_string(), Value::from(self.tree_id as i64)), | ||
("added_at".to_string(), Value::from(self.added_at as i64)), | ||
("added_by".to_string(), Value::from(self.added_by as i64)), | ||
("name".to_string(), Value::from(self.name.clone())), | ||
("value".to_string(), Value::from(self.value.clone())), | ||
]) | ||
} | ||
} |