Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/database/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Model {
#[sea_orm(unique)]
pub path: NormalizedPathAbsolute,
#[sea_orm(has_many)]
pub content_folders: HasMany<super::content_folder::Entity>,
pub content_folders: HasMany<content_folder::Entity>,
}

#[async_trait::async_trait]
Expand All @@ -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"))]
Expand Down Expand Up @@ -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<Model, CategoryError> {
let category = Entity::find_by_id(id)
.one(&self.state.database)
Expand All @@ -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<Model, CategoryError> {
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.
Expand Down
21 changes: 19 additions & 2 deletions src/database/content_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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<Model, ContentFolderError> {
let content_folder = Entity::find_by_id(id)
.one(&self.state.database)
Expand All @@ -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<Model, ContentFolderError> {
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:
Expand Down
2 changes: 1 addition & 1 deletion src/database/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/extractors/normalized_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl AsRef<Path> 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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
Loading