diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs index a329f2a..89945af 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs @@ -1,7 +1,7 @@ -use std::collections::{HashMap, HashSet}; + use std::fmt::Debug; -use std::iter::FromIterator; + use std::path::PathBuf; use sea_orm::DatabaseConnection; @@ -9,14 +9,14 @@ use sea_orm::DatabaseConnection; use mediarepo_core::error::RepoResult; use mediarepo_core::fs::file_hash_store::FileHashStore; use mediarepo_core::fs::thumbnail_store::ThumbnailStore; -use mediarepo_core::itertools::Itertools; + use mediarepo_core::utils::parse_namespace_and_tag; use mediarepo_database::get_database; use mediarepo_database::queries::analysis::{get_all_counts, Counts}; -use crate::dao::tag::by_name::TagByNameQuery; + use crate::dao::{DaoContext, DaoProvider}; use crate::namespace::Namespace; use crate::tag::Tag; diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/tag/add.rs b/mediarepo-daemon/mediarepo-logic/src/dao/tag/add.rs index f52da09..602a7ea 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/tag/add.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/tag/add.rs @@ -21,8 +21,13 @@ impl TagDao { let existing_tag_map: HashMap = HashMap::from_iter(existing_tags.into_iter().map(|t| (t.normalized_name(), t))); - let namespace_map = add_or_get_all_namespaces(&trx, namespaces).await?; tags.retain(|dto| !existing_tag_map.contains_key(&dto.normalized_name())); + let namespace_map = add_or_get_all_namespaces(&trx, namespaces).await?; + + if tags.is_empty() { + return Ok(existing_tag_map.into_values().collect()); + } + let tag_models: Vec = tags .iter() .map(|t| tag::ActiveModel { @@ -38,7 +43,7 @@ impl TagDao { tag::Entity::insert_many(tag_models).exec(&trx).await?; let mut tag_dtos = tags_by_name(&trx, tags).await?; trx.commit().await?; - tag_dtos.append(&mut existing_tag_map.into_iter().map(|(_, dto)| dto).collect()); + tag_dtos.append(&mut existing_tag_map.into_values().collect()); Ok(tag_dtos) } @@ -48,6 +53,9 @@ async fn add_or_get_all_namespaces( trx: &DatabaseTransaction, mut namespaces: Vec, ) -> RepoResult> { + if namespaces.is_empty() { + return Ok(HashMap::with_capacity(0)); + } let existing_namespaces = namespaces_by_name(trx, namespaces.clone()).await?; let mut namespace_map = HashMap::from_iter( existing_namespaces @@ -58,6 +66,9 @@ async fn add_or_get_all_namespaces( return Ok(namespace_map); } namespaces.retain(|nsp| !namespace_map.contains_key(nsp)); + if namespaces.is_empty() { + return Ok(namespace_map); + } let namespace_models: Vec = namespaces .iter() .map(|nsp| namespace::ActiveModel { @@ -81,6 +92,9 @@ async fn namespaces_by_name( trx: &DatabaseTransaction, names: Vec, ) -> RepoResult> { + if names.is_empty() { + return Ok(vec![]); + } let namespaces: Vec = namespace::Entity::find() .filter(namespace::Column::Name.is_in(names)) .all(trx) @@ -93,6 +107,9 @@ async fn namespaces_by_name( } async fn tags_by_name(trx: &DatabaseTransaction, tags: Vec) -> RepoResult> { + if tags.is_empty() { + return Ok(vec![]); + } let condition = tags .into_iter() .map(build_tag_condition) diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs index 6e08e01..1403456 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use std::iter::FromIterator; use mediarepo_core::error::RepoResult; -use mediarepo_core::mediarepo_api::types::filtering::TagQuery; + use mediarepo_core::utils::parse_namespace_and_tag; use mediarepo_database::entities::{content_descriptor, content_descriptor_tag, namespace, tag};