Skip to content

Commit

Permalink
refactor: pagination, folder structure,
Browse files Browse the repository at this point in the history
  • Loading branch information
RakuJa committed Jun 5, 2024
1 parent e100570 commit ea6138a
Show file tree
Hide file tree
Showing 24 changed files with 276 additions and 94 deletions.
68 changes: 55 additions & 13 deletions src/db/bestiary_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};

use crate::db::data_providers::creature_fetcher::fetch_traits_associated_with_creatures;
use crate::db::data_providers::{creature_fetcher, generic_fetcher};
use crate::models::bestiary_structs::{BestiaryPaginatedRequest, CreatureSortEnum};
use crate::models::creature::creature_component::creature_core::CreatureCoreData;
use crate::models::creature::creature_filter_enum::{CreatureFilter, FieldsUniqueValuesStruct};
use crate::models::creature::creature_metadata::alignment_enum::AlignmentEnum;
Expand All @@ -11,7 +12,7 @@ use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::creature::creature_metadata::variant_enum::CreatureVariant;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use crate::models::response_data::OptionalData;
use crate::models::routers_validator_structs::{CreatureFieldFilters, PaginatedRequest};
use crate::models::routers_validator_structs::{CreatureFieldFilters, OrderEnum};
use crate::AppState;
use anyhow::Result;
use cached::proc_macro::once;
Expand Down Expand Up @@ -47,21 +48,69 @@ pub async fn get_elite_creature_by_id(
pub async fn get_paginated_creatures(
app_state: &AppState,
filters: &CreatureFieldFilters,
pagination: &PaginatedRequest,
pagination: &BestiaryPaginatedRequest,
) -> Result<(u32, Vec<Creature>)> {
let list = get_list(app_state, CreatureVariant::Base).await;

let filtered_list: Vec<Creature> = list
let mut filtered_list: Vec<Creature> = list
.into_iter()
.filter(|x| Creature::is_passing_filters(x, filters))
.collect();

let total_creature_count = filtered_list.len();

filtered_list.sort_by(|a, b| {
let cmp = match pagination
.bestiary_sort_data
.sort_by
.clone()
.unwrap_or_default()
{
CreatureSortEnum::Id => a.core_data.essential.id.cmp(&b.core_data.essential.id),
CreatureSortEnum::Name => a.core_data.essential.name.cmp(&b.core_data.essential.name),
CreatureSortEnum::Level => a
.core_data
.essential
.level
.cmp(&b.core_data.essential.level),
CreatureSortEnum::Trait => a
.core_data
.traits
.join(", ")
.cmp(&b.core_data.traits.join(", ")),
CreatureSortEnum::Size => a.core_data.essential.size.cmp(&b.core_data.essential.size),
CreatureSortEnum::Type => a
.core_data
.essential
.cr_type
.cmp(&b.core_data.essential.cr_type),
CreatureSortEnum::Hp => a.core_data.essential.hp.cmp(&b.core_data.essential.hp),
CreatureSortEnum::Rarity => a
.core_data
.essential
.rarity
.cmp(&b.core_data.essential.rarity),
CreatureSortEnum::Family => a
.core_data
.essential
.family
.cmp(&b.core_data.essential.family),
};
match pagination
.bestiary_sort_data
.order_by
.clone()
.unwrap_or_default()
{
OrderEnum::Ascending => cmp,
OrderEnum::Descending => cmp.reverse(),
}
});

let curr_slice: Vec<Creature> = filtered_list
.iter()
.skip(pagination.cursor as usize)
.take(pagination.page_size as usize)
.skip(pagination.paginated_request.cursor as usize)
.take(pagination.paginated_request.page_size as usize)
.cloned()
.collect();

Expand Down Expand Up @@ -182,14 +231,7 @@ async fn get_all_keys(app_state: &AppState) -> FieldsUniqueValuesStruct {
/// It will cache the result.
#[once(sync_writes = true, result = true)]
async fn get_all_creatures_from_db(app_state: &AppState) -> Result<Vec<CreatureCoreData>> {
creature_fetcher::fetch_creatures_core_data(
&app_state.conn,
&PaginatedRequest {
cursor: 0,
page_size: -1,
},
)
.await
creature_fetcher::fetch_creatures_core_data(&app_state.conn, 0, -1).await
}

/// Infallible method, it will expose a vector representing the values fetched from db or empty vec
Expand Down
18 changes: 7 additions & 11 deletions src/db/cr_core_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ use crate::db::data_providers::creature_fetcher::{
use crate::models::creature::creature_component::creature_core::EssentialData;
use crate::models::creature::creature_metadata::alignment_enum::AlignmentEnum;
use crate::models::creature::creature_metadata::creature_role::CreatureRoleEnum;
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::routers_validator_structs::PaginatedRequest;
use crate::models::shared::rarity_enum::RarityEnum;
use crate::models::shared::size_enum::SizeEnum;
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Pool, Sqlite};

/// Handler for startup, first creature_core initialization. Then it shouldn't be used
pub async fn update_creature_core_table(conn: &Pool<Sqlite>) -> Result<()> {
let pagination = PaginatedRequest {
cursor: 0,
page_size: -1,
};
let scales = fetch_creature_scales(conn).await?;
for cr in get_creatures_raw_essential_data(conn, &pagination).await? {
for cr in get_creatures_raw_essential_data(conn, 0, -1).await? {
let traits = fetch_creature_traits(conn, cr.id).await?;
let alignment = AlignmentEnum::from((&traits, cr.remaster));
update_alignment_column_value(conn, alignment.to_string(), cr.id).await?;
Expand Down Expand Up @@ -143,16 +138,17 @@ async fn update_alignment_column_value(

async fn get_creatures_raw_essential_data(
conn: &Pool<Sqlite>,
paginated_request: &PaginatedRequest,
cursor: u32,
page_size: i16,
) -> Result<Vec<RawEssentialData>> {
Ok(sqlx::query_as!(
RawEssentialData,
"SELECT
id, aon_id, name, hp, level, size, family, rarity,
license, remaster, source, cr_type
FROM CREATURE_TABLE ORDER BY name LIMIT ?,?",
paginated_request.cursor,
paginated_request.page_size
cursor,
page_size
)
.fetch_all(conn)
.await?)
Expand Down
8 changes: 4 additions & 4 deletions src/db/data_providers/creature_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use crate::models::db::raw_speed::RawSpeed;
use crate::models::db::raw_trait::RawTrait;
use crate::models::db::raw_weakness::RawWeakness;
use crate::models::response_data::OptionalData;
use crate::models::routers_validator_structs::PaginatedRequest;
use crate::models::scales_struct::ability_scales::AbilityScales;
use crate::models::scales_struct::ac_scales::AcScales;
use crate::models::scales_struct::area_dmg_scales::AreaDmgScales;
Expand Down Expand Up @@ -352,12 +351,13 @@ pub async fn fetch_creatures_core_data_with_filters(
/// for the search.
pub async fn fetch_creatures_core_data(
conn: &Pool<Sqlite>,
paginated_request: &PaginatedRequest,
cursor: u32,
page_size: i16,
) -> Result<Vec<CreatureCoreData>> {
let cr_core: Vec<CreatureCoreData> =
sqlx::query_as("SELECT * FROM CREATURE_CORE ORDER BY name LIMIT ?,?")
.bind(paginated_request.cursor)
.bind(paginated_request.page_size)
.bind(cursor)
.bind(page_size)
.fetch_all(conn)
.await?;
Ok(update_creatures_core_with_traits(conn, cr_core).await)
Expand Down
10 changes: 3 additions & 7 deletions src/db/data_providers/shop_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::db::data_providers::raw_query_builder::prepare_filtered_get_items;
use crate::models::db::raw_trait::RawTrait;
use crate::models::item::item_metadata::type_enum::ItemTypeEnum;
use crate::models::item::item_struct::Item;
use crate::models::routers_validator_structs::PaginatedRequest;
use crate::models::shop_structs::ShopFilterQuery;
use anyhow::Result;
use log::debug;
Expand All @@ -25,13 +24,10 @@ pub async fn fetch_item_by_id(conn: &Pool<Sqlite>, item_id: i64) -> Result<Item>
Ok(item)
}

pub async fn fetch_items(
conn: &Pool<Sqlite>,
paginated_request: &PaginatedRequest,
) -> Result<Vec<Item>> {
pub async fn fetch_items(conn: &Pool<Sqlite>, cursor: u32, page_size: i16) -> Result<Vec<Item>> {
let items: Vec<Item> = sqlx::query_as("SELECT * FROM ITEM_TABLE ORDER BY name LIMIT ?,?")
.bind(paginated_request.cursor)
.bind(paginated_request.page_size)
.bind(cursor)
.bind(page_size)
.fetch_all(conn)
.await?;
Ok(update_items_with_traits(conn, items).await)
Expand Down
44 changes: 30 additions & 14 deletions src/db/shop_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::db::data_providers::{generic_fetcher, shop_fetcher};
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::item::item_fields_enum::{FieldsUniqueValuesStruct, ItemField};
use crate::models::item::item_struct::Item;
use crate::models::routers_validator_structs::{ItemFieldFilters, PaginatedRequest};
use crate::models::shop_structs::ShopFilterQuery;
use crate::models::routers_validator_structs::{ItemFieldFilters, OrderEnum};
use crate::models::shop_structs::{ItemSortEnum, ShopFilterQuery, ShopPaginatedRequest};
use crate::AppState;
use anyhow::Result;
use cached::proc_macro::once;
Expand All @@ -25,21 +25,44 @@ pub async fn get_filtered_items(
pub async fn get_paginated_items(
app_state: &AppState,
filters: &ItemFieldFilters,
pagination: &PaginatedRequest,
pagination: &ShopPaginatedRequest,
) -> Result<(u32, Vec<Item>)> {
let list = get_list(app_state).await;

let filtered_list: Vec<Item> = list
let mut filtered_list: Vec<Item> = list
.into_iter()
.filter(|x| Item::is_passing_filters(x, filters))
.collect();

let total_item_count = filtered_list.len();

filtered_list.sort_by(|a, b| {
let cmp = match pagination
.shop_sort_data
.sort_by
.clone()
.unwrap_or_default()
{
ItemSortEnum::Id => a.id.cmp(&b.id),
ItemSortEnum::Name => a.name.cmp(&b.name),
ItemSortEnum::Level => a.level.cmp(&b.level),
ItemSortEnum::Type => a.item_type.cmp(&b.item_type),
};
match pagination
.shop_sort_data
.order_by
.clone()
.unwrap_or_default()
{
OrderEnum::Ascending => cmp,
OrderEnum::Descending => cmp.reverse(),
}
});

let curr_slice: Vec<Item> = filtered_list
.iter()
.skip(pagination.cursor as usize)
.take(pagination.page_size as usize)
.skip(pagination.paginated_request.cursor as usize)
.take(pagination.paginated_request.page_size as usize)
.cloned()
.collect();

Expand All @@ -50,14 +73,7 @@ pub async fn get_paginated_items(
/// It will cache the result.
#[once(sync_writes = true, result = true)]
async fn get_all_items_from_db(app_state: &AppState) -> Result<Vec<Item>> {
shop_fetcher::fetch_items(
&app_state.conn,
&PaginatedRequest {
cursor: 0,
page_size: -1,
},
)
.await
shop_fetcher::fetch_items(&app_state.conn, 0, -1).await
}

/// Infallible method, it will expose a vector representing the values fetched from db or empty vec
Expand Down
40 changes: 40 additions & 0 deletions src/models/bestiary_structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::models::routers_validator_structs::{OrderEnum, PaginatedRequest};
use serde::{Deserialize, Serialize};
use strum::Display;
use utoipa::{IntoParams, ToSchema};
use validator::Validate;

#[derive(Serialize, Deserialize, ToSchema, Default, Eq, PartialEq, Hash, Clone, Display)]
pub enum CreatureSortEnum {
#[serde(alias = "id", alias = "ID")]
Id,
#[default]
#[serde(alias = "name", alias = "NAME")]
Name,
#[serde(alias = "level", alias = "LEVEL")]
Level,
#[serde(alias = "trait", alias = "TRAIT")]
Trait,
#[serde(alias = "size", alias = "SIZE")]
Size,
#[serde(alias = "type", alias = "TYPE")]
Type,
#[serde(alias = "hp", alias = "HP")]
Hp,
#[serde(alias = "rarity", alias = "RARITY")]
Rarity,
#[serde(alias = "family", alias = "FAMILY")]
Family,
}

#[derive(Serialize, Deserialize, IntoParams, Validate, Eq, PartialEq, Hash, Default)]
pub struct BestiarySortData {
pub sort_by: Option<CreatureSortEnum>,
pub order_by: Option<OrderEnum>,
}

#[derive(Serialize, Deserialize, IntoParams, Validate, Eq, PartialEq, Hash)]
pub struct BestiaryPaginatedRequest {
pub paginated_request: PaginatedRequest,
pub bestiary_sort_data: BestiarySortData,
}
4 changes: 2 additions & 2 deletions src/models/creature/creature_component/creature_core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::models::creature::creature_metadata::alignment_enum::AlignmentEnum;
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::shared::rarity_enum::RarityEnum;
use crate::models::shared::size_enum::SizeEnum;
use serde::{Deserialize, Serialize};
use sqlx::sqlite::SqliteRow;
use sqlx::{Error, FromRow, Row};
Expand Down
4 changes: 2 additions & 2 deletions src/models/creature/creature_component/filter_struct.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::models::creature::creature_metadata::alignment_enum::AlignmentEnum;
use crate::models::creature::creature_metadata::creature_role::CreatureRoleEnum;
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use crate::models::shared::rarity_enum::RarityEnum;
use crate::models::shared::size_enum::SizeEnum;
use std::collections::HashSet;

pub struct FilterStruct {
Expand Down
2 changes: 0 additions & 2 deletions src/models/creature/creature_metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub mod alignment_enum;
pub mod creature_role;
pub mod rarity_enum;
pub mod size_enum;
pub mod type_enum;
pub mod variant_enum;
2 changes: 1 addition & 1 deletion src/models/creature/items/action.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::shared::rarity_enum::RarityEnum;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

Expand Down
2 changes: 1 addition & 1 deletion src/models/creature/items/weapon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::shared::rarity_enum::RarityEnum;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

Expand Down
4 changes: 2 additions & 2 deletions src/models/encounter_structs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::models::creature::creature_metadata::alignment_enum::AlignmentEnum;
use crate::models::creature::creature_metadata::creature_role::CreatureRoleEnum;
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum;
use crate::models::pf_version_enum::PathfinderVersionEnum;
use crate::models::shared::rarity_enum::RarityEnum;
use crate::models::shared::size_enum::SizeEnum;
use rand::distributions::{Distribution, Standard};
use rand::Rng;
use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions src/models/item/item_struct.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::models::creature::creature_metadata::rarity_enum::RarityEnum;
use crate::models::creature::creature_metadata::size_enum::SizeEnum;
use crate::models::item::item_metadata::type_enum::ItemTypeEnum;
use crate::models::routers_validator_structs::ItemFieldFilters;
use crate::models::shared::rarity_enum::RarityEnum;
use crate::models::shared::size_enum::SizeEnum;
use serde::{Deserialize, Serialize};
use sqlx::sqlite::SqliteRow;
use sqlx::{Error, FromRow, Row};
Expand Down
2 changes: 2 additions & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod bestiary_structs;
pub mod creature;
pub mod db;
pub mod encounter_structs;
Expand All @@ -6,4 +7,5 @@ pub mod pf_version_enum;
pub mod response_data;
pub mod routers_validator_structs;
pub mod scales_struct;
pub mod shared;
pub mod shop_structs;
Loading

0 comments on commit ea6138a

Please sign in to comment.