From 744475dd1e533ececcfaa47ca426b99f3a181cf0 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 29 Jan 2022 20:53:57 +0100 Subject: [PATCH] Move tag and namespace retrieval to dao Signed-off-by: trivernis --- .../mediarepo-logic/src/dao/repo/mod.rs | 30 ++-------------- .../mediarepo-logic/src/dao/tag/mod.rs | 35 +++++++++++++++++-- .../mediarepo-socket/src/from_model.rs | 11 +++++- .../mediarepo-socket/src/namespaces/tags.rs | 6 ++-- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs index 2954366..e0713ab 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/repo/mod.rs @@ -4,25 +4,19 @@ use std::fmt::Debug; use std::iter::FromIterator; use std::path::PathBuf; - - use sea_orm::DatabaseConnection; - - - -use mediarepo_core::error::{RepoResult}; +use mediarepo_core::error::RepoResult; use mediarepo_core::fs::file_hash_store::FileHashStore; -use mediarepo_core::fs::thumbnail_store::{ThumbnailStore}; +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::{Counts, get_all_counts}; +use mediarepo_database::queries::analysis::{get_all_counts, Counts}; use crate::dao::{DaoContext, DaoProvider}; -use crate::file_metadata::FileMetadata; use crate::namespace::Namespace; use crate::tag::Tag; @@ -72,24 +66,6 @@ impl Repo { &self.db } - /// Returns all file metadata entries for the given file ids - #[tracing::instrument(level = "debug", skip(self))] - pub async fn get_file_metadata_for_ids(&self, ids: Vec) -> RepoResult> { - FileMetadata::all_by_ids(self.db.clone(), ids).await - } - - /// Returns all tags stored in the database - #[tracing::instrument(level = "debug", skip(self))] - pub async fn tags(&self) -> RepoResult> { - Tag::all(self.db.clone()).await - } - - /// Returns all namespaces stored in the database - #[tracing::instrument(level = "debug", skip(self))] - pub async fn namespaces(&self) -> RepoResult> { - Namespace::all(self.db.clone()).await - } - /// Converts a list of tag names to tag ids #[tracing::instrument(level = "debug", skip(self))] pub async fn tag_names_to_ids(&self, tags: Vec) -> RepoResult> { diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs index ac71383..2f3dfa2 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs @@ -1,12 +1,12 @@ use sea_orm::prelude::*; +use sea_orm::JoinType; use sea_orm::QuerySelect; -use sea_orm::{JoinType}; use mediarepo_core::error::RepoResult; use mediarepo_database::entities::{content_descriptor, content_descriptor_tag, namespace, tag}; use crate::dao::{DaoContext, DaoProvider}; -use crate::dto::TagDto; +use crate::dto::{NamespaceDto, TagDto}; pub mod mappings; @@ -25,6 +25,31 @@ impl TagDao { Self { ctx } } + #[tracing::instrument(level = "debug", skip(self))] + pub async fn all(&self) -> RepoResult> { + let tags = tag::Entity::find() + .find_also_related(namespace::Entity) + .all(&self.ctx.db) + .await? + .into_iter() + .map(map_tag_dto) + .collect(); + + Ok(tags) + } + + #[tracing::instrument(level = "debug", skip(self))] + pub async fn all_namespaces(&self) -> RepoResult> { + let namespaces = namespace::Entity::find() + .all(&self.ctx.db) + .await? + .into_iter() + .map(NamespaceDto::new) + .collect(); + + Ok(namespaces) + } + #[tracing::instrument(level = "debug", skip(self))] pub async fn tags_for_cd(&self, cd_id: i64) -> RepoResult> { let tags = tag::Entity::find() @@ -41,9 +66,13 @@ impl TagDao { .all(&self.ctx.db) .await? .into_iter() - .map(|(t, n)| TagDto::new(t, n)) + .map(map_tag_dto) .collect(); Ok(tags) } } + +fn map_tag_dto(result: (tag::Model, Option)) -> TagDto { + TagDto::new(result.0, result.1) +} diff --git a/mediarepo-daemon/mediarepo-socket/src/from_model.rs b/mediarepo-daemon/mediarepo-socket/src/from_model.rs index ec96095..eaa6e7e 100644 --- a/mediarepo-daemon/mediarepo-socket/src/from_model.rs +++ b/mediarepo-daemon/mediarepo-socket/src/from_model.rs @@ -3,7 +3,7 @@ use mediarepo_core::mediarepo_api::types::files::{ }; use mediarepo_core::mediarepo_api::types::tags::{NamespaceResponse, TagResponse}; use mediarepo_logic::dto::{ - FileDto, FileMetadataDto, FileStatus as FileStatusModel, TagDto, ThumbnailDto, + FileDto, FileMetadataDto, FileStatus as FileStatusModel, NamespaceDto, TagDto, ThumbnailDto, }; use mediarepo_logic::file_metadata::FileMetadata; use mediarepo_logic::namespace::Namespace; @@ -99,3 +99,12 @@ impl FromModel for NamespaceResponse { } } } + +impl FromModel for NamespaceResponse { + fn from_model(model: NamespaceDto) -> Self { + Self { + id: model.id(), + name: model.name().to_owned(), + } + } +} diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs index 269dcd4..1bfde4c 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/tags.rs @@ -36,7 +36,8 @@ impl TagsNamespace { async fn all_tags(ctx: &Context, _event: Event) -> IPCResult<()> { let repo = get_repo_from_context(ctx).await; let tags: Vec = repo - .tags() + .tag() + .all() .await? .into_iter() .map(TagResponse::from_model) @@ -51,7 +52,8 @@ impl TagsNamespace { async fn all_namespaces(ctx: &Context, _event: Event) -> IPCResult<()> { let repo = get_repo_from_context(ctx).await; let namespaces: Vec = repo - .namespaces() + .tag() + .all_namespaces() .await? .into_iter() .map(NamespaceResponse::from_model)