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

/// How one person feels about one restaurant. Unranked counts as `Neutral`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(16))")]
#[serde(rename_all = "lowercase")]
pub enum Rank {
    #[sea_orm(string_value = "always")]
    Always,
    #[sea_orm(string_value = "positive")]
    Positive,
    #[sea_orm(string_value = "neutral")]
    Neutral,
    #[sea_orm(string_value = "never")]
    Never,
}

impl Rank {
    pub const ALL: [Rank; 4] = [Rank::Always, Rank::Positive, Rank::Neutral, Rank::Never];

    pub fn as_str(self) -> &'static str {
        match self {
            Rank::Always => "always",
            Rank::Positive => "positive",
            Rank::Neutral => "neutral",
            Rank::Never => "never",
        }
    }

    pub fn parse(s: &str) -> Option<Rank> {
        Rank::ALL.into_iter().find(|r| r.as_str() == s)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "rankings")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub user_id: Uuid,
    #[sea_orm(primary_key, auto_increment = false)]
    pub restaurant_id: Uuid,
    pub rank: Rank,
    pub updated_at: DateTimeWithTimeZone,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::restaurants::Entity",
        from = "Column::RestaurantId",
        to = "super::restaurants::Column::Id",
        on_delete = "Cascade"
    )]
    Restaurants,
    #[sea_orm(
        belongs_to = "super::users::Entity",
        from = "Column::UserId",
        to = "super::users::Column::Id",
        on_delete = "Cascade"
    )]
    Users,
}

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

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

impl ActiveModelBehavior for ActiveModel {}