use crate::request;
use once_cell::sync::OnceCell;
use crate::models::Product;
pub enum Category {
Plugin,
World,
Script,
ServerSetup,
Minigame,
Schematic,
Configuration,
Modpack,
PremiumPlugin
}
pub async fn all() -> &'static [Product] {
static PRODUCTS: OnceCell<Vec<Product>> = OnceCell::new();
if PRODUCTS.get().is_none() {
let products = request::get_data::<Vec<Product>>(
"https://facade-service-prod.superleague.com/facade/v1/client/products"
).await;
PRODUCTS.set(products).unwrap();
}
PRODUCTS.get().unwrap()
}
pub async fn of_category(category: Category) -> Vec<&'static Product> {
let category = match category {
Category::Plugin => "Plugin",
Category::World =>"World",
Category::Script => "Script",
Category::ServerSetup => "Server Setup",
Category::Minigame => "Minigame",
Category::Schematic => "Schematic",
Category::Configuration => "Configuration",
Category::Modpack => "Modpack",
Category::PremiumPlugin => "Premium Plugin"
};
all().await.into_iter().filter(
|p| p.category == category
).collect()
}
pub async fn search(query: &str) -> Vec<&'static Product> {
all().await.into_iter().filter(
|p| p.title.contains(query)
).collect()
}