add templates for reports

This commit is contained in:
Mitchell Marino 2023-04-13 23:05:03 -05:00
parent 8835413257
commit ebc86c268d
4 changed files with 107 additions and 18 deletions

View File

@ -16,3 +16,5 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }
sha256 = "1.1" sha256 = "1.1"
jsonwebtoken = "8.3" jsonwebtoken = "8.3"
tera = "1"
lazy_static = "1.4"

View File

@ -4,6 +4,7 @@ ENV DATABASE_URL="postgres://school_app_api_user:school_app_api_pass@mdev.local/
WORKDIR /usr/src/school_app_api WORKDIR /usr/src/school_app_api
COPY . . COPY . .
RUN apt update && apt install -y pkg-config libssl-dev
RUN cargo install sqlx-cli RUN cargo install sqlx-cli
RUN cargo install --path . RUN cargo install --path .

View File

@ -1,3 +1,8 @@
use crate::{
jwt::handle_token,
models::{ReportQuery, Role, UserReportEntry},
AppState,
};
use axum::{ use axum::{
extract::{Query, State}, extract::{Query, State},
http::StatusCode, http::StatusCode,
@ -5,14 +10,27 @@ use axum::{
Json, Json,
}; };
use axum_auth::AuthBearer; use axum_auth::AuthBearer;
use lazy_static::lazy_static;
use serde_json::json; use serde_json::json;
use sqlx::query_as; use sqlx::{
query_as,
use crate::{ types::chrono::{DateTime, Local, NaiveDate},
jwt::handle_token,
models::{ReportQuery, Role, UserReportEntry},
AppState,
}; };
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( pub async fn get_report(
AuthBearer(token): AuthBearer, AuthBearer(token): AuthBearer,
@ -20,8 +38,8 @@ pub async fn get_report(
Query(report_query): Query<ReportQuery>, Query(report_query): Query<ReportQuery>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let token_data = match handle_token(token, &app_state, Role::Teacher) { let token_data = match handle_token(token, &app_state, Role::Teacher) {
Ok(value) => value, Ok(token_data) => token_data,
Err(value) => return value, Err(err) => return err,
}; };
let result = query_as!( let result = query_as!(
@ -46,14 +64,36 @@ pub async fn get_report(
.fetch_all(&app_state.db_pool) .fetch_all(&app_state.db_pool)
.await; .await;
match result { let records = match result {
Ok(record) => (StatusCode::OK, Json(json!({ "data": record }))).into_response(), Ok(records) => records,
Err(err) => ( Err(err) => {
StatusCode::BAD_REQUEST, return (
Json(json!({ StatusCode::BAD_REQUEST,
"error": format!("Unknown error creating event: {}", err) Json(json!({
})), "error": format!("Unknown error creating event: {}", err)
) })),
.into_response(), )
} .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!()
} }

46
templates/report.html Normal file
View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<title>Student Point Report Grade {{ grade }}</title>
<style>
/* zebra striping */
tr:nth-child(even) {
background-color: #f2f2f2;
}
/* table styling */
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
border: 1px solid #ddd;
}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<h1>Student Point Report Grade {{ grade }}</h1>
<p>Date: {{ date }}</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Points</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.name }}</td>
<td>{{ user.points }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>