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

127 lines
3.6 KiB
Rust
Raw Normal View History

use askama::Template;
use axum::{
extract::{Path, State},
response::{Html, IntoResponse as _, Response},
};
use phono_backends::{pg_acl::PgPrivilegeType, pg_attribute::PgAttribute};
use phono_models::{
accessors::{Accessor, Actor, portal::PortalAccessor},
workspace::Workspace,
};
use serde::{Deserialize, Serialize};
use sqlx::postgres::types::Oid;
use uuid::Uuid;
2026-01-13 22:31:50 +00:00
use validator::Validate;
use crate::{
app::AppDbConn,
errors::AppError,
2026-01-13 22:31:50 +00:00
extractors::ValidatedForm,
navigator::Navigator,
settings::Settings,
user::CurrentUser,
workspace_nav::{NavLocation, RelLocation, WorkspaceNav},
workspace_pooler::{RoleAssignment, WorkspacePooler},
};
#[derive(Debug, Deserialize)]
pub(super) struct PathParams {
portal_id: Uuid,
rel_oid: u32,
workspace_id: Uuid,
}
2026-01-13 22:31:50 +00:00
#[derive(Debug, Deserialize, Validate)]
pub(super) struct FormBody {
#[serde(default)]
subfilter: String,
}
/// HTTP GET handler for the table viewer page of a [`Portal`]. This handler
/// performs some relatively simple queries pertaining to table structure, but
/// the bulk of the query logic resides in the [`super::get_data_handler`]
/// module.
pub(super) async fn get(
State(settings): State<Settings>,
State(mut pooler): State<WorkspacePooler>,
AppDbConn(mut app_db): AppDbConn,
CurrentUser(user): CurrentUser,
navigator: Navigator,
Path(PathParams {
portal_id,
rel_oid,
workspace_id,
}): Path<PathParams>,
2026-01-13 22:31:50 +00:00
ValidatedForm(FormBody {
subfilter: subfilter_str,
}): ValidatedForm<FormBody>,
) -> Result<Response, AppError> {
let mut workspace_client = pooler
.acquire_for(workspace_id, RoleAssignment::User(user.id))
.await?;
let portal = PortalAccessor::new()
.id(portal_id)
.as_actor(Actor::User(user.id))
.verify_workspace_id(workspace_id)
.verify_rel_oid(Oid(rel_oid))
.verify_rel_permissions([PgPrivilegeType::Select])
.using_app_db(&mut app_db)
.using_workspace_client(&mut workspace_client)
.fetch_one()
.await?;
let attrs = PgAttribute::all_for_rel(portal.class_oid)
.fetch_all(&mut workspace_client)
.await?;
let attr_names: Vec<String> = attrs.iter().map(|attr| attr.attname.clone()).collect();
#[derive(Clone, Debug, Serialize)]
struct ColumnInfo {
name: String,
regtype: String,
}
let columns: Vec<ColumnInfo> = attrs
.iter()
.map(|attr| ColumnInfo {
name: attr.attname.clone(),
regtype: attr.regtype.clone(),
})
.collect();
let workspace = Workspace::with_id(portal.workspace_id)
.fetch_one(&mut app_db)
.await?;
#[derive(Template)]
2026-01-19 18:48:14 +00:00
#[template(path = "relations_single/portal_table.html")]
struct ResponseTemplate {
columns: Vec<ColumnInfo>,
attr_names: Vec<String>,
filter: String,
settings: Settings,
2026-01-13 22:31:50 +00:00
subfilter_str: String,
navbar: WorkspaceNav,
}
Ok(Html(
ResponseTemplate {
columns,
attr_names,
filter: portal.filter,
navbar: WorkspaceNav::builder()
.navigator(navigator)
.workspace(workspace)
.populate_rels(&mut app_db, &mut workspace_client)
.await?
.current(NavLocation::Rel(
portal.class_oid,
Some(RelLocation::Portal(portal.id)),
))
.build()?,
2026-01-13 22:31:50 +00:00
subfilter_str,
settings,
}
.render()?,
)
.into_response())
}