use async_trait::async_trait;
use std::collections::HashMap;
use crate::types::*;
use serde::Serialize;
#[derive(Clone)]
pub struct AdminTitle(pub String);
#[derive(Clone)]
pub struct AdminPrefix(pub String);
#[derive(Serialize, Debug)]
pub struct ResourceInfo {
pub name: String,
pub plural_name: String,
pub slug: String,
pub icon: String,
pub path_list: String,
pub path_new: String,
pub path_edit: String,
pub path_delete: String,
}
#[async_trait]
pub trait AdminResource: Send + Sync + 'static {
fn name(&self) -> &str;
fn plural_name(&self) -> &str;
fn slug(&self) -> &str;
fn icon(&self) -> &str { "box" }
fn list_columns(&self) -> Vec<Column>;
fn form_fields(&self) -> Vec<FormField>;
fn searchable_fields(&self) -> Vec<&str> { vec![] }
fn can_create(&self) -> bool { true }
fn can_delete(&self) -> bool { true }
async fn list(&self, query: ListQuery) -> Result<ListResult, AdminError>;
async fn get(&self, id: &str) -> Result<serde_json::Value, AdminError>;
async fn create(&self, data: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, AdminError>;
async fn update(&self, id: &str, data: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, AdminError>;
async fn delete(&self, id: &str) -> Result<(), AdminError>;
async fn validate(
&self,
_data: &HashMap<String, serde_json::Value>
) -> Result<(), HashMap<String, String>> { Ok(()) }
fn info(&self, prefix: &str) -> ResourceInfo {
ResourceInfo {
name: self.name().to_string(),
plural_name: self.plural_name().to_string(),
slug: self.slug().to_string(),
icon: self.icon().to_string(),
path_list: format!("{}/{}/", prefix, self.slug()),
path_new: format!("{}/{}/new", prefix, self.slug()),
path_edit: format!("{}/{}/:id/edit", prefix, self.slug()),
path_delete: format!("{}/{}/:id/delete", prefix, self.slug()),
}
}
}