phonograph/phono-server/src/routes/relations_single/settings_handler.rs

81 lines
2.2 KiB
Rust
Raw Normal View History

2025-10-01 22:36:19 -07:00
use askama::Template;
use axum::{
debug_handler,
extract::{Path, State},
response::{Html, IntoResponse},
};
use phono_backends::pg_class::PgClass;
use phono_models::workspace::Workspace;
2025-10-01 22:36:19 -07:00
use serde::Deserialize;
use sqlx::postgres::types::Oid;
use uuid::Uuid;
use crate::{
app::{App, AppDbConn},
2025-10-22 00:43:53 -07:00
errors::AppError,
2025-10-01 22:36:19 -07:00
navigator::Navigator,
settings::Settings,
user::CurrentUser,
workspace_nav::{NavLocation, RelLocation, WorkspaceNav},
workspace_pooler::{RoleAssignment, WorkspacePooler},
};
#[derive(Debug, Deserialize)]
pub(super) struct PathParams {
rel_oid: u32,
workspace_id: Uuid,
}
/// HTTP GET handler for table settings, including renaming, access control,
/// and deletion.
#[debug_handler(state = App)]
pub(super) async fn get(
State(settings): State<Settings>,
CurrentUser(user): CurrentUser,
AppDbConn(mut app_db): AppDbConn,
Path(PathParams {
rel_oid,
workspace_id,
}): Path<PathParams>,
navigator: Navigator,
State(mut pooler): State<WorkspacePooler>,
) -> Result<impl IntoResponse, AppError> {
2025-10-22 00:43:53 -07:00
// FIXME: Check workspace authorization.
2025-10-01 22:36:19 -07:00
// FIXME ensure workspace corresponds to rel/portal, and that user has
// permission to access/alter both as needed.
let workspace = Workspace::with_id(workspace_id)
.fetch_one(&mut app_db)
.await?;
let mut workspace_client = pooler
.acquire_for(workspace.id, RoleAssignment::User(user.id))
.await?;
let rel = PgClass::with_oid(Oid(rel_oid))
.fetch_one(&mut workspace_client)
.await?;
#[derive(Debug, Template)]
#[template(path = "relations_single/settings.html")]
struct ResponseTemplate {
rel: PgClass,
settings: Settings,
workspace_nav: WorkspaceNav,
}
Ok(Html(
ResponseTemplate {
workspace_nav: WorkspaceNav::builder()
.navigator(navigator)
.workspace(workspace)
.populate_rels(&mut app_db, &mut workspace_client)
.await?
.current(NavLocation::Rel(Oid(rel_oid), Some(RelLocation::Sharing)))
.build()?,
rel,
settings,
}
.render()?,
))
}