Move tag and namespace retrieval to dao

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/5/head
trivernis 3 years ago
parent acdad7ac7e
commit 744475dd1e
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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<i64>) -> RepoResult<Vec<FileMetadata>> {
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<Vec<Tag>> {
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<Vec<Namespace>> {
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<String>) -> RepoResult<HashMap<String, i64>> {

@ -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<Vec<TagDto>> {
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<Vec<NamespaceDto>> {
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<Vec<TagDto>> {
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<namespace::Model>)) -> TagDto {
TagDto::new(result.0, result.1)
}

@ -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<Namespace> for NamespaceResponse {
}
}
}
impl FromModel<NamespaceDto> for NamespaceResponse {
fn from_model(model: NamespaceDto) -> Self {
Self {
id: model.id(),
name: model.name().to_owned(),
}
}
}

@ -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<TagResponse> = 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<NamespaceResponse> = repo
.namespaces()
.tag()
.all_namespaces()
.await?
.into_iter()
.map(NamespaceResponse::from_model)

Loading…
Cancel
Save