Updated routes to use filters/error handling
This commit is contained in:
@@ -13,13 +13,48 @@ pub struct QuerySpell {
|
||||
pub data: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct QueryFilters {
|
||||
pub by_name: Option<String>,
|
||||
pub by_schools: Option<Vec<SchoolType>>,
|
||||
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>,
|
||||
}
|
||||
|
||||
impl Default for QueryFilters {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
by_name: None,
|
||||
by_schools: None,
|
||||
by_levels: None,
|
||||
by_ritual: None,
|
||||
by_concentration: None,
|
||||
by_classes: None,
|
||||
by_damage_inflict: None,
|
||||
by_damage_resist: None,
|
||||
by_conditions: None,
|
||||
by_saving_throw: None,
|
||||
by_attack_type: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl QuerySpell {
|
||||
pub fn get_all(limit: i32, page: i32) -> Result<Vec<Self>, ServiceError> {
|
||||
pub fn get_all(filters: &QueryFilters, limit: i32, page: i32) -> Result<Vec<Self>, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let mut query = spells::table
|
||||
.limit(limit as i64)
|
||||
.into_boxed();
|
||||
let mut query = spells::table.limit(limit as i64).into_boxed();
|
||||
query = query.filter(spells::id.gt(std::cmp::max(0, page - 1) * limit));
|
||||
if let Some(name) = filters.by_name.to_owned() {
|
||||
query = query.filter(spells::name.ilike(format!("%{}%", name)));
|
||||
}
|
||||
|
||||
let spells = query.load::<QuerySpell>(&mut conn)?;
|
||||
Ok(spells)
|
||||
}
|
||||
@@ -32,9 +67,14 @@ impl QuerySpell {
|
||||
Ok(spell)
|
||||
}
|
||||
|
||||
pub fn get_count() -> Result<i64, ServiceError> {
|
||||
pub fn get_count(filters: &QueryFilters) -> Result<i64, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let count = spells::table.count().get_result(&mut conn)?;
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user