feat: Reporting

This commit is contained in:
2026-02-09 16:47:46 +01:00
parent 0f48c24009
commit 7090595505
3 changed files with 70 additions and 1 deletions

View File

@@ -3,11 +3,14 @@ mod models;
mod schedule; mod schedule;
mod teacher_absence; mod teacher_absence;
mod all; mod all;
mod report;
use std::sync::RwLock; use std::sync::RwLock;
pub use models::*; pub use models::*;
use crate::report::{report_impl, ReportLocation};
uniffi::setup_scaffolding!(); uniffi::setup_scaffolding!();
#[derive(uniffi::Object)] #[derive(uniffi::Object)]
@@ -43,4 +46,9 @@ impl JecnaSuplClient {
let provider = self.provider_url.read().unwrap(); let provider = self.provider_url.read().unwrap();
all::get_all_impl(&provider) all::get_all_impl(&provider)
} }
pub fn report(&self, content: String, class: String, report_location: ReportLocation) -> Result<(), SuplError> {
let provider = self.provider_url.read().unwrap();
report_impl(&provider, content, class, report_location)
}
} }

View File

@@ -91,7 +91,7 @@ pub struct DailyData {
#[serde(rename = "takesPlace")] #[serde(rename = "takesPlace")]
pub takes_place: String, pub takes_place: String,
#[serde(rename = "reservedRooms")] #[serde(rename = "reservedRooms")]
pub reserved_rooms: Vec<String>, pub reserved_rooms: Vec<Option<String>>,
} }
#[derive(Deserialize, Debug, uniffi::Record, Clone)] #[derive(Deserialize, Debug, uniffi::Record, Clone)]

61
src/report.rs Normal file
View File

@@ -0,0 +1,61 @@
use serde::Serialize;
use crate::SuplError;
#[derive(uniffi::Enum)]
pub enum ReportLocation {
Timetable,
TeacherAbsence,
TakesPlace
}
impl ReportLocation {
fn to_str(&self) -> &'static str {
match self {
ReportLocation::Timetable => "TIMETABLE",
ReportLocation::TeacherAbsence => "ABSENCE",
ReportLocation::TakesPlace => "TAKES_PLACE"
}
}
}
#[derive(Serialize)]
struct ReportRequest {
content: String,
class: String,
location: String,
}
pub fn report_impl(
provider: &str,
content: String,
class: String,
report_location: ReportLocation,
) -> Result<(), SuplError> {
let trimmed = provider.trim_end_matches('/');
let url = format!("{}/versioned/v3", trimmed);
let body = ReportRequest {
content,
class,
location: report_location.to_str().to_string(),
};
let json = serde_json::to_string(&body)
.map_err(|e| SuplError::ParseError { reason: e.to_string() })?;
let response = minreq::post(url)
.with_header("Content-Type", "application/json")
.with_body(json)
.send()
.map_err(|e| SuplError::NetworkError { reason: e.to_string() })?;
if response.status_code >= 400 {
return Err(SuplError::RuntimeError {
reason: format!("Server returned HTTP {}", response.status_code),
});
}
Ok(())
}