Updated query parameters for spells
This commit is contained in:
@@ -3,29 +3,39 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{db::{schema::spells::{self}, classes::AbilityType, conditions::ConditionType}, error_handler::ServiceError};
|
||||
|
||||
use super::{SchoolType, CastingTime, CastingType, SpellAttackType, SpellDamageType, Range, Area, Components, Duration, Source, Description};
|
||||
use super::{SchoolType, CastingTime, CastingType, SpellAttackType, SpellDamageType, Range, Area, Components, Duration, Source, Description, DurationType};
|
||||
|
||||
#[derive(Queryable, QueryableByName)]
|
||||
#[diesel(table_name = spells)]
|
||||
pub struct QuerySpell {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub school: String,
|
||||
pub level: i32,
|
||||
pub ritual: bool,
|
||||
pub concentration: bool,
|
||||
pub classes: Vec<String>,
|
||||
pub damage_inflict: Vec<String>,
|
||||
pub damage_resist: Vec<String>,
|
||||
pub conditions: Vec<String>,
|
||||
pub saving_throw: Vec<String>,
|
||||
pub attack_type: Option<String>,
|
||||
pub data: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct QueryFilters {
|
||||
pub by_name: Option<String>,
|
||||
pub by_schools: Option<Vec<SchoolType>>,
|
||||
pub by_schools: Option<Vec<String>>,
|
||||
pub by_levels: Option<Vec<i32>>,
|
||||
pub by_ritual: Option<bool>,
|
||||
pub by_concentration: Option<bool>,
|
||||
pub by_classes: Option<Vec<String>>,
|
||||
pub by_damage_inflict: Option<Vec<SpellDamageType>>,
|
||||
pub by_damage_resist: Option<Vec<SpellDamageType>>,
|
||||
pub by_conditions: Option<Vec<ConditionType>>,
|
||||
pub by_saving_throw: Option<Vec<AbilityType>>,
|
||||
pub by_attack_type: Option<SpellAttackType>,
|
||||
pub by_damage_inflict: Option<Vec<String>>,
|
||||
pub by_damage_resist: Option<Vec<String>>,
|
||||
pub by_conditions: Option<Vec<String>>,
|
||||
pub by_saving_throw: Option<Vec<String>>,
|
||||
pub by_attack_type: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for QueryFilters {
|
||||
@@ -53,15 +63,85 @@ impl QuerySpell {
|
||||
// Limit query to page and limit
|
||||
let offset = (page - 1) * limit;
|
||||
query = query.offset(offset as i64);
|
||||
// Apply filters
|
||||
if let Some(name) = filters.by_name.to_owned() {
|
||||
if let Some(name) = &filters.by_name {
|
||||
query = query.filter(spells::name.ilike(format!("%{}%", name)));
|
||||
}
|
||||
|
||||
if let Some(schools) = &filters.by_schools {
|
||||
query = query.filter(spells::school.eq_any(schools.iter().map(|school| school.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(levels) = &filters.by_levels {
|
||||
query = query.filter(spells::level.eq_any(levels));
|
||||
}
|
||||
if let Some(ritual) = filters.by_ritual {
|
||||
query = query.filter(spells::ritual.eq(ritual));
|
||||
}
|
||||
if let Some(concentration) = filters.by_concentration {
|
||||
query = query.filter(spells::concentration.eq(concentration));
|
||||
}
|
||||
if let Some(classes) = &filters.by_classes {
|
||||
query = query.filter(spells::classes.overlaps_with(classes));
|
||||
}
|
||||
if let Some(damage_inflict) = &filters.by_damage_inflict {
|
||||
query = query.filter(spells::damage_inflict.overlaps_with(damage_inflict.iter().map(|damage_inflict| damage_inflict.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(damage_resist) = &filters.by_damage_resist {
|
||||
query = query.filter(spells::damage_resist.overlaps_with(damage_resist.iter().map(|damage_resist| damage_resist.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(conditions) = &filters.by_conditions {
|
||||
query = query.filter(spells::conditions.overlaps_with(conditions.iter().map(|condition| condition.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(saving_throw) = &filters.by_saving_throw {
|
||||
query = query.filter(spells::saving_throw.overlaps_with(saving_throw.iter().map(|saving_throw| saving_throw.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(attack_type) = &filters.by_attack_type {
|
||||
query = query.filter(spells::attack_type.eq(attack_type.to_string()));
|
||||
}
|
||||
|
||||
let spells = query.load::<QuerySpell>(&mut conn)?;
|
||||
Ok(spells)
|
||||
}
|
||||
|
||||
pub fn get_count(filters: &QueryFilters) -> Result<i64, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let mut query = spells::table.count().into_boxed();
|
||||
if let Some(name) = &filters.by_name {
|
||||
query = query.filter(spells::name.ilike(format!("%{}%", name)));
|
||||
}
|
||||
if let Some(schools) = &filters.by_schools {
|
||||
query = query.filter(spells::school.eq_any(schools.iter().map(|school| school.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(levels) = &filters.by_levels {
|
||||
query = query.filter(spells::level.eq_any(levels));
|
||||
}
|
||||
if let Some(ritual) = filters.by_ritual {
|
||||
query = query.filter(spells::ritual.eq(ritual));
|
||||
}
|
||||
if let Some(concentration) = filters.by_concentration {
|
||||
query = query.filter(spells::concentration.eq(concentration));
|
||||
}
|
||||
if let Some(classes) = &filters.by_classes {
|
||||
query = query.filter(spells::classes.overlaps_with(classes));
|
||||
}
|
||||
if let Some(damage_inflict) = &filters.by_damage_inflict {
|
||||
query = query.filter(spells::damage_inflict.overlaps_with(damage_inflict.iter().map(|damage_inflict| damage_inflict.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(damage_resist) = &filters.by_damage_resist {
|
||||
query = query.filter(spells::damage_resist.overlaps_with(damage_resist.iter().map(|damage_resist| damage_resist.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(conditions) = &filters.by_conditions {
|
||||
query = query.filter(spells::conditions.overlaps_with(conditions.iter().map(|condition| condition.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(saving_throw) = &filters.by_saving_throw {
|
||||
query = query.filter(spells::saving_throw.overlaps_with(saving_throw.iter().map(|saving_throw| saving_throw.to_string()).collect::<Vec<String>>()));
|
||||
}
|
||||
if let Some(attack_type) = &filters.by_attack_type {
|
||||
query = query.filter(spells::attack_type.eq(attack_type.to_string()));
|
||||
}
|
||||
|
||||
let count = query.get_result(&mut conn)?;
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
pub fn get_by_id(id: i32) -> Result<Self, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let spell = spells::table
|
||||
@@ -70,17 +150,6 @@ impl QuerySpell {
|
||||
Ok(spell)
|
||||
}
|
||||
|
||||
pub fn get_count(filters: &QueryFilters) -> Result<i64, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let mut query = spells::table.count().into_boxed();
|
||||
if let Some(name) = filters.by_name.to_owned() {
|
||||
query = query.filter(spells::name.ilike(format!("%{}%", name)));
|
||||
}
|
||||
|
||||
let count = query.get_result(&mut conn)?;
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
pub fn delete(id: i32) -> Result<Self, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let spell = diesel::delete(spells::table.filter(spells::id.eq(id))).get_result(&mut conn)?;
|
||||
@@ -92,6 +161,16 @@ impl QuerySpell {
|
||||
#[diesel(table_name = spells)]
|
||||
pub struct InsertSpell {
|
||||
pub name: String,
|
||||
pub school: String,
|
||||
pub level: i32,
|
||||
pub ritual: bool,
|
||||
pub concentration: bool,
|
||||
pub classes: Vec<String>,
|
||||
pub damage_inflict: Vec<String>,
|
||||
pub damage_resist: Vec<String>,
|
||||
pub conditions: Vec<String>,
|
||||
pub saving_throw: Vec<String>,
|
||||
pub attack_type: Option<String>,
|
||||
pub data: serde_json::Value
|
||||
}
|
||||
|
||||
@@ -174,13 +253,38 @@ impl Into<InsertSpell> for Spell {
|
||||
fn into(self) -> InsertSpell {
|
||||
return InsertSpell {
|
||||
name: self.name.to_string(),
|
||||
school: self.school.to_string(),
|
||||
level: self.level,
|
||||
ritual: self.ritual,
|
||||
concentration: self.durations.iter().any(|duration| match duration.duration_type {
|
||||
DurationType::Concentration => true,
|
||||
_ => false
|
||||
}),
|
||||
classes: self.classes.iter().map(|class| class.to_string()).collect::<Vec<String>>(),
|
||||
damage_inflict: match &self.damage_inflict {
|
||||
Some(damage_inflict) => damage_inflict.iter().map(|damage_inflict| damage_inflict.to_string()).collect(),
|
||||
None => vec![]
|
||||
},
|
||||
damage_resist: match &self.damage_resist {
|
||||
Some(damage_resist) => damage_resist.iter().map(|damage_resist| damage_resist.to_string()).collect(),
|
||||
None => vec![]
|
||||
},
|
||||
conditions: match &self.conditions {
|
||||
Some(conditions) => conditions.iter().map(|condition| condition.to_string()).collect(),
|
||||
None => vec![]
|
||||
},
|
||||
saving_throw: match &self.saving_throw {
|
||||
Some(saving_throw) => saving_throw.iter().map(|saving_throw| saving_throw.to_string()).collect(),
|
||||
None => vec![]
|
||||
},
|
||||
attack_type: self.attack_type.as_ref().map(|attack_type| attack_type.to_string()),
|
||||
data: match serde_json::to_value(&self) {
|
||||
Ok(data) => data,
|
||||
Err(err) => {
|
||||
log::error!("Failed to serialize spell description: {}", err);
|
||||
log::error!("Failed to serialize spell: {}", err);
|
||||
serde_json::Value::Null
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user