diff --git a/src/bin/server.rs b/src/bin/server.rs index 88cdee0..94b111f 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -89,7 +89,7 @@ fn routes() -> Router { ) .push( // /api/dictionary - Router::with_path("dictionary") + Router::with_path("dictionary") .push( // /api/dictionary/browsers/ Router::with_path("browsers") @@ -115,8 +115,8 @@ fn routes() -> Router { ) ) .push( - // /api/dictionary/processes - Router::with_path("processes") + // /api/dictionary/processes + Router::with_path("processes") .options(options_response) .get(get_processes) .push( diff --git a/src/models/browser.rs b/src/models/browser.rs index 75ab142..c18cff2 100644 --- a/src/models/browser.rs +++ b/src/models/browser.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; use salvo::prelude::*; -use serde_json::json; +use serde_json::{json, Value}; use std::{io::ErrorKind, io::Error}; use crate::{controller::opensearch::{find, get_by_id, IndexDocument}, models::get_index_name}; @@ -291,6 +291,18 @@ pub struct DependendField { pub parent_name: Option } + +pub fn parse_browser(value: Value) -> Browser { + let mut browser: Browser = serde_json::from_value(value).unwrap(); + + // sort fields by sequence + if let Some(ref mut fields) = browser.fields { + fields.sort_by_key(|field| field.sequence.clone().unwrap_or(0)); + } + browser.to_owned() +} + + pub async fn browser_from_id(_id: Option, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result { if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) { return Err( @@ -312,17 +324,12 @@ pub async fn browser_from_id(_id: Option, _language: Option<&String>, _d let _browser_document: &dyn IndexDocument = &_document; match get_by_id(_browser_document).await { Ok(value) => { - let mut browser: Browser = serde_json::from_value(value).unwrap(); - log::info!("Finded Value: {:?}", browser.id); + let browser: Browser = parse_browser(value); + log::info!("Finded Browser {:?}: {:?}", browser.name, browser.id); - // sort fields by sequence - if let Some(ref mut fields) = browser.fields { - fields.sort_by_key(|field| field.sequence.clone().unwrap_or(0)); - } - - Ok( - browser - ) + Ok( + browser + ) }, Err(error) => { log::error!("{}", error); @@ -354,11 +361,8 @@ pub async fn browsers(_language: Option<&String>, _search_value: Option<&String> Ok(values) => { let mut browsers_list: Vec = vec![]; for value in values { - let mut browser: Browser = serde_json::from_value(value).unwrap(); - // sort fields by sequence - if let Some(ref mut fields) = browser.fields { - fields.sort_by_key(|field| field.sequence.clone().unwrap_or(0)); - } + let browser: Browser = parse_browser(value); + browsers_list.push(browser.to_owned()); } Ok(BrowserListResponse { diff --git a/src/models/form.rs b/src/models/form.rs index a4ca34e..7737128 100644 --- a/src/models/form.rs +++ b/src/models/form.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; use salvo::prelude::*; -use serde_json::json; +use serde_json::{json, Value}; use std::{io::ErrorKind, io::Error}; use crate::{controller::opensearch::{find, get_by_id, IndexDocument}, models::get_index_name}; @@ -132,6 +132,12 @@ impl IndexDocument for Form { } } + +pub fn parse_form(value: Value) -> Form { + let form: Form = serde_json::from_value(value).unwrap(); + form.to_owned() +} + pub async fn form_from_id(_id: Option, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result { if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) { return Err( @@ -153,8 +159,8 @@ pub async fn form_from_id(_id: Option, _language: Option<&String>, _dict let _form_document: &dyn IndexDocument = &_document; match get_by_id(_form_document).await { Ok(value) => { - let form: Form = serde_json::from_value(value).unwrap(); - log::info!("Finded Form Value: {:?}", form.id); + let form: Form = parse_form(value); + log::info!("Finded Form {:?} Value: {:?}", form.name, form.id); // Ok(FormResponse { // form: Some(form) // }) @@ -192,7 +198,7 @@ pub async fn forms(_language: Option<&String>, _search_value: Option<&String>, _ Ok(values) => { let mut forms_list: Vec
= vec![]; for value in values { - let form: Form = serde_json::from_value(value).unwrap(); + let form: Form = parse_form(value); forms_list.push(form.to_owned()); } Ok(FormsListResponse { diff --git a/src/models/process.rs b/src/models/process.rs index dba6a2f..aa884ad 100644 --- a/src/models/process.rs +++ b/src/models/process.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; use salvo::prelude::*; -use serde_json::json; +use serde_json::{json, Value}; use std::{io::ErrorKind, io::Error}; use crate::{controller::opensearch::{find, get_by_id, IndexDocument}, models::get_index_name}; @@ -270,6 +270,18 @@ pub struct Workflow { pub help: Option, } + +pub fn parse_process(value: Value) -> Process { + let mut process: Process = serde_json::from_value(value).unwrap(); + + // sort process parameter list by sequence + if let Some(ref mut parameters) = process.parameters { + parameters.sort_by_key(|parameter: &ProcessParameters| parameter.sequence.clone().unwrap_or(0)); + } + process.to_owned() +} + + pub async fn process_from_id(_id: Option, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result { if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) { return Err( @@ -291,13 +303,8 @@ pub async fn process_from_id(_id: Option, _language: Option<&String>, _d let _process_document: &dyn IndexDocument = &_document; match get_by_id(_process_document).await { Ok(value) => { - let mut process: Process = serde_json::from_value(value).unwrap(); - log::info!("Finded Process Value: {:?}", process.id); - - // sort process parameter by sequence - if let Some(ref mut parameters) = process.parameters { - parameters.sort_by_key(|parameter| parameter.sequence.clone().unwrap_or(0)); - } + let process: Process = parse_process(value); + log::info!("Finded Process {:?} Value: {:?}", process.name, process.id); Ok( process @@ -333,11 +340,8 @@ pub async fn processes(_language: Option<&String>, _search_value: Option<&String Ok(values) => { let mut processes_list: Vec = vec![]; for value in values { - let mut process: Process = serde_json::from_value(value).unwrap(); - // sort process parameter by sequence - if let Some(ref mut parameters) = process.parameters { - parameters.sort_by_key(|parameter| parameter.sequence.clone().unwrap_or(0)); - } + let process: Process = parse_process(value); + processes_list.push(process.to_owned()); } diff --git a/src/models/window.rs b/src/models/window.rs index deb8d70..e5d1f6f 100644 --- a/src/models/window.rs +++ b/src/models/window.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; use salvo::prelude::*; -use serde_json::json; +use serde_json::{json, Value}; use std::{io::ErrorKind, io::Error}; use crate::{controller::opensearch::{find, get_by_id, IndexDocument}, models::get_index_name}; @@ -304,6 +304,29 @@ pub struct Table { pub selection_colums: Option>, } + +pub fn parse_window(value: Value) -> Window { + let mut window: Window = serde_json::from_value(value).unwrap(); + + // sort tabs list by sequence + if let Some(ref mut tabs) = window.tabs { + // sort tabs list by sequence + tabs.sort_by_key(|tab: &WindowTab| tab.sequence.clone().unwrap_or(0)); + for tab in tabs.iter_mut() { + // sort processes list by name + if let Some(ref mut processes) = tab.processes { + processes.sort_by_key(|process: &Process| process.name.clone().unwrap_or("".to_owned())); + } + // sort fields list by sequence + if let Some(ref mut fields) = tab.fields { + fields.sort_by_key(|field: &WindowField| field.sequence.clone().unwrap_or(0)); + } + } + } + window.to_owned() +} + + pub async fn window_from_id(_id: Option, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result { if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) { return Err( @@ -325,24 +348,8 @@ pub async fn window_from_id(_id: Option, _language: Option<&String>, _di let _window_document: &dyn IndexDocument = &_document; match get_by_id(_window_document).await { Ok(value) => { - let mut window: Window = serde_json::from_value(value).unwrap(); - log::info!("Finded Window Value: {:?}", window.id); - - // sort tabs list by sequence - if let Some(ref mut tabs) = window.tabs { - // sort tabs list by sequence - tabs.sort_by_key(|tab: &WindowTab| tab.sequence.clone().unwrap_or(0)); - for tab in tabs.iter_mut() { - // sort processes list by name - if let Some(ref mut processes) = tab.processes { - processes.sort_by_key(|process: &Process| process.name.clone().unwrap_or("".to_owned())); - } - // sort fields list by sequence - if let Some(ref mut fields) = tab.fields { - fields.sort_by_key(|field: &WindowField| field.sequence.clone().unwrap_or(0)); - } - } - } + let window: Window = parse_window(value); + log::info!("Finded Window {:?} Value: {:?}", window.name, window.id); Ok(window) }, @@ -376,22 +383,7 @@ pub async fn windows(_language: Option<&String>, _search_value: Option<&String>, Ok(values) => { let mut windows_list: Vec = vec![]; for value in values { - let mut window: Window = serde_json::from_value(value).unwrap(); - // sort tabs by sequence - if let Some(ref mut tabs) = window.tabs { - // sort tabs list by sequence - tabs.sort_by_key(|tab: &WindowTab| tab.sequence.clone().unwrap_or(0)); - for tab in tabs.iter_mut() { - // sort processes list by name - if let Some(ref mut processes) = tab.processes { - processes.sort_by_key(|process: &Process| process.name.clone().unwrap_or("".to_owned())); - } - // sort fields list by sequence - if let Some(ref mut fields) = tab.fields { - fields.sort_by_key(|field: &WindowField| field.sequence.clone().unwrap_or(0)); - } - } - } + let window: Window = parse_window(value); windows_list.push(window.to_owned()); }