backend/src/minecraft_session/has_joined.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

2024-12-28 21:35:45 +03:00
use std::sync::Arc;
use axum::{
Json,
extract::{State, Query},
response::IntoResponse,
http::StatusCode
};
use serde::Deserialize;
use bb8::Pool;
use bb8_postgres::PostgresConnectionManager;
use tokio_postgres::NoTls;
use crate::minecraft_session::create_profile_response;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HasJoinedQuery {
username: String,
server_id: String,
}
pub async fn has_joined(
State(pool): State<Arc<Pool<PostgresConnectionManager<NoTls>>>>,
Query(params): Query<HasJoinedQuery>,
) -> impl IntoResponse {
let conn = match pool.get().await {
Ok(conn) => conn,
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
"error": "InternalServerError",
"errorMessage": "Failed to get DB connection"
}))).into_response()
};
let query = "SELECT uuid FROM accounts WHERE username = $1 AND server_id = $2";
let row = match conn.query_one(query, &[&params.username, &params.server_id]).await {
Ok(row) => row,
Err(_) => return (StatusCode::NOT_FOUND, Json(serde_json::json!({
"error": "NotFound",
"errorMessage": "User not found"
}))).into_response()
};
let uuid = row.get("uuid");
create_profile_response(uuid, params.username)
}