Spells endpoints
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use diesel::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::db::schema::spells;
|
||||
use crate::{db::schema::spells, error_handler::ServiceError};
|
||||
|
||||
#[derive(Queryable, QueryableByName)]
|
||||
#[diesel(table_name = spells)]
|
||||
@@ -24,6 +24,38 @@ pub struct QuerySpell {
|
||||
pub description: String
|
||||
}
|
||||
|
||||
impl QuerySpell {
|
||||
pub fn get_all(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();
|
||||
query = query.filter(spells::id.gt(std::cmp::max(0, page - 1) * limit));
|
||||
let spells = query.load::<QuerySpell>(&mut conn)?;
|
||||
Ok(spells)
|
||||
}
|
||||
|
||||
pub fn get_by_id(id: i32) -> Result<Self, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let spell = spells::table
|
||||
.filter(spells::id.eq(id))
|
||||
.first::<QuerySpell>(&mut conn)?;
|
||||
Ok(spell)
|
||||
}
|
||||
|
||||
pub fn get_count() -> Result<i64, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let count = spells::table.count().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)?;
|
||||
Ok(spell)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Insertable, AsChangeset)]
|
||||
#[diesel(table_name = spells)]
|
||||
pub struct InsertSpell {
|
||||
@@ -44,6 +76,20 @@ pub struct InsertSpell {
|
||||
pub description: String
|
||||
}
|
||||
|
||||
impl InsertSpell {
|
||||
pub fn insert(spell: Self) -> Result<QuerySpell, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let spell = diesel::insert_into(spells::table).values(spell).get_result(&mut conn)?;
|
||||
Ok(spell)
|
||||
}
|
||||
|
||||
pub fn update(id: i32, spell: Self) -> Result<QuerySpell, ServiceError> {
|
||||
let mut conn = crate::db::connection()?;
|
||||
let spell = diesel::update(spells::table.filter(spells::id.eq(id))).set(spell).get_result(&mut conn)?;
|
||||
Ok(spell)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Spell {
|
||||
pub name: String,
|
||||
@@ -56,6 +102,7 @@ pub struct Spell {
|
||||
pub duration: String,
|
||||
pub classes: Vec<String>,
|
||||
pub sources: Vec<String>,
|
||||
pub tags: Vec<String>,
|
||||
pub description: String
|
||||
}
|
||||
|
||||
@@ -67,30 +114,8 @@ pub struct Components {
|
||||
pub materials_needed: Option<String>
|
||||
}
|
||||
|
||||
impl Spell {
|
||||
/// Convert spell to insertable struct
|
||||
pub fn to_insert(self) -> InsertSpell {
|
||||
return InsertSpell {
|
||||
name: self.name,
|
||||
school: self.school,
|
||||
level: self.level,
|
||||
ritual: self.ritual,
|
||||
casting_time: self.casting_time,
|
||||
range: self.range,
|
||||
components_verbal: self.components.verbal,
|
||||
components_somatic: self.components.somatic,
|
||||
components_material: self.components.material,
|
||||
components_materials_needed: self.components.materials_needed,
|
||||
duration: self.duration,
|
||||
classes: self.classes,
|
||||
sources: self.sources,
|
||||
tags: vec![],
|
||||
description: self.description
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert query struct to spell
|
||||
pub fn from_query(query: QuerySpell) -> Self {
|
||||
impl From<QuerySpell> for Spell {
|
||||
fn from(query: QuerySpell) -> Self {
|
||||
return Self {
|
||||
name: query.name,
|
||||
school: query.school,
|
||||
@@ -107,14 +132,30 @@ impl Spell {
|
||||
duration: query.duration,
|
||||
classes: query.classes,
|
||||
sources: query.sources,
|
||||
tags: query.tags,
|
||||
description: query.description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert file to spell
|
||||
pub fn from_file(file: String) -> Self {
|
||||
let data = std::fs::read_to_string(file).unwrap();
|
||||
let spell: Spell = serde_json::from_str(&data).unwrap();
|
||||
return spell;
|
||||
impl Into<InsertSpell> for Spell {
|
||||
fn into(self) -> InsertSpell {
|
||||
return InsertSpell {
|
||||
name: self.name,
|
||||
school: self.school,
|
||||
level: self.level,
|
||||
ritual: self.ritual,
|
||||
casting_time: self.casting_time,
|
||||
range: self.range,
|
||||
components_verbal: self.components.verbal,
|
||||
components_somatic: self.components.somatic,
|
||||
components_material: self.components.material,
|
||||
components_materials_needed: self.components.materials_needed,
|
||||
duration: self.duration,
|
||||
classes: self.classes,
|
||||
sources: self.sources,
|
||||
tags: self.tags,
|
||||
description: self.description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user