backend/src/minecraft_session/profile.rs

56 lines
1.6 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)]
pub struct ProfileQuery {
uuid: String,
}
pub async fn profile(
State(pool): State<Arc<Pool<PostgresConnectionManager<NoTls>>>>,
Query(params): Query<ProfileQuery>,
) -> 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()
};
2025-01-20 04:17:13 +03:00
let query = "SELECT
username,
TO_CHAR(skin_date, 'YYYY-MM-DD_HH24-MI-SS') AS skin_date,
TO_CHAR(cape_date, 'YYYY-MM-DD_HH24-MI-SS') AS cape_date
FROM accounts
WHERE uuid = $1";
2025-01-19 22:08:10 +03:00
2024-12-28 21:35:45 +03:00
let row = match conn.query_one(query, &[&params.uuid]).await {
Ok(row) => row,
Err(_) => return (StatusCode::NOT_FOUND, Json(serde_json::json!({
"error": "NotFound",
"errorMessage": "User not found"
}))).into_response()
};
let username: String = row.get("username");
2025-01-19 22:08:10 +03:00
let skin_date: String = row.get("skin_date");
let cape_date: String = row.get("cape_date");
2024-12-28 21:35:45 +03:00
2025-01-19 22:08:10 +03:00
create_profile_response(params.uuid, username, skin_date, cape_date)
2024-12-28 21:35:45 +03:00
}