Add sorting preset dto and function to retrieve all presets

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/12/head
trivernis 2 years ago
parent ed68b76a42
commit 3e768d7c7c
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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<Vec<FileDto>> {
let files = file::Entity::find()

@ -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);

@ -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<T: Into<sea_orm::Value>>(opt: Option<T>) -> ActiveValue<T> {

@ -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<Vec<SortingPresetDto>> {
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<sort_key::Model>),
) -> SortingPresetDto {
SortingPresetDto::new(entry.0, entry.1.into_iter().map(SortKeyDto::new).collect())
}

@ -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<Vec<TagDto>> {
let tags = tag::Entity::find()

@ -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;

@ -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<SortKeyDto>,
}
impl SortingPresetDto {
pub fn new(model: sorting_preset::Model, keys: Vec<SortKeyDto>) -> Self {
Self { model, keys }
}
pub fn id(&self) -> i32 {
self.model.id
}
pub fn keys(&self) -> &Vec<SortKeyDto> {
&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()
}
}
Loading…
Cancel
Save