Skip to content

Commit

Permalink
Merge pull request #70 from EdwinBetanc0urt/feature/parse-documents-r…
Browse files Browse the repository at this point in the history
…emove-redundant-code

feat: Remove redundant code to parse documents.
  • Loading branch information
yamelsenih authored Dec 5, 2024
2 parents 01d7583 + 153e2ac commit 08a20c5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 71 deletions.
6 changes: 3 additions & 3 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand Down
36 changes: 20 additions & 16 deletions src/models/browser.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -291,6 +291,18 @@ pub struct DependendField {
pub parent_name: Option<String>
}


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<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Browser, String> {
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Expand All @@ -312,17 +324,12 @@ pub async fn browser_from_id(_id: Option<String>, _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);
Expand Down Expand Up @@ -354,11 +361,8 @@ pub async fn browsers(_language: Option<&String>, _search_value: Option<&String>
Ok(values) => {
let mut browsers_list: Vec<Browser> = 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 {
Expand Down
14 changes: 10 additions & 4 deletions src/models/form.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Form, String> {
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Expand All @@ -153,8 +159,8 @@ pub async fn form_from_id(_id: Option<String>, _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)
// })
Expand Down Expand Up @@ -192,7 +198,7 @@ pub async fn forms(_language: Option<&String>, _search_value: Option<&String>, _
Ok(values) => {
let mut forms_list: Vec<Form> = 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 {
Expand Down
30 changes: 17 additions & 13 deletions src/models/process.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -270,6 +270,18 @@ pub struct Workflow {
pub help: Option<String>,
}


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<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Process, String> {
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Expand All @@ -291,13 +303,8 @@ pub async fn process_from_id(_id: Option<String>, _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
Expand Down Expand Up @@ -333,11 +340,8 @@ pub async fn processes(_language: Option<&String>, _search_value: Option<&String
Ok(values) => {
let mut processes_list: Vec<Process> = 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());
}

Expand Down
62 changes: 27 additions & 35 deletions src/models/window.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -304,6 +304,29 @@ pub struct Table {
pub selection_colums: Option<Vec<String>>,
}


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<String>, _language: Option<&String>, _dictionary_code: Option<&String>) -> Result<Window, String> {
if _id.is_none() || _id.as_deref().map_or(false, |s| s.trim().is_empty()) {
return Err(
Expand All @@ -325,24 +348,8 @@ pub async fn window_from_id(_id: Option<String>, _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)
},
Expand Down Expand Up @@ -376,22 +383,7 @@ pub async fn windows(_language: Option<&String>, _search_value: Option<&String>,
Ok(values) => {
let mut windows_list: Vec<Window> = 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());
}

Expand Down

0 comments on commit 08a20c5

Please sign in to comment.