Add sorting of namespaces by numeric value if it can be parsed

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent e0618b5781
commit d704cc91fa

@ -3,6 +3,7 @@ use sea_orm::{Database, DatabaseConnection};
use sqlx::migrate::MigrateDatabase; use sqlx::migrate::MigrateDatabase;
pub mod entities; pub mod entities;
pub mod queries;
/// Connects to the database, runs migrations and returns the RepoDatabase wrapper type /// Connects to the database, runs migrations and returns the RepoDatabase wrapper type
pub async fn get_database<S: AsRef<str>>(uri: S) -> RepoDatabaseResult<DatabaseConnection> { pub async fn get_database<S: AsRef<str>>(uri: S) -> RepoDatabaseResult<DatabaseConnection> {

@ -0,0 +1,43 @@
use crate::entities::hash;
use crate::entities::hash_tag;
use crate::entities::namespace;
use crate::entities::tag;
use sea_orm::prelude::*;
use sea_orm::sea_query::Query;
use sea_orm::{DatabaseConnection, JoinType};
use std::collections::HashMap;
/*
pub async fn get_hashes_with_namespaced_tags(
db: DatabaseConnection,
hash_ids: Vec<i64>,
) -> HashMap<i64, HashMap<String, String>> {
Query::select()
.expr(hash_tag::Column::HashId)
.expr(tag::Column::Name)
.expr(namespace::Column::Name)
.from(tag::Entity)
.join(
JoinType::LeftJoin,
hash_tag::Entity,
hash_tag::Column::TagId.eq(tag::Column::Id),
)
.join(
JoinType::InnerJoin,
namespace::Entity,
tag::Column::NamespaceId.eq(namespace::Column::Id),
)
.build(&db)
.await?;
let tags: Vec<(tag::Model, Option<namespace::Model>)> = tag::Entity::find()
.find_also_related(namespace::Entity)
.join(JoinType::LeftJoin, hash_tag::Relation::Tag.def().rev())
.join(JoinType::InnerJoin, hash_tag::Relation::Hash.def())
.filter(hash::Column::Id.eq(self.hash.id))
.all(&self.db)
.await?;
let tags = tags
.into_iter()
.map(|(tag, namespace)| Tag::new(self.db.clone(), tag, namespace))
.collect();
}
*/

@ -201,12 +201,22 @@ fn compare_files(
expression: &Vec<SortKey>, expression: &Vec<SortKey>,
) -> Ordering { ) -> Ordering {
let cmp_date = compare::natural(); let cmp_date = compare::natural();
for sort_key in expression { for sort_key in expression {
let ordering = match sort_key { let ordering = match sort_key {
SortKey::Namespace(namespace) => adjust_for_dir( SortKey::Namespace(namespace) => {
compare_opts(nsp_a.get(&namespace.tag), nsp_b.get(&namespace.tag)), let tag_a = nsp_a.get(&namespace.tag);
&namespace.direction, let tag_b = nsp_b.get(&namespace.tag);
),
if let (Some(a), Some(b)) = (
tag_a.and_then(|a| a.parse::<f32>().ok()),
tag_b.and_then(|b| b.parse::<f32>().ok()),
) {
adjust_for_dir(compare_f32(a, b), &namespace.direction)
} else {
adjust_for_dir(compare_opts(tag_a, tag_b), &namespace.direction)
}
}
SortKey::FileName(direction) => adjust_for_dir( SortKey::FileName(direction) => adjust_for_dir(
compare_opts(file_a.name().clone(), file_b.name().clone()), compare_opts(file_a.name().clone(), file_b.name().clone()),
direction, direction,
@ -266,6 +276,16 @@ fn compare_opts<T: Ord + Sized>(opt_a: Option<T>, opt_b: Option<T>) -> Ordering
} }
} }
fn compare_f32(a: f32, b: f32) -> Ordering {
if a > b {
Ordering::Greater
} else if b > a {
Ordering::Less
} else {
Ordering::Equal
}
}
fn adjust_for_dir(ordering: Ordering, direction: &SortDirection) -> Ordering { fn adjust_for_dir(ordering: Ordering, direction: &SortDirection) -> Ordering {
if *direction == SortDirection::Descending { if *direction == SortDirection::Descending {
ordering.reverse() ordering.reverse()

Loading…
Cancel
Save