This commit is contained in:
Benjamin Sherriff
2023-10-05 09:07:53 -04:00
parent ac17be838a
commit 1b41849115
54 changed files with 6473 additions and 129 deletions

View File

@@ -6,7 +6,7 @@ use crate::db::{schema::spells::{self}, classes::AbilityType, conditions::Condit
use super::{SchoolType, CastingTime, CastingType, SpellAttackType, SpellDamageType, Range, Area, Components, Duration, Source, Description, DurationType};
#[derive(Queryable, QueryableByName)]
#[derive(Queryable, QueryableByName, Serialize, Deserialize)]
#[diesel(table_name = spells)]
pub struct QuerySpell {
pub id: i32,
@@ -191,6 +191,7 @@ impl InsertSpell {
#[derive(Debug, Serialize, Deserialize)]
pub struct Spell {
pub id: Option<i32>,
pub name: String,
pub school: SchoolType,
pub level: i32,
@@ -226,17 +227,18 @@ impl From<QuerySpell> for Spell {
Err(err) => {
log::error!("Failed to parse spell: {}", err);
Self {
id: None,
name: "".to_string(),
school: SchoolType::Abjuration,
level: 0,
ritual: false,
casting_time: CastingTime { amount: 0, casting_type: CastingType::Action },
casting_time: CastingTime { value: 0, casting_type: CastingType::Action },
saving_throw: None,
attack_type: None,
damage_inflict: None,
damage_resist: None,
conditions: None,
range: Range { range_type: "".to_string(), amount: None, unit: None },
range: Range { range_type: "".to_string(), value: None, unit: None },
area: None,
components: Components { verbal: false, somatic: false, material: false, materials_needed: None, materials_cost: None, materials_consumed: None },
durations: vec![],

View File

@@ -73,7 +73,7 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
None => None
};
// Limit must be between 1 and 100
let limit = std::cmp::min(std::cmp::max(params.limit.unwrap_or(20), 1), 100);
let limit = std::cmp::min(std::cmp::max(params.limit.unwrap_or(100), 1), 100);
let total_count = QuerySpell::get_count(&filters).unwrap();
let max_page = std::cmp::max((total_count as f64 / limit as f64).ceil() as i32, 1);
// Page must be between 1 and max_page
@@ -82,8 +82,11 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
match web::block(move || QuerySpell::get_all(&filters, limit, page)).await.unwrap() {
Ok(spells) => {
let mut response: Vec<Spell> = Vec::new();
for spell in spells {
response.push(Spell::from(spell));
for query_spell in spells {
let id = query_spell.id;
let mut spell = Spell::from(query_spell);
spell.id = Some(id);
response.push(spell);
}
HttpResponse::Ok().json(GetResponse {
data: response,
@@ -112,10 +115,15 @@ async fn get_by_id(id: web::Path<String>) -> HttpResponse {
})
};
match web::block(move || QuerySpell::get_by_id(id)).await.unwrap() {
Ok(spell) => HttpResponse::Ok().json(GetResponse {
data: Spell::from(spell),
metadata: None
}),
Ok(query_spell) => {
let id = query_spell.id;
let mut spell = Spell::from(query_spell);
spell.id = Some(id);
HttpResponse::Ok().json(GetResponse {
data: spell,
metadata: None
})
},
Err(err) => {
error!("{:?}", err.message);
ResponseError::error_response(&err)

View File

@@ -56,7 +56,7 @@ impl FromStr for SchoolType {
#[derive(Debug, Serialize, Deserialize)]
pub struct CastingTime {
pub amount: i32,
pub value: i32,
#[serde(rename = "unit")]
pub casting_type: CastingType
}
@@ -209,7 +209,7 @@ pub struct Range {
#[serde(rename = "type")]
pub range_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<i32>,
pub value: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit: Option<String>
}
@@ -219,7 +219,7 @@ pub struct Area {
#[serde(rename = "type")]
pub area_type: AreaType,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<i32>,
pub value: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit: Option<String>
}
@@ -270,7 +270,7 @@ pub struct Duration {
#[serde(rename = "type")]
pub duration_type: DurationType,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<i32>,
pub value: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit: Option<String>
}

View File

@@ -4,6 +4,7 @@ extern crate diesel_migrations;
use std::env;
use actix_cors::Cors;
use actix_web::{HttpServer, App};
use dotenv::dotenv;
@@ -22,9 +23,15 @@ async fn main() -> std::io::Result<()> {
let port = env::var("SERVICE_PORT").unwrap_or("5000".to_string());
match HttpServer::new(|| {
let cors = Cors::default()
.allow_any_origin()
.allow_any_method()
.allow_any_header()
.max_age(3600);
App::new()
.configure(db::messages::init_routes)
.configure(db::spells::init_routes)
.wrap(cors)
})
.bind(format!("{}:{}", host, port)) {
Ok(b) => {