Skip to content

Commit

Permalink
chore: add sources endpoint, filters and sort
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa committed Jul 19, 2024
1 parent c974573 commit 089964e
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ counter = "0.6.0"
ordered-float = { version = "4", features = ["serde"]}
num-traits = "0.2.19"
maplit = "1.0.2"
itertools = "0.13.0"

regex = "1.10.5"

Expand Down
15 changes: 1 addition & 14 deletions src/db/data_providers/shop_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db::data_providers::generic_fetcher::{
fetch_armor_runes, fetch_item_traits, fetch_weapon_damage_data, fetch_weapon_runes, MyString,
fetch_armor_runes, fetch_item_traits, fetch_weapon_damage_data, fetch_weapon_runes,
};
use crate::db::data_providers::raw_query_builder::prepare_filtered_get_items;
use crate::models::item::armor_struct::{Armor, ArmorData};
Expand Down Expand Up @@ -228,19 +228,6 @@ pub async fn fetch_shields(
Ok(result_vec)
}

pub async fn fetch_traits_associated_with_items(conn: &Pool<Sqlite>) -> Result<Vec<String>> {
let x: Vec<MyString> = sqlx::query_as(
"
SELECT
tt.name AS my_str
FROM TRAIT_ITEM_ASSOCIATION_TABLE tiat
LEFT JOIN TRAIT_TABLE tt ON tiat.trait_id = tt.name GROUP BY tt.name",
)
.fetch_all(conn)
.await?;
Ok(x.iter().map(|x| x.my_str.clone()).collect())
}

