pub mod administrator;
pub mod caching;
pub mod converters;
pub mod db_query_api;
pub mod hooks;
pub mod output_data;
pub mod validation;
use crate::widgets::Widget;
use mongodb::{
bson::{doc, oid::ObjectId},
sync::Client,
};
use serde::Deserialize;
use serde_json::value::Value;
use std::{collections::HashMap, error::Error};
#[derive(Deserialize, Clone, Debug)]
pub struct Meta {
pub model_name: String,
pub project_name: String,
pub unique_project_key: String,
pub service_name: String,
pub database_name: String,
pub db_client_name: String,
pub db_query_docs_limit: u32,
pub collection_name: String,
pub fields_count: usize,
pub fields_name: Vec<String>,
pub is_add_docs: bool,
pub is_up_docs: bool,
pub is_del_docs: bool,
pub field_type_map: std::collections::HashMap<String, String>,
pub widget_type_map: std::collections::HashMap<String, String>,
pub default_value_map: std::collections::HashMap<String, (String, String)>,
pub ignore_fields: Vec<String>,
}
impl Default for Meta {
fn default() -> Self {
Meta {
model_name: String::new(),
project_name: String::new(),
unique_project_key: String::new(),
service_name: String::new(),
database_name: String::new(),
db_client_name: String::new(),
db_query_docs_limit: 0_u32,
collection_name: String::new(),
fields_count: 0_usize,
fields_name: Vec::new(),
is_add_docs: true,
is_up_docs: true,
is_del_docs: true,
field_type_map: std::collections::HashMap::new(),
widget_type_map: std::collections::HashMap::new(),
default_value_map: std::collections::HashMap::new(),
ignore_fields: Vec::new(),
}
}
}
pub trait Main {
fn key() -> Result<String, Box<dyn Error>>;
fn meta() -> Result<Meta, Box<dyn Error>>;
fn widgets() -> Result<HashMap<String, Widget>, Box<dyn Error>>;
fn get_hash(&self) -> String;
fn set_hash(&mut self, value: String);
fn get_created_at(&self) -> String;
fn set_created_at(&mut self, value: String);
fn get_updated_at(&self) -> String;
fn set_updated_at(&mut self, value: String);
fn self_to_json(&self) -> Result<Value, Box<dyn Error>>;
fn hash_to_id(hash: &str) -> Result<ObjectId, Box<dyn Error>> {
Ok(ObjectId::with_string(hash)?)
}
fn id_to_hash(object_id: ObjectId) -> String {
object_id.to_hex()
}
fn vitaminize(
project_name: &str,
unique_project_key: &str,
collection_name: &str,
client: &Client,
map_widgets: &mut HashMap<String, Widget>,
) -> Result<(), Box<dyn Error>> {
let db_mango_tech: String = format!("mango_tech__{}__{}", project_name, unique_project_key);
let collection = client
.database(&db_mango_tech)
.collection("dynamic_widgets");
let filter = doc! {
"collection": collection_name
};
if let Some(doc) = collection.find_one(filter, None)? {
let doc_dyn_values = doc.get_document("fields")?;
for (field_name, widget) in map_widgets {
let widget_type = widget.widget.clone();
if widget_type.contains("Dyn") {
let arr = doc_dyn_values.get_array(field_name)?;
let options: Vec<(String, String)> = arr
.iter()
.map(|item| {
let arr = item.as_array().unwrap();
(
arr[0].as_str().unwrap().to_string(),
arr[1].as_str().unwrap().to_string(),
)
})
.collect();
widget.options = options;
}
}
} else {
Err(format!(
"Model: {} ; Method: `vitaminize()` => \
Document with values for dynamic widgets not found.",
Self::meta()?.model_name
))?
}
Ok(())
}
}