fix(api): search for teacher field until found

This commit is contained in:
Mariano Riefolo 2024-10-03 19:13:53 +02:00
parent c682ca2893
commit c5c6fb97e4

View File

@ -5,7 +5,7 @@ use axum::{
routing::get,
Router,
};
use csv::StringRecord;
use csv::{Reader, StringRecord};
use failure::Error;
use scraper::{Html, Selector};
use serde::Deserialize;
@ -115,7 +115,7 @@ pub struct FilterByTeacherQuery {
}
pub struct Header {
teacher: u8,
teacher: Option<u8>,
weekdays: HashMap<String, Range<usize>>,
}
@ -132,11 +132,11 @@ pub async fn get_teacher(Query(params): Query<FilterByTeacherQuery>) -> impl Int
let csv_content = fs::read_to_string(&filename).unwrap();
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
let header = get_header(rdr.headers().unwrap());
let header = get_header_wrapper(&mut rdr);
for record in rdr.records() {
let record = record.unwrap();
if record[header.teacher as usize] == params.professore {
if record[header.teacher.unwrap() as usize] == params.professore {
let mut result: HashMap<String, Vec<&str>> = HashMap::new();
for (i, cell) in record.iter().enumerate() {
for (j, range) in header.weekdays.clone() {
@ -174,7 +174,7 @@ pub async fn get_class(Query(params): Query<FilterByClassQuery>) -> impl IntoRes
let csv_content = fs::read_to_string(&filename).unwrap();
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
let header = get_header(rdr.headers().unwrap());
let header = get_header_wrapper(&mut rdr);
let mut result: HashMap<String, Vec<String>> = HashMap::new();
@ -192,7 +192,8 @@ pub async fn get_class(Query(params): Query<FilterByClassQuery>) -> impl IntoRes
v.resize(range.end - range.start, String::new());
v
});
value[i - range.start] = record[header.teacher as usize].to_string();
value[i - range.start] =
record[header.teacher.unwrap() as usize].to_string();
}
}
}
@ -209,9 +210,13 @@ pub async fn get_class(Query(params): Query<FilterByClassQuery>) -> impl IntoRes
}
}
pub fn get_header(record: &StringRecord) -> Header {
let mut docente: u8 = 0;
pub fn get_header(record: &StringRecord, header: Option<Header>) -> Header {
let mut docente: Option<u8> = None;
let mut weekdays = HashMap::new();
if let Some(h) = header {
docente = h.teacher;
weekdays = h.weekdays;
}
for (i, field) in record.iter().enumerate() {
let next_index = record
@ -223,7 +228,7 @@ pub fn get_header(record: &StringRecord) -> Header {
let field = field.replace(" ", "").to_uppercase();
if field == "DOCENTE" {
docente = i as u8;
docente = Some(i as u8);
} else if field.starts_with("LUN") {
weekdays.insert("Lunedì".to_string(), i..next_index + i + 1);
} else if field.starts_with("MAR") {
@ -245,6 +250,16 @@ pub fn get_header(record: &StringRecord) -> Header {
}
}
pub fn get_header_wrapper(rdr: &mut Reader<&[u8]>) -> Header {
let mut header = get_header(rdr.headers().unwrap(), None);
let mut records = rdr.records();
while header.teacher.is_none() {
let record = records.next().unwrap().unwrap();
header = get_header(&record, Some(header));
}
header
}
#[derive(Deserialize)]
pub struct GetTeachersQuery {
id: u8,
@ -263,7 +278,7 @@ pub async fn get_teachers(Query(params): Query<GetTeachersQuery>) -> impl IntoRe
let csv_content = fs::read_to_string(&filename).unwrap();
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
let header = get_header(rdr.headers().unwrap());
let header = get_header_wrapper(&mut rdr);
let mut teachers = Vec::new();
@ -273,7 +288,7 @@ pub async fn get_teachers(Query(params): Query<GetTeachersQuery>) -> impl IntoRe
Err(_) => continue,
};
if let Some(teacher) = record.get(header.teacher as usize) {
if let Some(teacher) = record.get(header.teacher.unwrap() as usize) {
if !teacher.is_empty() {
teachers.push(teacher.to_owned());
}
@ -314,7 +329,8 @@ pub async fn get_classes(Query(params): Query<GetClassesQuery>) -> impl IntoResp
let csv_content = fs::read_to_string(&filename).unwrap();
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
let header = get_header(rdr.headers().unwrap());
let header = get_header_wrapper(&mut rdr);
println!("weekdays: {:?}", header.weekdays);
let mut classes: Vec<HashMap<char, HashSet<String>>> = vec![HashMap::new(); 5];