2025-09-14 16:19:44 -04:00
|
|
|
use axum::{
|
|
|
|
|
debug_handler,
|
|
|
|
|
extract::{Path, State},
|
|
|
|
|
response::Response,
|
|
|
|
|
};
|
|
|
|
|
// [`axum_extra`]'s form extractor is preferred:
|
|
|
|
|
// https://docs.rs/axum-extra/0.10.1/axum_extra/extract/struct.Form.html#differences-from-axumextractform
|
|
|
|
|
use axum_extra::extract::Form;
|
|
|
|
|
use interim_models::{
|
2025-10-22 00:43:53 -07:00
|
|
|
field::Field, portal::Portal, presentation::Presentation, workspace::Workspace,
|
2025-09-14 16:19:44 -04:00
|
|
|
};
|
|
|
|
|
use interim_pgtypes::{escape_identifier, pg_class::PgClass};
|
|
|
|
|
use serde::Deserialize;
|
2025-10-01 22:36:19 -07:00
|
|
|
use sqlx::{postgres::types::Oid, query};
|
2025-09-14 16:19:44 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
2025-09-23 13:08:51 -07:00
|
|
|
app::{App, AppDbConn},
|
2025-10-22 00:43:53 -07:00
|
|
|
errors::AppError,
|
2025-10-01 22:36:19 -07:00
|
|
|
navigator::{Navigator, NavigatorPage},
|
2025-10-21 18:58:09 +00:00
|
|
|
presentation_form::PresentationForm,
|
2025-11-01 00:17:07 +00:00
|
|
|
roles::get_writer_role,
|
2025-09-14 16:19:44 -04:00
|
|
|
user::CurrentUser,
|
2025-09-23 13:08:51 -07:00
|
|
|
workspace_pooler::{RoleAssignment, WorkspacePooler},
|
2025-09-14 16:19:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub(super) struct PathParams {
|
|
|
|
|
portal_id: Uuid,
|
|
|
|
|
rel_oid: u32,
|
|
|
|
|
workspace_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 06:23:50 +00:00
|
|
|
// FIXME: validate name, prevent leading underscore
|
2025-09-14 16:19:44 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub(super) struct FormBody {
|
|
|
|
|
name: String,
|
2025-10-21 18:58:09 +00:00
|
|
|
table_label: String,
|
|
|
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
|
presentation_form: PresentationForm,
|
2025-09-14 16:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// HTTP POST handler for adding a [`Field`] to a [`Portal`]. If the field name
|
|
|
|
|
/// does not match a column in the backing database, a new column is created
|
|
|
|
|
/// with a compatible type.
|
|
|
|
|
///
|
|
|
|
|
/// This handler expects 3 path parameters with the structure described by
|
|
|
|
|
/// [`PathParams`].
|
|
|
|
|
#[debug_handler(state = App)]
|
|
|
|
|
pub(super) async fn post(
|
|
|
|
|
State(mut workspace_pooler): State<WorkspacePooler>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
CurrentUser(user): CurrentUser,
|
|
|
|
|
navigator: Navigator,
|
|
|
|
|
Path(PathParams {
|
|
|
|
|
portal_id,
|
2025-10-01 22:36:19 -07:00
|
|
|
rel_oid,
|
2025-09-14 16:19:44 -04:00
|
|
|
workspace_id,
|
|
|
|
|
}): Path<PathParams>,
|
|
|
|
|
Form(form): Form<FormBody>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
2025-10-22 00:43:53 -07:00
|
|
|
// FIXME: Check workspace authorization.
|
2025-09-14 16:19:44 -04:00
|
|
|
// FIXME ensure workspace corresponds to rel/portal, and that user has
|
|
|
|
|
// permission to access/alter both as needed.
|
|
|
|
|
|
|
|
|
|
let portal = Portal::with_id(portal_id).fetch_one(&mut app_db).await?;
|
|
|
|
|
let workspace = Workspace::with_id(portal.workspace_id)
|
|
|
|
|
.fetch_one(&mut app_db)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let mut workspace_client = workspace_pooler
|
|
|
|
|
.acquire_for(workspace.id, RoleAssignment::User(user.id))
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let class = PgClass::with_oid(portal.class_oid)
|
|
|
|
|
.fetch_one(&mut workspace_client)
|
|
|
|
|
.await?;
|
|
|
|
|
|
2025-10-21 18:58:09 +00:00
|
|
|
let presentation = Presentation::try_from(form.presentation_form)?;
|
2025-09-14 16:19:44 -04:00
|
|
|
|
|
|
|
|
query(&format!(
|
|
|
|
|
"alter table {ident} add column if not exists {col} {typ}",
|
|
|
|
|
ident = class.get_identifier(),
|
|
|
|
|
col = escape_identifier(&form.name),
|
|
|
|
|
typ = presentation.attr_data_type_fragment(),
|
|
|
|
|
))
|
|
|
|
|
.execute(workspace_client.get_conn())
|
|
|
|
|
.await?;
|
2025-10-25 05:32:22 +00:00
|
|
|
query(&format!(
|
|
|
|
|
"grant insert ({col}), update ({col}) on table {ident} to {writer_role}",
|
|
|
|
|
col = escape_identifier(&form.name),
|
|
|
|
|
ident = class.get_identifier(),
|
|
|
|
|
writer_role = escape_identifier(&get_writer_role(class.clone())?),
|
|
|
|
|
))
|
|
|
|
|
.execute(workspace_client.get_conn())
|
|
|
|
|
.await?;
|
2025-09-14 16:19:44 -04:00
|
|
|
|
|
|
|
|
Field::insert()
|
|
|
|
|
.portal_id(portal.id)
|
|
|
|
|
.name(form.name)
|
2025-10-21 18:58:09 +00:00
|
|
|
.table_label(if form.table_label.is_empty() {
|
2025-09-14 16:19:44 -04:00
|
|
|
None
|
|
|
|
|
} else {
|
2025-10-21 18:58:09 +00:00
|
|
|
Some(form.table_label)
|
2025-09-14 16:19:44 -04:00
|
|
|
})
|
|
|
|
|
.presentation(presentation)
|
|
|
|
|
.build()?
|
|
|
|
|
.insert(&mut app_db)
|
|
|
|
|
.await?;
|
|
|
|
|
|
2025-10-01 22:36:19 -07:00
|
|
|
Ok(navigator
|
|
|
|
|
.portal_page()
|
|
|
|
|
.workspace_id(workspace_id)
|
|
|
|
|
.rel_oid(Oid(rel_oid))
|
|
|
|
|
.portal_id(portal_id)
|
|
|
|
|
.build()?
|
|
|
|
|
.redirect_to())
|
2025-09-14 16:19:44 -04:00
|
|
|
}
|