Fix preset creation

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

@ -0,0 +1,17 @@
PRAGMA foreign_keys= off;
ALTER TABLE sorting_preset_keys
RENAME TO _sorting_preset_keys_old;
CREATE TABLE sorting_preset_keys
(
preset_id INTEGER REFERENCES sorting_presets (id) ON DELETE CASCADE,
key_id INTEGER REFERENCES sort_keys (id),
key_index INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (preset_id, key_id, key_index)
);
INSERT INTO sorting_preset_keys SELECT * FROM _sorting_preset_keys_old;
DROP TABLE _sorting_preset_keys_old;
PRAGMA foreign_keys= on;

@ -9,6 +9,7 @@ pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
key_id: i32, key_id: i32,
#[sea_orm(primary_key)]
key_index: i32, key_index: i32,
} }

@ -4,7 +4,13 @@ use mediarepo_core::error::RepoResult;
use mediarepo_database::entities::{sort_key, sorting_preset, sorting_preset_key}; use mediarepo_database::entities::{sort_key, sorting_preset, sorting_preset_key};
use sea_orm::prelude::*; use sea_orm::prelude::*;
use sea_orm::ActiveValue::Set; use sea_orm::ActiveValue::Set;
use sea_orm::{Condition, ConnectionTrait, DatabaseTransaction, JoinType, QuerySelect}; use sea_orm::{
Condition, ConnectionTrait, DatabaseTransaction, DbBackend, FromQueryResult, JoinType,
QuerySelect, Statement,
};
#[allow(unused_imports)]
use sea_orm::TryGetableMany; // otherwise intellijrust hates on me
impl SortingPresetDao { impl SortingPresetDao {
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
@ -33,10 +39,17 @@ impl SortingPresetDao {
trx.commit().await?; trx.commit().await?;
return Ok(SortingPresetDto::new(model, keys)); return Ok(SortingPresetDto::new(model, keys));
} }
let preset_model = sorting_preset::ActiveModel {
..Default::default() // sea_orm currently doesn't support all-default-value inserts.
}; // TODOD: Replace after the change for default inserts has been merged
let preset_model = preset_model.insert(&trx).await?; let preset_model = sorting_preset::Model::find_by_statement(Statement::from_string(
DbBackend::Sqlite,
"INSERT INTO sorting_presets DEFAULT VALUES RETURNING *;".to_string(),
))
.one(&trx)
.await?
.expect("failed to insert new sorting preset");
let mapping_models = key_ids let mapping_models = key_ids
.into_iter() .into_iter()
.map(|(idx, key)| sorting_preset_key::ActiveModel { .map(|(idx, key)| sorting_preset_key::ActiveModel {
@ -45,9 +58,12 @@ impl SortingPresetDao {
key_index: Set(idx as i32), key_index: Set(idx as i32),
}) })
.collect::<Vec<sorting_preset_key::ActiveModel>>(); .collect::<Vec<sorting_preset_key::ActiveModel>>();
if !mapping_models.is_empty() {
sorting_preset_key::Entity::insert_many(mapping_models) sorting_preset_key::Entity::insert_many(mapping_models)
.exec(&trx) .exec(&trx)
.await?; .await?;
}
trx.commit().await?; trx.commit().await?;
Ok(SortingPresetDto::new(preset_model, keys)) Ok(SortingPresetDto::new(preset_model, keys))
@ -56,19 +72,21 @@ impl SortingPresetDao {
async fn add_keys( async fn add_keys(
trx: &DatabaseTransaction, trx: &DatabaseTransaction,
mut keys: Vec<AddSortKeyDto>, keys: Vec<AddSortKeyDto>,
) -> RepoResult<Vec<SortKeyDto>> { ) -> RepoResult<Vec<SortKeyDto>> {
let mut key_dtos = find_sort_keys(trx, &keys).await?; let mut key_dtos = find_sort_keys(trx, &keys).await?;
let mut insert_keys = keys.clone();
key_dtos.iter().for_each(|key| { key_dtos.iter().for_each(|key| {
keys.retain(|k| { insert_keys.retain(|k| {
k.ascending != key.ascending() k.ascending != key.ascending()
&& Some(k.key_type) != key.key_type() || k.key_type != key.key_type().unwrap()
&& key.value() != key.value() || !compare_opts_eq(key.value(), k.value.as_ref())
}) })
}); });
if !keys.is_empty() { if !insert_keys.is_empty() {
let active_models: Vec<sort_key::ActiveModel> = keys let active_models: Vec<sort_key::ActiveModel> = insert_keys
.iter() .iter()
.cloned() .cloned()
.map(|key| sort_key::ActiveModel { .map(|key| sort_key::ActiveModel {
@ -81,7 +99,7 @@ async fn add_keys(
sort_key::Entity::insert_many(active_models) sort_key::Entity::insert_many(active_models)
.exec(trx) .exec(trx)
.await?; .await?;
let mut new_keys = find_sort_keys(trx, &keys).await?; let mut new_keys = find_sort_keys(trx, &insert_keys).await?;
key_dtos.append(&mut new_keys); key_dtos.append(&mut new_keys);
} }
@ -91,9 +109,9 @@ async fn add_keys(
key_dtos key_dtos
.iter() .iter()
.find(|key| { .find(|key| {
k.ascending != key.ascending() k.ascending == key.ascending()
&& Some(k.key_type) != key.key_type() && k.key_type == key.key_type().unwrap()
&& key.value() != key.value() && compare_opts_eq(key.value(), k.value.as_ref())
}) })
.cloned() .cloned()
}) })
@ -144,3 +162,11 @@ fn create_mapping_condition(entry: (usize, i32)) -> Condition {
.add(sorting_preset_key::Column::KeyId.eq(entry.1)) .add(sorting_preset_key::Column::KeyId.eq(entry.1))
.add(sorting_preset_key::Column::KeyIndex.eq(entry.0 as i32)) .add(sorting_preset_key::Column::KeyIndex.eq(entry.0 as i32))
} }
fn compare_opts_eq<T: Eq>(opt1: Option<T>, opt2: Option<T>) -> bool {
if let (Some(opt1), Some(opt2)) = (&opt1, &opt2) {
opt1 == opt2
} else {
opt1.is_none() && opt2.is_none()
}
}

@ -5,7 +5,7 @@ use crate::dto::{SortKeyDto, SortingPresetDto};
use mediarepo_core::error::RepoResult; use mediarepo_core::error::RepoResult;
use mediarepo_database::entities::{sort_key, sorting_preset, sorting_preset_key}; use mediarepo_database::entities::{sort_key, sorting_preset, sorting_preset_key};
use sea_orm::prelude::*; use sea_orm::prelude::*;
use sea_orm::{JoinType, QueryOrder, QuerySelect}; use sea_orm::QueryOrder;
dao_provider!(SortingPresetDao); dao_provider!(SortingPresetDao);
@ -14,10 +14,6 @@ impl SortingPresetDao {
pub async fn all(&self) -> RepoResult<Vec<SortingPresetDto>> { pub async fn all(&self) -> RepoResult<Vec<SortingPresetDto>> {
let presets = sorting_preset::Entity::find() let presets = sorting_preset::Entity::find()
.find_with_related(sort_key::Entity) .find_with_related(sort_key::Entity)
.join(
JoinType::InnerJoin,
sorting_preset_key::Relation::SortingKey.def(),
)
.order_by_asc(sorting_preset_key::Column::KeyIndex) .order_by_asc(sorting_preset_key::Column::KeyIndex)
.all(&self.ctx.db) .all(&self.ctx.db)
.await? .await?

@ -291,10 +291,8 @@ impl FilesNamespace {
let found_thumbnail = thumbnails.into_iter().find(|thumb| { let found_thumbnail = thumbnails.into_iter().find(|thumb| {
let Dimensions { height, width } = thumb.size(); let Dimensions { height, width } = thumb.size();
*height >= min_size.0 (*height <= max_size.0 && *width <= max_size.1)
&& *height <= max_size.0 && (*width >= min_size.1 || *height >= min_size.0)
&& *width >= min_size.1
&& *width <= max_size.1
}); });
let thumbnail = if let Some(thumbnail) = found_thumbnail { let thumbnail = if let Some(thumbnail) = found_thumbnail {

@ -65,6 +65,8 @@ impl PresetsNamespace {
let id = event.payload::<i32>()?; let id = event.payload::<i32>()?;
let repo = get_repo_from_context(ctx).await; let repo = get_repo_from_context(ctx).await;
repo.sorting_preset().delete(id).await?; repo.sorting_preset().delete(id).await?;
ctx.emit_to(Self::name(), "delete_sorting_preset", ())
.await?;
Ok(()) Ok(())
} }

Loading…
Cancel
Save