Move json_to_insertitle to database module
This commit is contained in:
parent
2dea10ca62
commit
da6f295027
2 changed files with 77 additions and 78 deletions
|
@ -66,6 +66,82 @@ pub fn get_history() -> Result<Vec<History>, DatabaseError> {
|
|||
crud::get_history(connection)
|
||||
}
|
||||
|
||||
/// Converts a json Value object to an InsertTitle instance.
|
||||
pub fn json_to_insert_title(obj: &serde_json::Value) -> InsertTitle {
|
||||
InsertTitle {
|
||||
mirabelid: obj.get("id").unwrap().as_i64().unwrap() as i32,
|
||||
revueid: obj.get("revueid").unwrap().as_i64().unwrap() as i32,
|
||||
obsoletepar: match obj.get("obsoletepar") {
|
||||
Some(val) => match val.as_i64() {
|
||||
Some(val) => Some(val as i32),
|
||||
None => None,
|
||||
},
|
||||
None => None,
|
||||
},
|
||||
titre: match obj.get("titre") {
|
||||
Some(val) => match val.as_str() {
|
||||
Some(val) => val,
|
||||
None => "",
|
||||
},
|
||||
None => "",
|
||||
},
|
||||
prefixe: match obj.get("prefixe") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
datedebut: match obj.get("datedebut") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
datefin: match obj.get("datefin") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
url: match obj.get("url") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
periodicite: match obj.get("periodicite") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
editeurs: match obj.get("editeurs") {
|
||||
Some(val) => match val.as_array() {
|
||||
Some(val) => val
|
||||
.iter()
|
||||
.map(|item| item.as_str())
|
||||
.collect::<Vec<Option<&str>>>(),
|
||||
None => vec![],
|
||||
},
|
||||
None => vec![],
|
||||
},
|
||||
langues: match obj.get("langues") {
|
||||
Some(val) => match val.as_array() {
|
||||
Some(val) => val
|
||||
.iter()
|
||||
.map(|item| item.as_str())
|
||||
.collect::<Vec<Option<&str>>>(),
|
||||
None => vec![],
|
||||
},
|
||||
None => vec![],
|
||||
},
|
||||
liens: match obj.get("liens") {
|
||||
Some(val) => match val.as_array() {
|
||||
Some(val) => val
|
||||
.iter()
|
||||
.map(|item| item.as_str())
|
||||
.collect::<Vec<Option<&str>>>(),
|
||||
None => vec![],
|
||||
},
|
||||
None => vec![],
|
||||
},
|
||||
url_revue_mirabel: match obj.get("url_revue_mirabel") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts a history in the local database from a request.
|
||||
pub fn save_request_to_history(request: &Request) -> Result<(), DatabaseError> {
|
||||
let connection = &mut crud::establish_connection()?;
|
||||
|
|
79
src/lib.rs
79
src/lib.rs
|
@ -13,7 +13,6 @@ mod schema;
|
|||
|
||||
use api_client::ApiClient;
|
||||
use ear::Ear;
|
||||
use models::InsertTitle;
|
||||
|
||||
/// Entry point of the library, propagates errors to `main`.
|
||||
pub fn run() -> Result<(), Box<dyn Error>> {
|
||||
|
@ -30,7 +29,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
|
|||
.as_array()
|
||||
.unwrap() // we just checked for Option above so we can safely unwrap here
|
||||
.iter()
|
||||
.map(|val| json_to_insert_title(val));
|
||||
.map(|val| database::json_to_insert_title(val));
|
||||
// Add the fetched titles to the database
|
||||
database::add_all_titles(all_titles).unwrap_or_else(|err| {
|
||||
eprintln!("{err}");
|
||||
|
@ -43,79 +42,3 @@ pub fn run() -> Result<(), Box<dyn Error>> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Converts a json Value object to an InsertTitle instance.
|
||||
fn json_to_insert_title(obj: &serde_json::Value) -> InsertTitle {
|
||||
InsertTitle {
|
||||
mirabelid: obj.get("id").unwrap().as_i64().unwrap() as i32,
|
||||
revueid: obj.get("revueid").unwrap().as_i64().unwrap() as i32,
|
||||
obsoletepar: match obj.get("obsoletepar") {
|
||||
Some(val) => match val.as_i64() {
|
||||
Some(val) => Some(val as i32),
|
||||
None => None,
|
||||
},
|
||||
None => None,
|
||||
},
|
||||
titre: match obj.get("titre") {
|
||||
Some(val) => match val.as_str() {
|
||||
Some(val) => val,
|
||||
None => "",
|
||||
},
|
||||
None => "",
|
||||
},
|
||||
prefixe: match obj.get("prefixe") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
datedebut: match obj.get("datedebut") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
datefin: match obj.get("datefin") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
url: match obj.get("url") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
periodicite: match obj.get("periodicite") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
editeurs: match obj.get("editeurs") {
|
||||
Some(val) => match val.as_array() {
|
||||
Some(val) => val
|
||||
.iter()
|
||||
.map(|item| item.as_str())
|
||||
.collect::<Vec<Option<&str>>>(),
|
||||
None => vec![],
|
||||
},
|
||||
None => vec![],
|
||||
},
|
||||
langues: match obj.get("langues") {
|
||||
Some(val) => match val.as_array() {
|
||||
Some(val) => val
|
||||
.iter()
|
||||
.map(|item| item.as_str())
|
||||
.collect::<Vec<Option<&str>>>(),
|
||||
None => vec![],
|
||||
},
|
||||
None => vec![],
|
||||
},
|
||||
liens: match obj.get("liens") {
|
||||
Some(val) => match val.as_array() {
|
||||
Some(val) => val
|
||||
.iter()
|
||||
.map(|item| item.as_str())
|
||||
.collect::<Vec<Option<&str>>>(),
|
||||
None => vec![],
|
||||
},
|
||||
None => vec![],
|
||||
},
|
||||
url_revue_mirabel: match obj.get("url_revue_mirabel") {
|
||||
Some(val) => val.as_str(),
|
||||
None => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue