Skip to content

Commit

Permalink
Track thumbnail changes as props
Browse files Browse the repository at this point in the history
  • Loading branch information
umonkey committed Jan 1, 2025
1 parent 43c1b2b commit 58191c6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backend/src/common/database/repositories/mod.rs
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 backend/src/common/database/repositories/prop_repository.rs
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 })
}
}
18 changes: 17 additions & 1 deletion backend/src/handlers/update_tree_thumbnail_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;
pub struct UpdateTreeThumbnailHandler {
trees: Arc<TreeRepository>,
files: Arc<FileRepository>,
props: Arc<PropRepository>,
}

impl UpdateTreeThumbnailHandler {
Expand All @@ -16,6 +17,16 @@ impl UpdateTreeThumbnailHandler {

self.trees.update_thumbnail(tree.id, file.small_id).await?;

self.props
.add(&PropRecord {
tree_id: tree.id,
added_by: req.user_id,
name: "thumbnail_id".to_string(),
value: file.small_id.to_string(),
..Default::default()
})
.await?;

info!(
"Thumbnail for tree {} changed to {} by {}.",
req.tree_id, req.file_id, req.user_id
Expand All @@ -29,6 +40,11 @@ impl Locatable for UpdateTreeThumbnailHandler {
fn create(locator: &Locator) -> Result<Self> {
let trees = locator.get::<TreeRepository>()?;
let files = locator.get::<FileRepository>()?;
Ok(Self { trees, files })
let props = locator.get::<PropRepository>()?;
Ok(Self {
trees,
files,
props,
})
}
}
1 change: 0 additions & 1 deletion backend/src/services/database/database_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub trait DatabaseInterface: Send + Sync {
async fn get_record(&self, query: SelectQuery) -> Result<Option<Attributes>>;
#[allow(unused)]
async fn get_records(&self, query: SelectQuery) -> Result<Vec<Attributes>>;
#[allow(unused)]
async fn add_record(&self, query: InsertQuery) -> Result<()>;
async fn update(&self, query: UpdateQuery) -> Result<()>;

Expand Down
4 changes: 0 additions & 4 deletions backend/src/types/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,4 @@ impl Attributes {
_ => Err(Error::DatabaseStructure),
}
}

fn type_name<T>(_: &T) -> &'static str {
std::any::type_name::<T>()
}
}
2 changes: 2 additions & 0 deletions backend/src/types/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod comment_record;
mod file_record;
mod osm_tree_record;
mod prop_record;
mod species_record;
mod training_record;
mod tree_record;
mod user_record;
pub use self::comment_record::*;
pub use self::file_record::*;
pub use self::osm_tree_record::*;
pub use self::prop_record::*;
pub use self::species_record::*;
pub use self::training_record::*;
pub use self::tree_record::*;
Expand Down
36 changes: 36 additions & 0 deletions backend/src/types/database/prop_record.rs
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())),
])
}
}

0 comments on commit 58191c6

Please sign in to comment.