use actix_web::http::header;
use actix_web::{web, HttpRequest, HttpResponse};
use crate::pages::templates::MY;
use crate::pages::{insert_security_headers, request_to_jar, CommonAuthInfo};
use crate::{DataBaseRo, WebData};
#[derive(serde_derive::Serialize)]
struct PageData {
common_auth_info: CommonAuthInfo,
games: Vec<GameData>,
}
#[derive(serde_derive::Serialize)]
struct GameData {
gameuid: String,
gameuidenc: String,
status: Option<String>,
}
pub async fn my(
request: HttpRequest,
data: web::Data<WebData<'_>>,
data_ro: web::Data<DataBaseRo>,
) -> HttpResponse {
let jar = request_to_jar(request);
let u = jar
.private(&data.cookies_key)
.get("auth")
.map(|x| x.value().to_string());
if let Some(user) = u {
let dbclient = match data_ro.0.get().await {
Ok(client) => client,
Err(e) => {
log::error!("{}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let stmt = match dbclient
.prepare("select g.game_uid, g.status::text from games.games g inner join games.players p on g.game_uid = p.game_uid where p.player_name = $1 order by g.game_uid;")
.await
{
Ok(stmt) => stmt,
Err(e) => {
log::error!("{}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let rows = match dbclient.query(&stmt, &[&user]).await {
Ok(rows) => rows,
Err(e) => {
log::error!("{}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let mut games = Vec::with_capacity(rows.len());
for row in rows {
let gameuid = row.get::<_, &str>(0);
let status = row.get::<_, Option<&str>>(1);
games.push(GameData {
gameuid: gameuid.to_string(),
gameuidenc: pct_str::PctString::encode(gameuid.chars(), pct_str::URIReserved)
.into_string(),
status: status.map(str::to_string),
});
}
let body = match data.handlebars.render(
MY,
&PageData {
common_auth_info: CommonAuthInfo { user: Some(user) },
games,
},
) {
Ok(b) => b,
Err(e) => {
log::error!("Render index error: {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
insert_security_headers(HttpResponse::Ok()).body(body)
} else {
HttpResponse::Found()
.append_header((header::LOCATION, "login.html"))
.finish()
}
}