use j_db::database::Database; use serde::{Deserialize, Serialize}; use std::collections::HashSet; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Service { pub name: String, id: Option, } impl Service { pub fn new(name: &str) -> Self { Self { name: name.to_string(), id: None, } } pub fn add_service(db: &Database, name: &str) -> Result { let service = Self::new(name); db.insert(service) } pub fn find_service_by_name( db: &Database, name: &str, ) -> Result, j_db::error::JDbError> { Ok(db .filter(|_, service: &Service| service.name.contains(name))? .next() .clone()) } } impl j_db::model::JdbModel for Service { fn id(&self) -> Option { self.id } fn set_id(&mut self, id: u64) { self.id = Some(id) } fn tree() -> String { "Service".to_string() } fn check_unique(&self, other: &Self) -> bool { !self.name.eq_ignore_ascii_case(&other.name) } } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ServiceGroup { pub name: String, pub description: String, pub services: HashSet, id: Option, } impl ServiceGroup { pub fn new(name: &str, description: &str) -> Self { Self { name: name.to_string(), description: description.to_string(), services: HashSet::new(), id: None, } } pub fn add_group( db: &Database, name: &str, description: &str, ) -> Result { let service_group = Self::new(name, description); db.insert(service_group) } pub fn find_group_by_name( db: &Database, name: &str, ) -> Result, j_db::error::JDbError> { Ok(db .filter(|_, group: &ServiceGroup| group.name.eq_ignore_ascii_case(name))? .next() .clone()) } pub fn get_service_groups( db: &Database, service: u64, ) -> Result, j_db::error::JDbError> { Ok(db .filter(|_, group: &ServiceGroup| group.services.contains(&service))? .collect()) } } impl j_db::model::JdbModel for ServiceGroup { fn id(&self) -> Option { self.id } fn set_id(&mut self, id: u64) { self.id = Some(id) } fn tree() -> String { "ServiceGroup".to_string() } fn check_unique(&self, other: &Self) -> bool { !self.name.eq_ignore_ascii_case(&other.name) } }