pub async fn post_delegate(
request: HttpRequest,
form: web::Form<DelegationData>,
data: web::Data<WebData<'_>>,
data_rw: web::Data<DataBaseRw>,
) -> HttpResponse {
let cached_data = {
let mut cache = data.cache_delegation_game.lock().await;
cache.remove(&form.csrf)
};
let cached_data = if let Some(cd) = cached_data {
cd
} else {
log::warn!("Unknown data for CSRF: {}", form.csrf);
return HttpResponse::BadRequest().body("Incorrect");
};
let jar = request_to_jar(request);
let user = if let Some(u) = jar
.private(&data.cookies_key)
.get("auth")
.map(|x| x.value().to_lowercase())
{
u
} else {
log::warn!("Incorrect user");
return HttpResponse::BadRequest().body("Incorrect");
};
if form.submit == "Accept Delegate" && user == cached_data.2 {
let dbclient_rw = match data_rw.0.get().await {
Ok(c) => c,
Err(e) => {
log::error!("Pool RW error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
match dbclient_rw
.execute(
"delete from games.query_delegation where game_uid = $1 and player_name = $2;",
&[&cached_data.0, &cached_data.1],
)
.await
{
Ok(_) => (),
Err(e) => {
log::error!("Pool RW statement error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
match dbclient_rw
.execute("update games.players set delegate_name = $3 where game_uid = $1 and player_name = $2;", &[&cached_data.0, &cached_data.1, &cached_data.2])
.await
{
Ok(_) => (),
Err(e) => {
log::error!("Pool RW statement error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
} else if form.submit == "Reject Delegate" && (user == cached_data.1 || user == cached_data.2) {
let dbclient_rw = match data_rw.0.get().await {
Ok(c) => c,
Err(e) => {
log::error!("Pool RW error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let stmt = match dbclient_rw
.prepare("delete from games.query_delegation where game_uid = $1 and player_name = $2 and delegate_name = $3;")
.await
{
Ok(stmt) => stmt,
Err(e) => {
log::error!("Pool RW statement error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
match dbclient_rw
.execute(&stmt, &[&cached_data.0, &cached_data.1, &cached_data.2])
.await
{
Ok(_) => (),
Err(e) => {
log::error!("Pool RW execute insert error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
} else {
log::warn!("Incorrect user submit {}", form.submit);
return HttpResponse::BadRequest().body("Incorrect");
}
HttpResponse::Found()
.append_header((
header::LOCATION,
format!("slow-game-{}.html", cached_data.0),
))
.finish()
}