actix-admin/examples/basic/entity/post.rs

73 lines
2.1 KiB
Rust
Raw Normal View History

2022-08-06 20:09:30 +02:00
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use actix_admin::prelude::*;
use std::fmt;
use std::fmt::Display;
use std::str::FromStr;
2022-08-20 20:54:39 +02:00
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdmin, DeriveActixAdminViewModel, DeriveActixAdminModel, DeriveActixAdminModelSelectList)]
2022-08-06 20:09:30 +02:00
#[sea_orm(table_name = "post")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
2022-12-29 19:56:20 +01:00
#[actix_admin(searchable, not_empty)]
2022-08-06 20:09:30 +02:00
pub title: String,
#[sea_orm(column_type = "Text")]
2022-08-20 23:31:45 +02:00
#[actix_admin(searchable, textarea)]
2022-08-06 20:09:30 +02:00
pub text: String,
#[actix_admin(select_list="Tea")]
pub tea_mandatory: Tea,
#[actix_admin(select_list="Tea")]
pub tea_optional: Option<Tea>,
2023-01-08 15:45:28 +01:00
#[sea_orm(column_type = "Date")]
2022-08-06 20:09:30 +02:00
pub insert_date: Date,
2022-12-29 19:56:20 +01:00
#[actix_admin(file_upload)]
2023-01-08 15:45:28 +01:00
pub attachment: Option<String>
2022-08-06 20:09:30 +02:00
}
2022-08-20 20:54:39 +02:00
impl Display for Model {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match &*self {
2023-01-08 15:45:28 +01:00
_ => write!(formatter, "{} {}", &self.title, ""/* &self.insert_date*/),
2022-08-20 20:54:39 +02:00
}
}
}
2022-08-06 20:09:30 +02:00
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
2022-08-20 20:54:39 +02:00
pub enum Relation {
#[sea_orm(has_many = "super::comment::Entity")]
Comment,
}
impl Related<super::comment::Entity> for Entity {
fn to() -> RelationDef {
Relation::Comment.def()
}
}
2022-08-06 20:09:30 +02:00
impl ActiveModelBehavior for ActiveModel {}
2022-08-20 20:54:39 +02:00
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Deserialize, Serialize, DeriveActixAdminEnumSelectList)]
2022-08-06 20:09:30 +02:00
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")]
pub enum Tea {
#[sea_orm(string_value = "EverydayTea")]
EverydayTea,
#[sea_orm(string_value = "BreakfastTea")]
BreakfastTea,
}
impl FromStr for Tea {
type Err = ();
fn from_str(input: &str) -> Result<Tea, Self::Err> {
match input {
"EverydayTea" => Ok(Tea::EverydayTea),
"BreakfastTea" => Ok(Tea::BreakfastTea),
_ => Err(()),
}
}
}
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}