actix-admin/tests/test_setup/comment.rs

68 lines
1.6 KiB
Rust
Raw Normal View History

2023-07-31 13:55:20 +02:00
use super::Post;
use actix_admin::prelude::*;
2022-08-27 14:41:08 +02:00
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
2023-07-31 13:55:20 +02:00
#[derive(
Clone,
Debug,
PartialEq,
DeriveEntityModel,
Deserialize,
Serialize,
DeriveActixAdmin,
DeriveActixAdminModel,
DeriveActixAdminViewModel,
2022-09-02 17:06:13 +02:00
)]
2022-08-27 14:41:08 +02:00
#[sea_orm(table_name = "comment")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
2023-01-13 14:54:16 +01:00
#[actix_admin(searchable)]
2022-08-27 14:41:08 +02:00
pub comment: String,
#[sea_orm(column_type = "Text")]
#[actix_admin(html_input_type = "email")]
pub user: String,
#[sea_orm(column_type = "DateTime")]
pub insert_date: DateTime,
pub is_visible: bool,
2023-07-31 13:55:20 +02:00
#[actix_admin(select_list = "Post")]
2022-08-27 14:41:08 +02:00
pub post_id: Option<i32>,
2023-07-31 13:55:20 +02:00
pub my_decimal: Decimal,
2022-08-27 14:41:08 +02:00
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::post::Entity",
from = "Column::PostId",
to = "super::post::Column::Id"
)]
Post,
}
impl Related<super::post::Entity> for Entity {
fn to() -> RelationDef {
Relation::Post.def()
}
}
impl ActiveModelBehavior for ActiveModel {}
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {
fn validate(model: &ActiveModel) -> HashMap<String, String> {
let mut errors = HashMap::new();
if model.my_decimal.clone().unwrap() < Decimal::from(100 as i16) {
2023-07-31 13:55:20 +02:00
errors.insert(
"my_decimal".to_string(),
"Must be larger than 100".to_string(),
);
2022-08-27 14:41:08 +02:00
}
errors
}
2023-06-25 12:10:35 +02:00
}
2023-07-31 13:55:20 +02:00
impl ActixAdminModelFilterTrait<Entity> for Entity {}