code: static init
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
pub use config::AdminModelConfig;
|
||||
pub use dto::AdminApp;
|
||||
pub use dto::AdminModel;
|
||||
pub use repository::{AdminRepository, RepositoryInfo, RepositoryList};
|
||||
pub use repository::{
|
||||
AdminRepository, RepoInfo, RepositoryInfo, RepositoryInfoBuilder, RepositoryList,
|
||||
};
|
||||
|
||||
mod auth {
|
||||
|
||||
@@ -47,6 +49,7 @@ mod dto {
|
||||
}
|
||||
|
||||
pub mod repository {
|
||||
use derive_builder::Builder;
|
||||
use serde::{Serialize, Serializer};
|
||||
use serde_json::Value;
|
||||
use std::vec::IntoIter;
|
||||
@@ -95,8 +98,18 @@ pub mod repository {
|
||||
}
|
||||
}
|
||||
|
||||
/// Static initializer for RepositoryInfo.
|
||||
pub struct RepoInfo {
|
||||
pub name: &'static str,
|
||||
pub lookup_key: &'static str,
|
||||
pub display_list: &'static [&'static str],
|
||||
}
|
||||
|
||||
// consider builder: https://docs.rs/derive_builder/latest/derive_builder/
|
||||
// downside of builders as macro: no intellisense!
|
||||
// each repository has to implement a repo info.
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Builder)]
|
||||
#[builder(setter(into))]
|
||||
pub struct RepositoryInfo {
|
||||
name: String,
|
||||
lookup_key: String,
|
||||
@@ -112,16 +125,34 @@ pub mod repository {
|
||||
}
|
||||
}
|
||||
|
||||
/* // self mutating builder pattern?
|
||||
pub fn display_list(mut self, display_list: &[&str]) -> RepositoryInfo {
|
||||
self.display_list = display_list.iter().map(|&e| e.to_string()).collect();
|
||||
self
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl From<RepoInfo> for RepositoryInfo {
|
||||
fn from(repo_info: RepoInfo) -> Self {
|
||||
RepositoryInfo {
|
||||
name: repo_info.name.to_string(),
|
||||
lookup_key: repo_info.lookup_key.to_string(),
|
||||
display_list: repo_info
|
||||
.display_list
|
||||
.iter()
|
||||
.map(|&s| s.to_string())
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AdminRepository: Send + Sync {
|
||||
fn get_item(&self, id: usize) -> Option<Value>;
|
||||
|
||||
fn get_repo_info(&self) -> RepositoryInfo;
|
||||
fn get_list(&self) -> RepositoryList;
|
||||
fn info(&self) -> RepositoryInfo;
|
||||
fn list(&self) -> RepositoryList;
|
||||
fn get(&self, id: usize) -> Option<Value>;
|
||||
fn create(&self, data: Value) -> Option<Value>;
|
||||
fn update(&self, id: usize, data: Value) -> Option<Value>;
|
||||
fn delete(&self, id: usize) -> Option<Value>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// implementation of static repository
|
||||
|
||||
use super::domain::{AdminModelConfig, AdminRepository, RepositoryInfo, RepositoryList};
|
||||
use super::domain::{AdminModelConfig, AdminRepository, RepoInfo, RepositoryInfo, RepositoryList};
|
||||
use super::state::AdminRegistry;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
@@ -25,19 +25,38 @@ impl MyStaticRepository {
|
||||
}
|
||||
|
||||
impl AdminRepository for MyStaticRepository {
|
||||
fn get_item(&self, id: usize) -> Option<Value> {
|
||||
fn get(&self, id: usize) -> Option<Value> {
|
||||
self.content.get(id).cloned()
|
||||
}
|
||||
|
||||
fn get_list(&self) -> RepositoryList {
|
||||
fn list(&self) -> RepositoryList {
|
||||
RepositoryList::List {
|
||||
values: self.content.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_repo_info(&self) -> RepositoryInfo {
|
||||
RepositoryInfo::new("My Static Repository", "id")
|
||||
.display_list(&["id", "name", "age", "level"])
|
||||
fn info(&self) -> RepositoryInfo {
|
||||
RepoInfo {
|
||||
name: "My Static Repository",
|
||||
lookup_key: "id",
|
||||
display_list: &["id", "name", "level", "age"],
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn create(&self, data: Value) -> Option<Value> {
|
||||
println!("I would now create: {}", data);
|
||||
None
|
||||
}
|
||||
|
||||
fn update(&self, id: usize, data: Value) -> Option<Value> {
|
||||
println!("I would now update: {}, {}", id, data);
|
||||
None
|
||||
}
|
||||
|
||||
fn delete(&self, id: usize) -> Option<Value> {
|
||||
println!("Would delete: {}", id);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -109,8 +109,8 @@ pub async fn list_item_collection(
|
||||
AdminContext {
|
||||
available_apps: registry.get_apps(),
|
||||
content: model_key.to_owned(),
|
||||
item_info: Some(repo.get_repo_info()),
|
||||
item_list: repo.get_list(),
|
||||
item_info: Some(repo.info()),
|
||||
item_list: repo.list(),
|
||||
..Default::default()
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user