use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use crate::domain::ids::{RoastId, RoasterId}; use crate::domain::listing::{SortDirection, SortKey}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Roast { pub id: RoastId, pub roaster_id: RoasterId, pub name: String, pub slug: String, pub origin: Option, pub region: Option, pub producer: Option, pub tasting_notes: Vec, pub process: Option, pub created_at: DateTime, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RoastWithRoaster { pub roast: Roast, pub roaster_name: String, pub roaster_slug: String, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct NewRoast { pub roaster_id: RoasterId, pub name: String, pub origin: String, pub region: String, pub producer: String, pub tasting_notes: Vec, pub process: String, } impl NewRoast { pub fn slug(&self) -> String { slug::slugify(&self.name) } } #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct UpdateRoast { pub roaster_id: Option, pub name: Option, pub origin: Option, pub region: Option, pub producer: Option, pub tasting_notes: Option>, pub process: Option, } #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum RoastSortKey { CreatedAt, Name, Roaster, Origin, Producer, } impl SortKey for RoastSortKey { fn default() -> Self { RoastSortKey::CreatedAt } fn from_query(value: &str) -> Option { match value { "created-at" => Some(RoastSortKey::CreatedAt), "name" => Some(RoastSortKey::Name), "roaster" => Some(RoastSortKey::Roaster), "origin" => Some(RoastSortKey::Origin), "producer" => Some(RoastSortKey::Producer), _ => None, } } fn query_value(self) -> &'static str { match self { RoastSortKey::CreatedAt => "created-at", RoastSortKey::Name => "name", RoastSortKey::Roaster => "roaster", RoastSortKey::Origin => "origin", RoastSortKey::Producer => "producer", } } fn default_direction(self) -> SortDirection { match self { RoastSortKey::CreatedAt => SortDirection::Desc, _ => SortDirection::Asc, } } }