async fn update_items_with_traits(conn: &Pool<Sqlite>, mut items: Vec<Item>) -> Vec<Item> {
for item in &mut items {
item.traits = fetch_item_traits(conn, item.id).await.unwrap_or(vec![]);
Expand Down
75 changes: 10 additions & 65 deletions src/db/shop_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::db::data_providers::{generic_fetcher, shop_fetcher};
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::db::data_providers::shop_fetcher;
use crate::models::item::armor_struct::Armor;
use crate::models::item::item_fields_enum::{FieldsUniqueValuesStruct, ItemField};
use crate::models::item::item_struct::Item;
use crate::models::item::shield_struct::Shield;
use crate::models::item::weapon_struct::Weapon;
Expand All @@ -11,7 +9,7 @@ use crate::models::shop_structs::{ItemSortEnum, ShopFilterQuery, ShopPaginatedRe
use crate::AppState;
use anyhow::Result;
use cached::proc_macro::once;
use strum::IntoEnumIterator;
use itertools::Itertools;

pub async fn get_item_by_id(app_state: &AppState, id: i64) -> Option<ResponseItem> {
shop_fetcher::fetch_item_by_id(&app_state.conn, id)
Expand Down Expand Up @@ -52,6 +50,7 @@ pub async fn get_paginated_items(
ItemSortEnum::Level => a.core_item.level.cmp(&b.core_item.level),
ItemSortEnum::Type => a.core_item.item_type.cmp(&b.core_item.item_type),
ItemSortEnum::Rarity => a.core_item.rarity.cmp(&b.core_item.rarity),
ItemSortEnum::Source => a.core_item.source.cmp(&b.core_item.source),
};
match pagination
.shop_sort_data
Expand Down Expand Up @@ -133,67 +132,13 @@ async fn get_list(app_state: &AppState) -> Vec<ResponseItem> {
response_vec
}

pub async fn get_all_possible_values_of_filter(
app_state: &AppState,
field: ItemField,
) -> Vec<String> {
let runtime_fields_values = get_all_keys(app_state).await;
let mut x = match field {
ItemField::Category => runtime_fields_values.list_of_categories,

ItemField::Size => runtime_fields_values.list_of_sizes,
ItemField::Rarity => runtime_fields_values.list_of_rarities,
ItemField::Traits => runtime_fields_values.list_of_traits,
ItemField::Sources => runtime_fields_values.list_of_sources,
ItemField::Level => runtime_fields_values.list_of_levels,
ItemField::ItemType => CreatureTypeEnum::iter().map(|x| x.to_string()).collect(),
_ => vec![],
};
x.sort();
x
}

/// Gets all the runtime keys (each table column unique values). It will cache the result
/// Gets all the runtime sources. It will cache the result
#[once(sync_writes = true)]
async fn get_all_keys(app_state: &AppState) -> FieldsUniqueValuesStruct {
FieldsUniqueValuesStruct {
list_of_levels: generic_fetcher::fetch_unique_values_of_field(
&app_state.conn,
"CREATURE_CORE",
"level",
)
.await
.unwrap_or_default(),
list_of_categories: generic_fetcher::fetch_unique_values_of_field(
&app_state.conn,
"CREATURE_CORE",
"family",
)
.await
.unwrap(),
list_of_traits: shop_fetcher::fetch_traits_associated_with_items(&app_state.conn)
.await
.unwrap_or_default(),
list_of_sources: generic_fetcher::fetch_unique_values_of_field(
&app_state.conn,
"CREATURE_CORE",
"source",
)
.await
.unwrap_or_default(),
list_of_sizes: generic_fetcher::fetch_unique_values_of_field(
&app_state.conn,
"CREATURE_CORE",
"size",
)
.await
.unwrap_or_default(),
list_of_rarities: generic_fetcher::fetch_unique_values_of_field(
&app_state.conn,
"CREATURE_CORE",
"rarity",
)
.await
.unwrap_or_default(),
pub async fn get_all_sources(app_state: &AppState) -> Vec<String> {
match get_all_items_from_db(app_state).await {
Ok(v) => v.into_iter().map(|x| x.source).unique().collect(),
Err(_) => {
vec![]
}
}
}
6 changes: 5 additions & 1 deletion src/models/item/item_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ impl Item {
PathfinderVersionEnum::Legacy => !self.remaster,
PathfinderVersionEnum::Remaster => self.remaster,
PathfinderVersionEnum::Any => true,
}
} && filters.source_filter.as_ref().map_or(true, |source| {
self.source
.to_lowercase()
.contains(source.to_lowercase().as_str())
})
}
}
1 change: 1 addition & 0 deletions src/models/routers_validator_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct CreatureFieldFilters {
pub struct ItemFieldFilters {
pub name_filter: Option<String>,
pub category_filter: Option<String>,
pub source_filter: Option<String>,

#[validate(range(min = 0.))]
pub min_bulk_filter: Option<f64>,
Expand Down
2 changes: 2 additions & 0 deletions src/models/shop_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum ItemSortEnum {
Type,
#[serde(alias = "rarity", alias = "RARITY")]
Rarity,
#[serde(alias = "source", alias = "SOURCE")]
Source,
}

#[derive(Serialize, Deserialize, IntoParams, Validate, Eq, PartialEq, Hash, Default)]
Expand Down
9 changes: 5 additions & 4 deletions src/routes/shop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn init_endpoints(cfg: &mut web::ServiceConfig) {
web::scope("/shop")
.service(get_item)
.service(get_shop_listing)
.service(get_sources_list)
.service(get_random_shop_listing),
);
}
Expand Down Expand Up @@ -134,7 +135,7 @@ pub async fn get_item(

#[utoipa::path(
get,
path = "/shop/traits",
path = "/shop/sources",
tag = "shop",
params(
Expand All @@ -144,9 +145,9 @@ pub async fn get_item(
(status=400, description = "Bad request.")
),
)]
#[get("/traits")]
pub async fn get_traits_list(data: web::Data<AppState>) -> actix_web::Result<impl Responder> {
Ok(web::Json(shop_service::get_traits_list(&data).await))
#[get("/sources")]
pub async fn get_sources_list(data: web::Data<AppState>) -> actix_web::Result<impl Responder> {
Ok(web::Json(shop_service::get_sources_list(&data).await))
}

fn sanitize_id(creature_id: &str) -> actix_web::Result<i64> {
Expand Down
5 changes: 2 additions & 3 deletions src/services/shop_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::db::shop_proxy;
use crate::models::item::item_fields_enum::ItemField;
use crate::models::response_data::ResponseItem;
use crate::models::routers_validator_structs::ItemFieldFilters;
use crate::models::shop_structs::{
Expand Down Expand Up @@ -120,8 +119,8 @@ pub async fn generate_random_shop_listing(
}
}

pub async fn get_traits_list(app_state: &AppState) -> Vec<String> {
shop_proxy::get_all_possible_values_of_filter(app_state, ItemField::Traits).await
pub async fn get_sources_list(app_state: &AppState) -> Vec<String> {
shop_proxy::get_all_sources(app_state).await
}

/// Gets the n of: weapons, armors, shields (in this order).
Expand Down
4 changes: 4 additions & 0 deletions src/services/url_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ fn shop_filter_query_calculator(field_filters: &ItemFieldFilters) -> String {
.category_filter
.clone()
.map(|cat| format!("category_filter={}", cat)),
field_filters
.source_filter
.clone()
.map(|source| format!("source_filter={}", source)),
field_filters
.min_hardness_filter
.map(|hn| format!("min_hardness_filter={}", hn)),
Expand Down

0 comments on commit 089964e

Please sign in to comment.