2022-08-06 20:09:30 +02:00
|
|
|
use sea_orm::entity::prelude::*;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-07-05 20:23:22 +02:00
|
|
|
use actix_admin::prelude::*;
|
2023-07-04 19:49:26 +02:00
|
|
|
use super::{Post, post};
|
2023-07-05 20:23:22 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2022-08-06 20:09:30 +02:00
|
|
|
|
2022-08-18 13:56:38 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel)]
|
2022-08-06 20:09:30 +02:00
|
|
|
#[sea_orm(table_name = "comment")]
|
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
|
|
|
#[serde(skip_deserializing)]
|
|
|
|
#[actix_admin(primary_key)]
|
|
|
|
pub id: i32,
|
|
|
|
pub comment: String,
|
|
|
|
#[sea_orm(column_type = "Text")]
|
2023-02-15 12:18:24 +01:00
|
|
|
#[actix_admin(html_input_type = "email", list_regex_mask= "^([a-zA-Z]*)")]
|
2022-08-06 20:09:30 +02:00
|
|
|
pub user: String,
|
|
|
|
#[sea_orm(column_type = "DateTime")]
|
|
|
|
pub insert_date: DateTime,
|
2022-08-20 20:54:39 +02:00
|
|
|
pub is_visible: bool,
|
|
|
|
#[actix_admin(select_list="Post")]
|
|
|
|
pub post_id: Option<i32>,
|
|
|
|
pub my_decimal: Decimal
|
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(
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2022-08-06 20:09:30 +02:00
|
|
|
|
2022-08-20 22:51:15 +02:00
|
|
|
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) {
|
|
|
|
errors.insert("my_decimal".to_string(), "Must be larger than 100".to_string());
|
|
|
|
}
|
2023-02-03 18:06:14 +01:00
|
|
|
|
2022-08-20 22:51:15 +02:00
|
|
|
errors
|
|
|
|
}
|
2023-06-16 16:17:28 +02:00
|
|
|
}
|
|
|
|
|
2023-07-04 19:49:26 +02:00
|
|
|
#[async_trait]
|
2023-06-16 16:17:28 +02:00
|
|
|
impl ActixAdminModelFilterTrait<Entity> for Entity {
|
|
|
|
fn get_filter() -> Vec<ActixAdminModelFilter<Entity>> {
|
|
|
|
vec![
|
|
|
|
ActixAdminModelFilter::<Entity> {
|
2023-07-04 19:49:26 +02:00
|
|
|
name: "User".to_string(),
|
|
|
|
filter_type: ActixAdminModelFilterType::Text,
|
2023-06-16 16:17:28 +02:00
|
|
|
filter: |q: sea_orm::Select<Entity>, v| -> sea_orm::Select<Entity> {
|
2023-07-04 19:49:26 +02:00
|
|
|
q.apply_if(v, | query, val: String| query.filter(Column::User.eq(val)))
|
|
|
|
},
|
|
|
|
values: None
|
2023-06-16 16:17:28 +02:00
|
|
|
},
|
|
|
|
ActixAdminModelFilter::<Entity> {
|
2023-07-04 19:49:26 +02:00
|
|
|
name: "Insert Date After".to_string(),
|
|
|
|
filter_type: ActixAdminModelFilterType::DateTime,
|
2023-06-16 16:17:28 +02:00
|
|
|
filter: |q: sea_orm::Select<Entity>, v| -> sea_orm::Select<Entity> {
|
2023-07-05 20:23:22 +02:00
|
|
|
q.apply_if(v, | query, val: String| query.filter(Column::InsertDate.gte(NaiveDateTime::parse_from_str(&val, "%Y-%m-%dT%H:%M").unwrap())))
|
2023-07-04 19:49:26 +02:00
|
|
|
},
|
|
|
|
values: None
|
|
|
|
},
|
|
|
|
ActixAdminModelFilter::<Entity> {
|
|
|
|
name: "Is Visible".to_string(),
|
|
|
|
filter_type: ActixAdminModelFilterType::Checkbox,
|
|
|
|
filter: |q: sea_orm::Select<Entity>, v| -> sea_orm::Select<Entity> {
|
|
|
|
q.apply_if(v, | query, val: String| query.filter(Column::IsVisible.eq(val)))
|
|
|
|
},
|
|
|
|
values: None
|
|
|
|
},
|
|
|
|
ActixAdminModelFilter::<Entity> {
|
|
|
|
name: "Post".to_string(),
|
|
|
|
filter_type: ActixAdminModelFilterType::SelectList,
|
|
|
|
filter: |q: sea_orm::Select<Entity>, v| -> sea_orm::Select<Entity> {
|
|
|
|
q.apply_if(v, | query, val: String| query.filter(Column::PostId.eq(val)))
|
|
|
|
},
|
|
|
|
values: None
|
2023-06-16 16:17:28 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2023-07-04 19:49:26 +02:00
|
|
|
|
|
|
|
async fn get_filter_values(filter: &ActixAdminModelFilter<Entity>, db: &DatabaseConnection) -> Option<Vec<(String, String)>> {
|
|
|
|
match filter.name.as_str() {
|
|
|
|
"Post" => Some({
|
|
|
|
Post::find().order_by_asc(post::Column::Id).all(db).await.unwrap()
|
|
|
|
.iter().map(|p| (p.id.to_string(), p.title.to_string())).collect()
|
|
|
|
}),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 16:17:28 +02:00
|
|
|
}
|
|
|
|
|