nanobot/src/routes.rs

61 lines
1.9 KiB
Rust
Raw Normal View History

2023-09-24 00:56:33 +00:00
use crate::PendingMember;
use axum::{http::StatusCode, response::IntoResponse, Json};
#[tracing::instrument]
pub(crate) async fn up() -> impl IntoResponse {
(StatusCode::OK, "Nano is up!")
}
#[derive(serde::Deserialize)]
2023-09-24 00:56:33 +00:00
pub(crate) struct Verify {
id: String,
shortcode: String,
fullname: String,
key: String,
}
2023-09-29 00:06:37 +00:00
#[tracing::instrument(skip_all)]
2023-09-24 00:56:33 +00:00
pub(crate) async fn verify(
pool: sqlx::PgPool,
payload: Option<Json<Verify>>,
key: String,
) -> impl IntoResponse {
match payload {
None => (StatusCode::BAD_REQUEST, "Invalid request body").into_response(),
Some(Json(verify)) => {
if verify.key == key {
2023-09-25 12:23:32 +00:00
let Ok(id) = verify.id.parse::<i64>() else {
return (StatusCode::BAD_REQUEST, "Invalid request body").into_response();
2023-09-24 00:56:33 +00:00
};
2023-09-25 12:23:32 +00:00
2023-09-24 00:56:33 +00:00
// Delete from pending if exists
let _ = crate::db::delete_pending_by_id(&pool, id).await;
match crate::db::insert_pending(
&pool,
PendingMember {
discord_id: id,
shortcode: verify.shortcode.clone(),
realname: verify.fullname.clone(),
},
)
.await
{
2023-12-08 16:42:31 +00:00
Ok(()) => {
2023-09-29 00:06:37 +00:00
tracing::info!(
"ID {} added: {}, {}",
id,
verify.shortcode,
verify.fullname
);
2023-09-24 00:56:33 +00:00
(StatusCode::OK, "Member added to `pending` database").into_response()
}
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response(),
}
} else {
(StatusCode::UNAUTHORIZED, "Auth required").into_response()
}
}
}
}