backend/src/api/v1/guilds/uuid/roles/uuid.rs
Radical fa52412b43
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
feat: make database access more uniform and stop locking as many pool connections
2025-07-21 04:15:04 +02:00

46 lines
1.1 KiB
Rust

use std::sync::Arc;
use ::uuid::Uuid;
use axum::{
Extension, Json,
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
};
use crate::{
AppState,
api::v1::auth::CurrentUser,
error::Error,
objects::{Member, Role},
utils::{CacheFns, global_checks},
};
pub async fn get(
State(app_state): State<Arc<AppState>>,
Path((guild_uuid, role_uuid)): Path<(Uuid, Uuid)>,
Extension(CurrentUser(uuid)): Extension<CurrentUser<Uuid>>,
) -> Result<impl IntoResponse, Error> {
let mut conn = app_state.pool.get().await?;
global_checks(&mut conn, &app_state.config, uuid).await?;
Member::check_membership(&mut conn, uuid, guild_uuid).await?;
if let Ok(cache_hit) = app_state
.cache_pool
.get_cache_key::<Role>(format!("{role_uuid}"))
.await
{
return Ok((StatusCode::OK, Json(cache_hit)).into_response());
}
let role = Role::fetch_one(&mut conn, role_uuid).await?;
app_state
.cache_pool
.set_cache_key(format!("{role_uuid}"), role.clone(), 60)
.await?;
Ok((StatusCode::OK, Json(role)).into_response())
}