From c5c6fb97e47f59e0699fa62a82263a3de3109334 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Thu, 3 Oct 2024 19:13:53 +0200 Subject: [PATCH] fix(api): search for teacher field until found --- src/api/mod.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 9c95163..62348ec 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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, weekdays: HashMap>, } @@ -132,11 +132,11 @@ pub async fn get_teacher(Query(params): Query) -> 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> = 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) -> 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> = HashMap::new(); @@ -192,7 +192,8 @@ pub async fn get_class(Query(params): Query) -> 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) -> impl IntoRes } } -pub fn get_header(record: &StringRecord) -> Header { - let mut docente: u8 = 0; +pub fn get_header(record: &StringRecord, header: Option
) -> Header { + let mut docente: Option = 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) -> 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) -> 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) -> 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>> = vec![HashMap::new(); 5];