2.1 KiB · text History 6dd7389
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "restaurants")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: Uuid,
    pub area_id: Uuid,
    pub name: String,
    /// `N123` / `W123` / `R123`; `None` for a hand-dropped pin.
    #[sea_orm(unique)]
    pub osm_ref: Option<String>,
    pub lat: f64,
    pub lon: f64,
    pub cuisine: Option<String>,
    pub address: Option<String>,
    pub website: Option<String>,
    /// The raw OSM `opening_hours` expression; see `crate::hours`.
    pub opening_hours: Option<String>,
    #[sea_orm(column_type = "Text")]
    pub notes: String,
    pub archived: bool,
    pub added_by: Option<Uuid>,
    pub created_at: DateTimeWithTimeZone,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::areas::Entity",
        from = "Column::AreaId",
        to = "super::areas::Column::Id",
        on_delete = "Cascade"
    )]
    Areas,
    #[sea_orm(has_many = "super::rankings::Entity")]
    Rankings,
    #[sea_orm(has_many = "super::lunches::Entity")]
    Lunches,
}

impl Related<super::areas::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Areas.def()
    }
}

impl Related<super::rankings::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Rankings.def()
    }
}

impl Related<super::lunches::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Lunches.def()
    }
}

impl Model {
    /// The cuisine field split into tags: "Indian; nepalese" and
    /// "indian, Nepalese" both give ["indian", "nepalese"].
    pub fn cuisine_tags(&self) -> Vec<String> {
        cuisine_tags(self.cuisine.as_deref().unwrap_or_default())
    }
}

pub fn cuisine_tags(cuisine: &str) -> Vec<String> {
    let mut tags: Vec<String> = cuisine
        .split([',', ';'])
        .map(|t| t.trim().to_lowercase())
        .filter(|t| !t.is_empty())
        .collect();
    tags.dedup();
    tags
}

impl ActiveModelBehavior for ActiveModel {}