1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use mongodb::bson::{doc, document::Document, oid::ObjectId};
use serde::{de::DeserializeOwned, ser::Serialize};
use serde_json::{json, Value};
use std::error::Error;
use crate::models::{
db_query_api::{commons::QCommons, paladins::QPaladins},
helpers::Meta,
};
pub enum OutputDataAdmin<T> {
Instance(Option<T>),
EarlyResult(String),
}
pub trait Administrator: QCommons + QPaladins {
fn instance_to_json_for_admin(&self) -> Result<String, Box<dyn Error>>
where
Self: Serialize + DeserializeOwned + Sized,
{
let (model_cache, _client_cache) = Self::get_cache_data_for_query()?;
let meta: Meta = model_cache.meta;
let model_json = self.self_to_json_val()?;
let mut field_type_list = Vec::<Value>::new();
let hash = self.hash();
for field_name in meta.fields_name.iter() {
let mut field_type = model_json.get(field_name).unwrap().clone();
if field_name == "created_at" || field_name == "updated_at" {
*field_type.get_mut("input_type").unwrap() = json!("datetime");
*field_type.get_mut("is_hide").unwrap() = json!(false);
}
if field_name.contains("password") && !hash.is_empty() {
*field_type.get_mut("input_type").unwrap() = json!("hidden");
*field_type.get_mut("is_hide").unwrap() = json!(true);
*field_type.get_mut("value").unwrap() = json!("");
}
field_type_list.push(field_type);
}
Ok(serde_json::to_string(&field_type_list)?)
}
fn actix_instance_for_admin(
doc_hash: Option<&str>,
bytes: Option<&actix_web::web::BytesMut>,
filter: Option<&Document>,
dyn_data: Option<Value>,
) -> Result<OutputDataAdmin<Self>, Box<dyn Error>>
where
Self: Serialize + DeserializeOwned + Sized,
{
if let Some(doc_hash) = doc_hash {
if doc_hash.is_empty() {
return Ok(OutputDataAdmin::EarlyResult(
Self::model_to_json_for_admin()?
));
}
let object_id = ObjectId::with_string(doc_hash);
if object_id.is_err() {
Err(format!(
"Model: `{}` > \
Method: `instance_for_admin` => \
Invalid document hash.",
Self::key()?
))?
}
let object_id = object_id.unwrap();
let filter = doc! {"_id": object_id};
Ok(OutputDataAdmin::Instance(Self::find_one_to_instance(
filter, None,
)?))
} else if let Some(bytes) = bytes {
Ok(OutputDataAdmin::Instance(Some(serde_json::from_slice::<
Self,
>(bytes)?)))
} else if let Some(filter) = filter {
Ok(OutputDataAdmin::Instance(Self::find_one_to_instance(
filter.clone(),
None,
)?))
} else if let Some(dyn_data) = dyn_data {
Self::update_dyn_field(dyn_data)?;
Ok(OutputDataAdmin::EarlyResult(String::new()))
} else {
Err(format!(
"Model: `{}` > \
Method: `instance_for_admin` => \
No match on function arguments.",
Self::key()?
))?
}
}
fn actix_result_for_admin(
&mut self,
doc_hash: Option<&str>,
bytes: Option<&actix_web::web::BytesMut>,
filter: Option<&Document>,
) -> Result<String, Box<dyn Error>>
where
Self: Serialize + DeserializeOwned + Sized,
{
if doc_hash.is_some() {
return self.instance_to_json_for_admin();
} else if bytes.is_some() {
let output_data = self.save(None, None)?;
return output_data.json_for_admin();
} else if filter.is_some() {
let output_data = self.delete(None)?;
if !output_data.is_valid() {
return Ok(output_data.err_msg());
}
} else {
Err(format!(
"Model: `{}` > \
Method: `result_for_admin` => \
No match on function arguments.",
Self::key()?
))?
}
Ok(String::new())
}
}