diff --git a/src/db/db_cache.rs b/src/db/db_cache.rs index 81ce106..5aed42a 100644 --- a/src/db/db_cache.rs +++ b/src/db/db_cache.rs @@ -7,6 +7,7 @@ pub struct RuntimeFieldsValues { pub list_of_levels: Vec, pub list_of_families: Vec, pub list_of_traits: Vec, + pub list_of_sources: Vec, pub list_of_alignments: Vec, pub list_of_sizes: Vec, pub list_of_rarities: Vec, @@ -25,6 +26,7 @@ pub fn from_db_data_to_filter_cache( list_of_levels: runtime_fields.list_of_levels.clone(), list_of_families: runtime_fields.list_of_families.clone(), list_of_traits: runtime_fields.list_of_traits.clone(), + list_of_sources: runtime_fields.list_of_sources.clone(), list_of_alignments: runtime_fields.list_of_alignments.clone(), list_of_sizes: runtime_fields.list_of_sizes.clone(), list_of_rarities: runtime_fields.list_of_rarities.clone(), @@ -62,6 +64,14 @@ pub fn from_db_data_to_filter_cache( } }); + curr_creature.sources.iter().for_each(|single_source| { + if !fields_values_cache.list_of_sources.contains(single_source) { + fields_values_cache + .list_of_sources + .push(single_source.to_string()) + } + }); + if !fields_values_cache.list_of_alignments.contains(&alignment) { fields_values_cache.list_of_alignments.push(alignment); } diff --git a/src/db/db_communicator.rs b/src/db/db_communicator.rs index 365a7ea..7437159 100644 --- a/src/db/db_communicator.rs +++ b/src/db/db_communicator.rs @@ -84,9 +84,7 @@ async fn from_raw_to_creature(conn: &Pool, raw: &RawCreature) -> Creatur } pub async fn fetch_creatures(conn: &Pool) -> Result, Error> { - let creatures = sqlx::query_as::<_, RawCreature>( - "SELECT * FROM CREATURE_TABLE ORDER BY name" - ) + let creatures = sqlx::query_as::<_, RawCreature>("SELECT * FROM CREATURE_TABLE ORDER BY name") .fetch_all(conn) .await; match creatures { diff --git a/src/db/db_proxy.rs b/src/db/db_proxy.rs index 2a9a54d..309293e 100644 --- a/src/db/db_proxy.rs +++ b/src/db/db_proxy.rs @@ -116,6 +116,7 @@ pub async fn get_keys(app_state: &AppState, field: CreatureField) -> Vec CreatureField::SpellCaster => vec![true.to_string(), false.to_string()], CreatureField::Family => runtime_fields_values.list_of_families, CreatureField::Traits => runtime_fields_values.list_of_traits, + CreatureField::Sources => runtime_fields_values.list_of_sources, CreatureField::Alignment => runtime_fields_values.list_of_alignments, CreatureField::Level => runtime_fields_values.list_of_levels, CreatureField::CreatureTypes => runtime_fields_values.list_of_creature_types, diff --git a/src/routes/bestiary.rs b/src/routes/bestiary.rs index e6942db..668ac15 100644 --- a/src/routes/bestiary.rs +++ b/src/routes/bestiary.rs @@ -19,6 +19,7 @@ pub fn init_endpoints(cfg: &mut web::ServiceConfig) { .service(get_creature) .service(get_families_list) .service(get_traits_list) + .service(get_sources_list) .service(get_rarities_list) .service(get_creature_types_list) .service(get_sizes_list) @@ -33,6 +34,7 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) { get_bestiary, get_families_list, get_traits_list, + get_sources_list, get_rarities_list, get_sizes_list, get_alignments_list, @@ -112,6 +114,23 @@ pub async fn get_traits_list(data: web::Data) -> Result) -> Result { + Ok(web::Json(bestiary_service::get_sources_list(&data).await)) +} + #[utoipa::path( get, path = "/bestiary/rarities", diff --git a/src/services/bestiary_service.rs b/src/services/bestiary_service.rs index 1f931eb..5e4b886 100644 --- a/src/services/bestiary_service.rs +++ b/src/services/bestiary_service.rs @@ -52,6 +52,10 @@ pub async fn get_traits_list(app_state: &AppState) -> Vec { db_proxy::get_keys(app_state, CreatureField::Traits).await } +pub async fn get_sources_list(app_state: &AppState) -> Vec { + db_proxy::get_keys(app_state, CreatureField::Sources).await +} + pub async fn get_rarities_list(app_state: &AppState) -> Vec { db_proxy::get_keys(app_state, CreatureField::Rarity).await }