Add other db tables

This commit is contained in:
Benjamin Sherriff
2023-10-03 12:11:48 -04:00
parent 6c8a7ceefc
commit 95ede3291e
38 changed files with 247 additions and 26 deletions

3
src/db/spells/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
mod model;
pub use model::*;

120
src/db/spells/model.rs Normal file
View File

@@ -0,0 +1,120 @@
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use crate::db::schema::spells;
#[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 casting_time: String,
pub range: String,
pub components_verbal: bool,
pub components_somatic: bool,
pub components_material: bool,
pub components_materials_needed: Option<String>,
pub duration: String,
pub classes: Vec<String>,
pub sources: Vec<String>,
pub tags: Vec<String>,
pub description: String
}
#[derive(Insertable, AsChangeset)]
#[diesel(table_name = spells)]
pub struct InsertSpell {
pub name: String,
pub school: String,
pub level: i32,
pub ritual: bool,
pub casting_time: String,
pub range: String,
pub components_verbal: bool,
pub components_somatic: bool,
pub components_material: bool,
pub components_materials_needed: Option<String>,
pub duration: String,
pub classes: Vec<String>,
pub sources: Vec<String>,
pub tags: Vec<String>,
pub description: String
}
#[derive(Serialize, Deserialize)]
pub struct Spell {
pub name: String,
pub school: String,
pub level: i32,
pub ritual: bool,
pub casting_time: String,
pub range: String,
pub components: Components,
pub duration: String,
pub classes: Vec<String>,
pub sources: Vec<String>,
pub description: String
}
#[derive(Serialize, Deserialize)]
pub struct Components {
pub verbal: bool,
pub somatic: bool,
pub material: bool,
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 {
return Self {
name: query.name,
school: query.school,
level: query.level,
ritual: query.ritual,
casting_time: query.casting_time,
range: query.range,
components: Components {
verbal: query.components_verbal,
somatic: query.components_somatic,
material: query.components_material,
materials_needed: query.components_materials_needed
},
duration: query.duration,
classes: query.classes,
sources: query.sources,
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;
}
}