From 235d49ba25ba3cc6c5d87ef43df3c44d14abcbd2 Mon Sep 17 00:00:00 2001 From: angrynode Date: Thu, 14 May 2026 17:45:45 +0200 Subject: [PATCH] feat: find_by_id_str helpers --- src/database/category.rs | 21 +++++++++++++++++++-- src/database/content_folder.rs | 21 +++++++++++++++++++-- src/database/operation.rs | 2 +- src/extractors/normalized_path/mod.rs | 2 +- src/state/mod.rs | 2 +- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/database/category.rs b/src/database/category.rs index 083618f..939f309 100644 --- a/src/database/category.rs +++ b/src/database/category.rs @@ -27,7 +27,7 @@ pub struct Model { #[sea_orm(unique)] pub path: NormalizedPathAbsolute, #[sea_orm(has_many)] - pub content_folders: HasMany, + pub content_folders: HasMany, } #[async_trait::async_trait] @@ -48,6 +48,8 @@ pub enum CategoryError { DB { source: sea_orm::DbErr }, #[snafu(display("The category (ID: {id}) does not exist"))] IDNotFound { id: i32 }, + #[snafu(display("The category id is invalid: {id}"))] + IDInvalid { id: String }, #[snafu(display("The category (Name: {name}) does not exist"))] NameNotFound { name: String }, #[snafu(display("Failed to save the operation log"))] @@ -84,7 +86,9 @@ impl CategoryOperator { /// Find one category by ID /// - /// Should not fail, unless SQLite was corrupted for some reason. + /// Fails if: + /// + /// - the requested ID does not exist pub async fn find_by_id(&self, id: i32) -> Result { let category = Entity::find_by_id(id) .one(&self.state.database) @@ -97,6 +101,19 @@ impl CategoryOperator { } } + /// Find one category by stringy ID + /// + /// Fails if: + /// + /// - the requested ID does not exist + /// - the requested ID could not be parsed into an i32 + pub async fn find_by_id_str(&self, id: &str) -> Result { + let id: i32 = id + .parse() + .map_err(|_e| CategoryError::IDInvalid { id: id.to_string() })?; + self.find_by_id(id).await + } + /// Find one category by Name /// /// Should not fail, unless SQLite was corrupted for some reason. diff --git a/src/database/content_folder.rs b/src/database/content_folder.rs index 4d6ecaa..33f69a5 100644 --- a/src/database/content_folder.rs +++ b/src/database/content_folder.rs @@ -46,6 +46,8 @@ pub enum ContentFolderError { PathTaken { path: String }, #[snafu(display("The Content Folder (Path: {path}) does not exist"))] NotFound { path: String }, + #[snafu(display("The content folder id is invalid: {id}"))] + IDInvalid { id: String }, #[snafu(display("Database error"))] DB { source: sea_orm::DbErr }, #[snafu(display("Failed to save the operation log"))] @@ -101,9 +103,11 @@ impl ContentFolderOperator { } } - /// Find one Content Folder by ID + /// Find one content folder by ID /// - /// Should not fail, unless SQLite was corrupted for some reason. + /// Fails if: + /// + /// - the requested ID does not exist pub async fn find_by_id(&self, id: i32) -> Result { let content_folder = Entity::find_by_id(id) .one(&self.state.database) @@ -118,6 +122,19 @@ impl ContentFolderOperator { } } + /// Find one content folder by stringy ID + /// + /// Fails if: + /// + /// - the requested ID does not exist + /// - the requested ID could not be parsed into an i32 + pub async fn find_by_id_str(&self, id: &str) -> Result { + let id: i32 = id + .parse() + .map_err(|_e| ContentFolderError::IDInvalid { id: id.to_string() })?; + self.find_by_id(id).await + } + /// Create a new content folder /// /// Fails if: diff --git a/src/database/operation.rs b/src/database/operation.rs index 492f8c4..666d8d1 100644 --- a/src/database/operation.rs +++ b/src/database/operation.rs @@ -38,7 +38,7 @@ pub enum Operation { impl std::fmt::Display for Operation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", &serde_json::to_string(self).unwrap()) + write!(f, "{}", serde_json::to_string(self).unwrap()) } } diff --git a/src/extractors/normalized_path/mod.rs b/src/extractors/normalized_path/mod.rs index 1dcdce1..32c1e78 100644 --- a/src/extractors/normalized_path/mod.rs +++ b/src/extractors/normalized_path/mod.rs @@ -61,7 +61,7 @@ impl AsRef for NormalizedPath { impl std::fmt::Display for NormalizedPath { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", &self.path) + write!(f, "{}", self.path) } } diff --git a/src/state/mod.rs b/src/state/mod.rs index 7fddd15..d5090b4 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -49,7 +49,7 @@ impl AppState { .context(InitAPISnafu)?; let sqlite_path = config.sqlite_path.clone(); - let database = Database::connect(format!("sqlite://{}?mode=rwc", &sqlite_path)) + let database = Database::connect(format!("sqlite://{}?mode=rwc", sqlite_path)) .await .context(SqliteSnafu)?;