Added timetable field ZusatzInfo (additional information)

This commit is contained in:
Denys Konovalov 2021-11-01 19:06:17 +01:00
parent 228bd4e46e
commit 57150ce7f1

@ -5,7 +5,7 @@ use chrono::Local;
use diesel::{Insertable, Queryable}; use diesel::{Insertable, Queryable};
use quickxml_to_serde::{xml_string_to_json, Config}; use quickxml_to_serde::{xml_string_to_json, Config};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use serde_json::json; use serde_json::{json, Map};
#[derive(Queryable, Serialize, Insertable, Deserialize)] #[derive(Queryable, Serialize, Insertable, Deserialize)]
#[table_name = "timetable"] #[table_name = "timetable"]
@ -20,6 +20,7 @@ pub struct Timetable {
pub struct TimetableData { pub struct TimetableData {
pub count: usize, pub count: usize,
pub courses: Vec<rocket::serde::json::Value>, pub courses: Vec<rocket::serde::json::Value>,
pub info: String,
} }
async fn get_today_timetable_xml() -> serde_json::value::Value { async fn get_today_timetable_xml() -> serde_json::value::Value {
@ -115,9 +116,28 @@ pub async fn get_today_timetable(_conn: DbConn) -> Vec<Timetable> {
dbg!("Failed: {:?}", &i); dbg!("Failed: {:?}", &i);
} }
} }
let empty_list = serde_json::Value::Array(Vec::new());
let empty_obj = serde_json::Value::Object(Map::new());
let info_list = xml
.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("ZusatzInfo")
.unwrap_or(&empty_obj)
.get("ZiZeile")
.unwrap_or(&empty_list)
.as_array()
.unwrap();
let mut info = String::new();
for item in info_list {
info.push_str(item.as_str().unwrap_or("\r\n"));
info.push_str("\r\n");
}
let response = TimetableData { let response = TimetableData {
count: plan.len(), count: plan.len(),
courses, courses,
info,
}; };
let timetable_element = Timetable { let timetable_element = Timetable {
date: String::from( date: String::from(
@ -188,9 +208,28 @@ pub async fn get_timetable(_conn: DbConn) -> Vec<Timetable> {
dbg!("Failed: {:?}", &i); dbg!("Failed: {:?}", &i);
} }
} }
let empty_list = serde_json::Value::Array(Vec::new());
let empty_obj = serde_json::Value::Object(Map::new());
let info_list = xml
.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("ZusatzInfo")
.unwrap_or(&empty_obj)
.get("ZiZeile")
.unwrap_or(&empty_list)
.as_array()
.unwrap();
let mut info = String::new();
for item in info_list {
info.push_str(item.as_str().unwrap_or("\r\n"));
info.push_str("\r\n");
}
let response = TimetableData { let response = TimetableData {
count: plan.len(), count: plan.len(),
courses, courses,
info,
}; };
let timetable_element = Timetable { let timetable_element = Timetable {
date: String::from( date: String::from(
@ -233,9 +272,32 @@ pub async fn get_timetable(_conn: DbConn) -> Vec<Timetable> {
} }
pub async fn get_today_class_timetable(_conn: DbConn, class: String) -> TimetableData { pub async fn get_today_class_timetable(_conn: DbConn, class: String) -> TimetableData {
let xml = get_today_timetable_xml().await;
let classes = get_today_timetable_xml_data().await; let classes = get_today_timetable_xml_data().await;
let courses: Vec<rocket::serde::json::Value> = Vec::new(); let courses: Vec<rocket::serde::json::Value> = Vec::new();
let mut response = TimetableData { count: 0, courses }; let empty_list = serde_json::Value::Array(Vec::new());
let empty_obj = serde_json::Value::Object(Map::new());
let info_list = xml
.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("ZusatzInfo")
.unwrap_or(&empty_obj)
.get("ZiZeile")
.unwrap_or(&empty_list)
.as_array()
.unwrap();
let mut info = String::new();
for item in info_list {
info.push_str(item.as_str().unwrap_or("\r\n"));
info.push_str("\r\n");
}
let mut response = TimetableData {
count: 0,
courses,
info,
};
for i in classes.iter() { for i in classes.iter() {
if i.as_object() if i.as_object()
.unwrap() .unwrap()
@ -277,9 +339,32 @@ pub async fn get_today_class_timetable(_conn: DbConn, class: String) -> Timetabl
} }
pub async fn get_class_timetable(_conn: DbConn, class: String) -> TimetableData { pub async fn get_class_timetable(_conn: DbConn, class: String) -> TimetableData {
let xml = get_timetable_xml().await;
let classes = get_timetable_xml_data().await; let classes = get_timetable_xml_data().await;
let courses: Vec<rocket::serde::json::Value> = Vec::new(); let courses: Vec<rocket::serde::json::Value> = Vec::new();
let mut response = TimetableData { count: 0, courses }; let empty_list = serde_json::Value::Array(Vec::new());
let empty_obj = serde_json::Value::Object(Map::new());
let info_list = xml
.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("ZusatzInfo")
.unwrap_or(&empty_obj)
.get("ZiZeile")
.unwrap_or(&empty_list)
.as_array()
.unwrap();
let mut info = String::new();
for item in info_list {
info.push_str(item.as_str().unwrap_or("\r\n"));
info.push_str("\r\n");
}
let mut response = TimetableData {
count: 0,
courses,
info,
};
for i in classes.iter() { for i in classes.iter() {
if i.as_object() if i.as_object()
.unwrap() .unwrap()