From 3e768d7c7c4fb8b8f7190bf4bf6c0ced494baace Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 5 Feb 2022 11:24:54 +0100 Subject: [PATCH] Add sorting preset dto and function to retrieve all presets Signed-off-by: trivernis --- .../mediarepo-logic/src/dao/file/mod.rs | 16 +----- .../mediarepo-logic/src/dao/job/mod.rs | 20 ++------ .../mediarepo-logic/src/dao/mod.rs | 29 +++++++++++ .../src/dao/sorting_preset/mod.rs | 34 +++++++++++++ .../mediarepo-logic/src/dao/tag/mod.rs | 16 +----- .../mediarepo-logic/src/dto/mod.rs | 5 +- .../mediarepo-logic/src/dto/sorting_preset.rs | 49 +++++++++++++++++++ 7 files changed, 122 insertions(+), 47 deletions(-) create mode 100644 mediarepo-daemon/mediarepo-logic/src/dao/sorting_preset/mod.rs create mode 100644 mediarepo-daemon/mediarepo-logic/src/dto/sorting_preset.rs diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/file/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/file/mod.rs index 210cf50..93f012c 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/file/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/file/mod.rs @@ -1,10 +1,10 @@ use sea_orm::prelude::*; use tokio::io::AsyncReadExt; +use crate::dao_provider; use mediarepo_core::error::RepoResult; use mediarepo_database::entities::{content_descriptor, file, file_metadata}; -use crate::dao::{DaoContext, DaoProvider}; use crate::dto::{FileDto, FileMetadataDto, ThumbnailDto}; pub mod add; @@ -12,21 +12,9 @@ pub mod delete; pub mod find; pub mod update; -pub struct FileDao { - ctx: DaoContext, -} - -impl DaoProvider for FileDao { - fn dao_ctx(&self) -> DaoContext { - self.ctx.clone() - } -} +dao_provider!(FileDao); impl FileDao { - pub fn new(ctx: DaoContext) -> Self { - Self { ctx } - } - #[tracing::instrument(level = "debug", skip(self))] pub async fn all(&self) -> RepoResult> { let files = file::Entity::find() diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/job/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/job/mod.rs index c47b28a..16bace3 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/job/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/job/mod.rs @@ -1,20 +1,6 @@ +use crate::dao_provider; + pub mod migrate_content_descriptors; pub mod sqlite_operations; -use crate::dao::{DaoContext, DaoProvider}; - -pub struct JobDao { - ctx: DaoContext, -} - -impl DaoProvider for JobDao { - fn dao_ctx(&self) -> DaoContext { - self.ctx.clone() - } -} - -impl JobDao { - pub fn new(ctx: DaoContext) -> JobDao { - Self { ctx } - } -} +dao_provider!(JobDao); diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/mod.rs index f3cf620..84c5964 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/mod.rs @@ -5,13 +5,38 @@ use mediarepo_core::fs::thumbnail_store::ThumbnailStore; use crate::dao::file::FileDao; use crate::dao::job::JobDao; +use crate::dao::sorting_preset::SortingPresetDao; use crate::dao::tag::TagDao; pub mod file; pub mod job; pub mod repo; +pub mod sorting_preset; pub mod tag; +#[macro_export] +macro_rules! dao_provider { + ($name:ident) => { + use crate::dao::{DaoContext, DaoProvider}; + + pub struct $name { + ctx: DaoContext, + } + + impl DaoProvider for $name { + fn dao_ctx(&self) -> DaoContext { + self.ctx.clone() + } + } + + impl $name { + pub fn new(ctx: DaoContext) -> Self { + Self { ctx } + } + } + }; +} + #[derive(Clone)] pub struct DaoContext { pub db: DatabaseConnection, @@ -33,6 +58,10 @@ pub trait DaoProvider { fn job(&self) -> JobDao { JobDao::new(self.dao_ctx()) } + + fn sorting_preset(&self) -> SortingPresetDao { + SortingPresetDao::new(self.dao_ctx()) + } } fn opt_to_active_val>(opt: Option) -> ActiveValue { diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/sorting_preset/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/sorting_preset/mod.rs new file mode 100644 index 0000000..eb8f241 --- /dev/null +++ b/mediarepo-daemon/mediarepo-logic/src/dao/sorting_preset/mod.rs @@ -0,0 +1,34 @@ +use crate::dao_provider; +use crate::dto::{SortKeyDto, SortingPresetDto}; +use mediarepo_core::error::RepoResult; +use mediarepo_database::entities::{sort_key, sorting_preset, sorting_preset_key}; +use sea_orm::prelude::*; +use sea_orm::{JoinType, QueryOrder, QuerySelect}; + +dao_provider!(SortingPresetDao); + +impl SortingPresetDao { + #[tracing::instrument(level = "debug", skip(self))] + pub async fn all(&self) -> RepoResult> { + let presets = sorting_preset::Entity::find() + .find_with_related(sort_key::Entity) + .join( + JoinType::InnerJoin, + sorting_preset_key::Relation::SortingKey.def(), + ) + .order_by_asc(sorting_preset_key::Column::KeyIndex) + .all(&self.ctx.db) + .await? + .into_iter() + .map(map_sorting_preset_dto) + .collect(); + + Ok(presets) + } +} + +fn map_sorting_preset_dto( + entry: (sorting_preset::Model, Vec), +) -> SortingPresetDto { + SortingPresetDto::new(entry.0, entry.1.into_iter().map(SortKeyDto::new).collect()) +} diff --git a/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs index 7f38e23..c931f10 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dao/tag/mod.rs @@ -10,7 +10,7 @@ use mediarepo_core::utils::parse_namespace_and_tag; use mediarepo_database::entities::{content_descriptor, content_descriptor_tag, namespace, tag}; use crate::dao::tag::by_name::TagByNameQuery; -use crate::dao::{DaoContext, DaoProvider}; +use crate::dao_provider; use crate::dto::{NamespaceDto, TagDto}; pub mod add; @@ -19,21 +19,9 @@ pub mod by_name; pub mod cdids_with_namespaced_tags; pub mod mappings; -pub struct TagDao { - ctx: DaoContext, -} - -impl DaoProvider for TagDao { - fn dao_ctx(&self) -> DaoContext { - self.ctx.clone() - } -} +dao_provider!(TagDao); impl TagDao { - pub fn new(ctx: DaoContext) -> Self { - Self { ctx } - } - #[tracing::instrument(level = "debug", skip(self))] pub async fn all(&self) -> RepoResult> { let tags = tag::Entity::find() diff --git a/mediarepo-daemon/mediarepo-logic/src/dto/mod.rs b/mediarepo-daemon/mediarepo-logic/src/dto/mod.rs index 0d7a962..2444214 100644 --- a/mediarepo-daemon/mediarepo-logic/src/dto/mod.rs +++ b/mediarepo-daemon/mediarepo-logic/src/dto/mod.rs @@ -1,12 +1,13 @@ pub use file::*; pub use file_metadata::*; pub use namespace::*; +pub use sorting_preset::*; pub use tag::*; pub use thumbnail::*; mod file; mod file_metadata; -mod tag; mod namespace; +mod sorting_preset; +mod tag; mod thumbnail; - diff --git a/mediarepo-daemon/mediarepo-logic/src/dto/sorting_preset.rs b/mediarepo-daemon/mediarepo-logic/src/dto/sorting_preset.rs new file mode 100644 index 0000000..462d904 --- /dev/null +++ b/mediarepo-daemon/mediarepo-logic/src/dto/sorting_preset.rs @@ -0,0 +1,49 @@ +use mediarepo_database::entities::sort_key; +use mediarepo_database::entities::sorting_preset; + +#[derive(Clone, Debug)] +pub struct SortingPresetDto { + model: sorting_preset::Model, + keys: Vec, +} + +impl SortingPresetDto { + pub fn new(model: sorting_preset::Model, keys: Vec) -> Self { + Self { model, keys } + } + + pub fn id(&self) -> i32 { + self.model.id + } + + pub fn keys(&self) -> &Vec { + &self.keys + } +} + +#[derive(Clone, Debug)] +pub struct SortKeyDto { + model: sort_key::Model, +} + +impl SortKeyDto { + pub fn new(model: sort_key::Model) -> Self { + Self { model } + } + + pub fn id(&self) -> i32 { + self.model.id + } + + pub fn key_type(&self) -> i32 { + self.model.key_type + } + + pub fn ascending(&self) -> bool { + self.model.ascending + } + + pub fn value(&self) -> Option<&String> { + self.model.value.as_ref() + } +}