Skip to content

Commit

Permalink
feat: Implement source endpoint (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa authored Nov 16, 2023
1 parent 7953c74 commit 7785ed4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/db/db_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct RuntimeFieldsValues {
pub list_of_levels: Vec<String>,
pub list_of_families: Vec<String>,
pub list_of_traits: Vec<String>,
pub list_of_sources: Vec<String>,
pub list_of_alignments: Vec<String>,
pub list_of_sizes: Vec<String>,
pub list_of_rarities: Vec<String>,
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions src/db/db_communicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ async fn from_raw_to_creature(conn: &Pool<Sqlite>, raw: &RawCreature) -> Creatur
}

pub async fn fetch_creatures(conn: &Pool<Sqlite>) -> Result<Vec<Creature>, 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 {
Expand Down
1 change: 1 addition & 0 deletions src/db/db_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub async fn get_keys(app_state: &AppState, field: CreatureField) -> Vec<String>
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,
Expand Down
19 changes: 19 additions & 0 deletions src/routes/bestiary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -112,6 +114,23 @@ pub async fn get_traits_list(data: web::Data<AppState>) -> Result<impl Responder
Ok(web::Json(bestiary_service::get_traits_list(&data).await))
}

#[utoipa::path(
get,
path = "/bestiary/sources",
tag = "bestiary",
params(
),
responses(
(status=200, description = "Successful Response", body = [String]),
(status=400, description = "Bad request.")
),
)]
#[get("/sources")]
pub async fn get_sources_list(data: web::Data<AppState>) -> Result<impl Responder> {
Ok(web::Json(bestiary_service::get_sources_list(&data).await))
}

#[utoipa::path(
get,
path = "/bestiary/rarities",
Expand Down
4 changes: 4 additions & 0 deletions src/services/bestiary_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub async fn get_traits_list(app_state: &AppState) -> Vec<String> {
db_proxy::get_keys(app_state, CreatureField::Traits).await
}

pub async fn get_sources_list(app_state: &AppState) -> Vec<String> {
db_proxy::get_keys(app_state, CreatureField::Sources).await
}

pub async fn get_rarities_list(app_state: &AppState) -> Vec<String> {
db_proxy::get_keys(app_state, CreatureField::Rarity).await
}
Expand Down

0 comments on commit 7785ed4

Please sign in to comment.