Skip to content

Commit

Permalink
User page WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Sep 17, 2024
1 parent d1dc175 commit c607b46
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 7 deletions.
37 changes: 34 additions & 3 deletions vzdv-site/src/endpoints/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use axum::{
};
use log::{debug, info, warn};
use minijinja::{context, Environment};
use serde::Serialize;
use std::{collections::HashMap, sync::Arc};
use tower_sessions::Session;
use vzdv::{
sql::{self, Controller},
vatusa,
sql::{self, Certification, Controller},
vatusa, ControllerRating,
};

/// Retrieve and show the user their training records from VATUSA.
Expand Down Expand Up @@ -132,6 +133,12 @@ async fn page_user(
session: Session,
Path(cid): Path<u32>,
) -> Result<Response, AppError> {
#[derive(Serialize)]
struct CertNameValue<'a> {
name: &'a str,
value: &'a str,
}

let user_info: Option<UserInfo> = session.get(SESSION_USER_INFO_KEY).await?;
let controller: Option<Controller> = sqlx::query_as(sql::GET_CONTROLLER_BY_CID)
.bind(cid)
Expand All @@ -149,10 +156,34 @@ async fn page_user(
return Ok(Redirect::to("/facility/roster").into_response());
}
};
let rating_str = ControllerRating::try_from(controller.rating)
.map_err(|err| AppError::GenericFallback("unknown controller rating", err))?
.as_str();

let db_certs: Vec<Certification> = sqlx::query_as(sql::GET_ALL_CERTIFICATIONS_FOR)
.bind(cid)
.fetch_all(&state.db)
.await?;
let mut certifications: Vec<CertNameValue> =
Vec::with_capacity(state.config.training.certifications.len());
let none = String::from("None");
for name in &state.config.training.certifications {
let db_match = db_certs.iter().find(|cert| &cert.name == name);
let value: &str = match db_match {
Some(row) => &row.value,
None => &none,
};
certifications.push(CertNameValue { name, value });
}

dbg!(&controller.discord_id);

let template = state.templates.get_template("user/overview")?;
let rendered: String = template.render(context! {
user_info,
controller
controller,
rating_str,
certifications
})?;
Ok(Html(rendered).into_response())
}
Expand Down
60 changes: 58 additions & 2 deletions vzdv-site/templates/user/overview.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,64 @@

{% block body %}

<h2>{{ controller.first_name }} {{ controller.last_name }}</h2>
<h2 class="pb-3">{{ controller.first_name }} {{ controller.last_name }}</h2>

<!-- TODO -->
<div class="row">
<div class="col-4">
<div class="card">
<div class="card-body p-5">
<h3 class="card-title">Overview</h3>
<p class="card-text">
<strong>CID:</strong> {{ controller.cid }}
<br>
<strong>Rating:</strong> {{ rating_str }}
<br>
<strong>Type:</strong> {% if not controller.is_on_roster %}Guest{% elif not controller.home_facility == 'ZDV' %}Visiting{% else %}Home{% endif %}
<br>
<strong>Joined:</strong> {{ controller.join_date }}
{% if user_info and user_info.is_some_staff %}
<br>
<strong>Discord user ID:</strong> {{ controller.discord_id }}
{% endif %}
</p>
</div>
</div>
</div>
<div class="col-8">
<div class="card">
<div class="card-body p-5">
<h3 class="card-title">Certifications</h3>
<div class="card-text">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Cert</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for cert in certifications %}
<tr>
<td><strong>{{ cert.name }}</strong></td>
<td>
{% if cert.value == 'certified' %}
<span class="text-success">Certified</span>
{% elif cert.value == 'training' %}
<span class="text-warning">Training</span>
{% elif cert.value == 'solo' %}
<span class="text-info">Solo</span>
{% else %}
<span>None</span>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>

{% endblock %}
6 changes: 6 additions & 0 deletions vzdv/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Config {
pub database: ConfigDatabase,
pub staff: ConfigStaff,
pub vatsim: ConfigVatsim,
pub training: ConfigTraining,
pub airports: ConfigAirports,
pub stats: ConfigStats,
pub discord: ConfigDiscord,
Expand Down Expand Up @@ -44,6 +45,11 @@ pub struct ConfigVatsim {
pub vatusa_api_key: String,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct ConfigTraining {
pub certifications: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct ConfigAirports {
pub all: Vec<Airport>,
Expand Down
4 changes: 2 additions & 2 deletions vzdv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl ControllerRating {
}

impl TryFrom<i8> for ControllerRating {
type Error = &'static str;
type Error = anyhow::Error;

fn try_from(value: i8) -> std::result::Result<Self, Self::Error> {
match value {
Expand All @@ -187,7 +187,7 @@ impl TryFrom<i8> for ControllerRating {
10 => Ok(Self::I3),
11 => Ok(Self::SUP),
12 => Ok(Self::ADM),
_ => Err("Unknown controller rating"),
_ => Err(anyhow::anyhow!("Unknown controller rating")),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions vzdv/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ pub const GET_CONTROLLER_BY_DISCORD_ID: &str = "SELECT * FROM controller WHERE d
pub const SET_CONTROLLER_DISCORD_ID: &str = "UPDATE controller SET discord_id=$1 WHERE cid=$2";

pub const GET_ALL_CERTIFICATIONS: &str = "SELECT * FROM certification";
pub const GET_ALL_CERTIFICATIONS_FOR: &str = "SELECT * FROM certification WHERE cid=$1";

pub const GET_ALL_ACTIVITY: &str =
"SELECT * FROM activity LEFT JOIN controller ON activity.cid = controller.cid";
Expand Down

0 comments on commit c607b46

Please sign in to comment.