use serde::{Deserialize, Serialize};
use indexmap::IndexMap;
use super::statements::Statement;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Controller {
pub name: String,
pub resource: Option<String>,
pub invokable: bool,
pub namespace: Option<String>,
pub parent: Option<String>,
pub extends: Option<String>,
pub traits: Vec<String>,
pub implements: Vec<String>,
pub policies: bool,
pub methods: IndexMap<String, Vec<Statement>>,
}
impl Controller {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
resource: None,
invokable: false,
namespace: None,
parent: None,
extends: None,
traits: vec![],
implements: vec![],
policies: false,
methods: IndexMap::new(),
}
}
pub fn is_resource(&self) -> bool {
self.resource.is_some()
}
pub fn is_api_resource(&self) -> bool {
matches!(self.resource.as_deref(), Some("api"))
}
pub fn resource_actions(&self) -> Vec<String> {
if let Some(ref resource_type) = self.resource {
let all_actions = vec![
"index".to_string(),
"create".to_string(),
"store".to_string(),
"show".to_string(),
"edit".to_string(),
"update".to_string(),
"destroy".to_string(),
];
if resource_type == "api" {
return all_actions.into_iter().filter(|a| a != "create" && a != "edit").collect();
}
return all_actions;
}
vec![]
}
}