use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use super::ids::GearId; use super::listing::{SortDirection, SortKey}; #[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum GearCategory { Grinder, Brewer, } impl GearCategory { pub fn as_str(&self) -> &'static str { match self { GearCategory::Grinder => "grinder", GearCategory::Brewer => "brewer", } } pub fn from_str(s: &str) -> Option { match s.to_lowercase().as_str() { "grinder" => Some(GearCategory::Grinder), "brewer" => Some(GearCategory::Brewer), _ => None, } } pub fn display_label(&self) -> &'static str { match self { GearCategory::Grinder => "Grinder", GearCategory::Brewer => "Brewer", } } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Gear { pub id: GearId, pub category: GearCategory, pub make: String, pub model: String, pub notes: Option, pub created_at: DateTime, pub updated_at: DateTime, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct NewGear { pub category: GearCategory, pub make: String, pub model: String, pub notes: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct UpdateGear { pub make: Option, pub model: Option, pub notes: Option, } #[derive(Debug, Default, Clone)] pub struct GearFilter { pub category: Option, } impl GearFilter { pub fn all() -> Self { Self::default() } pub fn for_category(category: GearCategory) -> Self { Self { category: Some(category), } } } #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum GearSortKey { Make, Model, Category, CreatedAt, } impl SortKey for GearSortKey { fn default() -> Self { GearSortKey::Make } fn from_query(value: &str) -> Option { match value { "make" => Some(GearSortKey::Make), "model" => Some(GearSortKey::Model), "category" => Some(GearSortKey::Category), "created-at" => Some(GearSortKey::CreatedAt), _ => None, } } fn query_value(self) -> &'static str { match self { GearSortKey::Make => "make", GearSortKey::Model => "model", GearSortKey::Category => "category", GearSortKey::CreatedAt => "created-at", } } fn default_direction(self) -> SortDirection { match self { GearSortKey::Make | GearSortKey::Model | GearSortKey::Category => SortDirection::Asc, GearSortKey::CreatedAt => SortDirection::Desc, } } }