diff --git a/Cargo.toml b/Cargo.toml index f0ea626..c0417af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,5 @@ tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } sha256 = "1.1" jsonwebtoken = "8.3" +tera = "1" +lazy_static = "1.4" diff --git a/Dockerfile b/Dockerfile index c87d3f6..671485e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,7 @@ ENV DATABASE_URL="postgres://school_app_api_user:school_app_api_pass@mdev.local/ WORKDIR /usr/src/school_app_api COPY . . +RUN apt update && apt install -y pkg-config libssl-dev RUN cargo install sqlx-cli RUN cargo install --path . diff --git a/src/report.rs b/src/report.rs index b4dbcbb..40053cf 100644 --- a/src/report.rs +++ b/src/report.rs @@ -1,3 +1,8 @@ +use crate::{ + jwt::handle_token, + models::{ReportQuery, Role, UserReportEntry}, + AppState, +}; use axum::{ extract::{Query, State}, http::StatusCode, @@ -5,14 +10,27 @@ use axum::{ Json, }; use axum_auth::AuthBearer; +use lazy_static::lazy_static; use serde_json::json; -use sqlx::query_as; - -use crate::{ - jwt::handle_token, - models::{ReportQuery, Role, UserReportEntry}, - AppState, +use sqlx::{ + query_as, + types::chrono::{DateTime, Local, NaiveDate}, }; +use tera::{Context, Tera}; + +lazy_static! { + pub static ref TEMPLATES: Tera = { + let mut tera = match Tera::new("templates/**/*") { + Ok(t) => t, + Err(e) => { + eprintln!("Parsing error(s): {}", e); + ::std::process::exit(1); + } + }; + tera.autoescape_on(vec![".html", ".sql"]); + tera + }; +} pub async fn get_report( AuthBearer(token): AuthBearer, @@ -20,8 +38,8 @@ pub async fn get_report( Query(report_query): Query, ) -> impl IntoResponse { let token_data = match handle_token(token, &app_state, Role::Teacher) { - Ok(value) => value, - Err(value) => return value, + Ok(token_data) => token_data, + Err(err) => return err, }; let result = query_as!( @@ -46,14 +64,36 @@ pub async fn get_report( .fetch_all(&app_state.db_pool) .await; - match result { - Ok(record) => (StatusCode::OK, Json(json!({ "data": record }))).into_response(), - Err(err) => ( - StatusCode::BAD_REQUEST, - Json(json!({ - "error": format!("Unknown error creating event: {}", err) - })), - ) - .into_response(), - } + let records = match result { + Ok(records) => records, + Err(err) => { + return ( + StatusCode::BAD_REQUEST, + Json(json!({ + "error": format!("Unknown error creating event: {}", err) + })), + ) + .into_response() + } + }; + + // render report + let mut context = Context::new(); + context.insert("grade", &report_query.grade); + context.insert("date", &Local::now().format("%Y-%m-%d").to_string()); + context.insert("users", &records); + let report = match TEMPLATES.render("report.html", &context) { + Ok(report) => report, + Err(err) => { + return ( + StatusCode::BAD_REQUEST, + Json(json!({ + "error": format!("Unknown error creating event: {}", err) + })), + ) + .into_response() + } + }; + + todo!() } diff --git a/templates/report.html b/templates/report.html new file mode 100644 index 0000000..1621fe6 --- /dev/null +++ b/templates/report.html @@ -0,0 +1,46 @@ + + + + Student Point Report Grade {{ grade }} + + + +

Student Point Report Grade {{ grade }}

+

Date: {{ date }}

+ + + + + + + + + {% for user in users %} + + + + + {% endfor %} + +
NamePoints
{{ user.name }}{{ user.points }}
+ +