2025-07-08 14:37:03 -07:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use askama::Template;
|
|
|
|
|
use axum::{
|
2025-07-08 16:54:51 -07:00
|
|
|
Json,
|
2025-07-08 14:37:03 -07:00
|
|
|
extract::{Path, State},
|
|
|
|
|
response::{Html, IntoResponse as _, Redirect, Response},
|
|
|
|
|
};
|
|
|
|
|
use axum_extra::extract::Form;
|
|
|
|
|
use interim_models::{
|
2025-07-08 16:54:51 -07:00
|
|
|
field::{Encodable, Field},
|
2025-07-08 14:37:03 -07:00
|
|
|
lens::{Lens, LensDisplayType},
|
2025-07-08 16:54:51 -07:00
|
|
|
selection::{AttrFilter, Selection},
|
|
|
|
|
};
|
|
|
|
|
use interim_pgtypes::{
|
|
|
|
|
pg_attribute::{PgAttribute, fetch_attributes_for_rel, fetch_primary_keys_for_rel},
|
|
|
|
|
pg_class::PgClass,
|
2025-07-08 14:37:03 -07:00
|
|
|
};
|
|
|
|
|
use serde::Deserialize;
|
2025-07-08 16:54:51 -07:00
|
|
|
use serde_json::json;
|
2025-07-08 14:37:03 -07:00
|
|
|
use sqlx::{
|
|
|
|
|
postgres::{PgRow, types::Oid},
|
|
|
|
|
query,
|
|
|
|
|
};
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
app_error::{AppError, not_found},
|
|
|
|
|
app_state::AppDbConn,
|
|
|
|
|
base_pooler::BasePooler,
|
|
|
|
|
bases::Base,
|
|
|
|
|
db_conns::{escape_identifier, init_role},
|
|
|
|
|
settings::Settings,
|
|
|
|
|
users::CurrentUser,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct LensesPagePath {
|
|
|
|
|
base_id: Uuid,
|
|
|
|
|
class_oid: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn lenses_page(
|
|
|
|
|
State(settings): State<Settings>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
Path(LensesPagePath { base_id, class_oid }): Path<LensesPagePath>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
// FIXME auth
|
|
|
|
|
let lenses = Lens::fetch_by_rel(base_id, Oid(class_oid), &mut *app_db).await?;
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "lenses.html")]
|
|
|
|
|
struct ResponseTemplate {
|
|
|
|
|
base_id: Uuid,
|
|
|
|
|
class_oid: u32,
|
|
|
|
|
lenses: Vec<Lens>,
|
|
|
|
|
settings: Settings,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Html(
|
|
|
|
|
ResponseTemplate {
|
|
|
|
|
base_id,
|
|
|
|
|
class_oid,
|
|
|
|
|
lenses,
|
|
|
|
|
settings,
|
|
|
|
|
}
|
|
|
|
|
.render()?,
|
|
|
|
|
)
|
|
|
|
|
.into_response())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn add_lens_page_get(
|
|
|
|
|
State(settings): State<Settings>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
Path(LensesPagePath { base_id, class_oid }): Path<LensesPagePath>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
// FIXME auth
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "add_lens.html")]
|
|
|
|
|
struct ResponseTemplate {
|
|
|
|
|
base_id: Uuid,
|
|
|
|
|
class_oid: u32,
|
|
|
|
|
settings: Settings,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Html(
|
|
|
|
|
ResponseTemplate {
|
|
|
|
|
base_id,
|
|
|
|
|
class_oid,
|
|
|
|
|
settings,
|
|
|
|
|
}
|
|
|
|
|
.render()?,
|
|
|
|
|
)
|
|
|
|
|
.into_response())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct AddLensPagePostForm {
|
|
|
|
|
name: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn add_lens_page_post(
|
|
|
|
|
State(Settings { root_path, .. }): State<Settings>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
Path(LensesPagePath { base_id, class_oid }): Path<LensesPagePath>,
|
|
|
|
|
Form(AddLensPagePostForm { name }): Form<AddLensPagePostForm>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
// FIXME auth
|
|
|
|
|
// FIXME csrf
|
|
|
|
|
let lens = Lens::insertable_builder()
|
|
|
|
|
.base_id(base_id)
|
|
|
|
|
.class_oid(Oid(class_oid))
|
|
|
|
|
.name(name)
|
|
|
|
|
.display_type(LensDisplayType::Table)
|
|
|
|
|
.build()?
|
|
|
|
|
.insert(&mut *app_db)
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(Redirect::to(&format!(
|
|
|
|
|
"{root_path}/d/{0}/r/{class_oid}/l/{1}",
|
|
|
|
|
base_id.simple(),
|
|
|
|
|
lens.id.simple()
|
|
|
|
|
))
|
|
|
|
|
.into_response())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct LensPagePath {
|
|
|
|
|
base_id: Uuid,
|
|
|
|
|
class_oid: u32,
|
|
|
|
|
lens_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn lens_page(
|
|
|
|
|
State(settings): State<Settings>,
|
|
|
|
|
State(mut base_pooler): State<BasePooler>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
|
Path(LensPagePath {
|
|
|
|
|
base_id,
|
|
|
|
|
class_oid,
|
|
|
|
|
lens_id,
|
|
|
|
|
}): Path<LensPagePath>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
// FIXME auth
|
|
|
|
|
let base = Base::fetch_by_id(base_id, &mut *app_db)
|
|
|
|
|
.await?
|
|
|
|
|
.ok_or(AppError::NotFound("no base found with that id".to_owned()))?;
|
|
|
|
|
let mut client = base_pooler.acquire_for(base_id).await?;
|
|
|
|
|
|
|
|
|
|
init_role(
|
|
|
|
|
&format!("{}{}", &base.user_role_prefix, ¤t_user.id.simple()),
|
|
|
|
|
&mut client,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// FIXME auth
|
|
|
|
|
|
2025-07-08 16:54:51 -07:00
|
|
|
let class = PgClass::fetch_by_oid(Oid(class_oid), &mut *client)
|
|
|
|
|
.await?
|
|
|
|
|
.ok_or(AppError::NotFound(
|
|
|
|
|
"no relation found with that oid".to_owned(),
|
|
|
|
|
))?;
|
|
|
|
|
let namespace = class.fetch_namespace(&mut *client).await?;
|
2025-07-08 14:37:03 -07:00
|
|
|
|
|
|
|
|
let lens = Lens::fetch_by_id(lens_id, &mut *app_db)
|
|
|
|
|
.await?
|
|
|
|
|
.ok_or(not_found!("no lens found with that id"))?;
|
|
|
|
|
|
|
|
|
|
let attrs = fetch_attributes_for_rel(Oid(class_oid), &mut *client).await?;
|
|
|
|
|
|
|
|
|
|
let selections = lens.fetch_selections(&mut *app_db).await?;
|
|
|
|
|
let mut fields: Vec<Field> = Vec::with_capacity(selections.len());
|
|
|
|
|
for selection in selections.clone() {
|
|
|
|
|
fields.append(&mut selection.resolve_fields_from_attrs(&attrs));
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 16:54:51 -07:00
|
|
|
let pkey_attrs = fetch_primary_keys_for_rel(Oid(class_oid), &mut *client).await?;
|
|
|
|
|
|
2025-07-08 14:37:03 -07:00
|
|
|
const FRONTEND_ROW_LIMIT: i64 = 1000;
|
2025-07-08 16:54:51 -07:00
|
|
|
struct Row {
|
|
|
|
|
pkeys: HashMap<String, Encodable>,
|
|
|
|
|
data: PgRow,
|
|
|
|
|
}
|
|
|
|
|
let rows: Vec<Row> = query(&format!(
|
|
|
|
|
"select {0} from {1}.{2} limit $1",
|
|
|
|
|
pkey_attrs
|
2025-07-08 14:37:03 -07:00
|
|
|
.iter()
|
2025-07-08 16:54:51 -07:00
|
|
|
.chain(attrs.iter())
|
|
|
|
|
.map(|attr| escape_identifier(&attr.attname))
|
2025-07-08 14:37:03 -07:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join(", "),
|
2025-07-08 16:54:51 -07:00
|
|
|
escape_identifier(&namespace.nspname),
|
2025-07-08 14:37:03 -07:00
|
|
|
escape_identifier(&class.relname),
|
|
|
|
|
))
|
|
|
|
|
.bind(FRONTEND_ROW_LIMIT)
|
|
|
|
|
.fetch_all(&mut *client)
|
2025-07-08 16:54:51 -07:00
|
|
|
.await?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|row| {
|
|
|
|
|
let mut pkey_values: HashMap<String, Encodable> = HashMap::new();
|
|
|
|
|
for attr in pkey_attrs.clone() {
|
|
|
|
|
let field = Field::default_from_attr(&attr);
|
|
|
|
|
pkey_values.insert(field.name.clone(), field.get_value_encodable(&row).unwrap());
|
|
|
|
|
}
|
|
|
|
|
Row {
|
|
|
|
|
pkeys: pkey_values,
|
|
|
|
|
data: row,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-07-08 14:37:03 -07:00
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "lens.html")]
|
|
|
|
|
struct ResponseTemplate {
|
|
|
|
|
fields: Vec<Field>,
|
|
|
|
|
all_columns: Vec<PgAttribute>,
|
2025-07-08 16:54:51 -07:00
|
|
|
rows: Vec<Row>,
|
2025-07-08 14:37:03 -07:00
|
|
|
selections_json: String,
|
|
|
|
|
settings: Settings,
|
|
|
|
|
}
|
|
|
|
|
Ok(Html(
|
|
|
|
|
ResponseTemplate {
|
|
|
|
|
all_columns: attrs,
|
|
|
|
|
fields,
|
|
|
|
|
rows,
|
|
|
|
|
selections_json: serde_json::to_string(&selections)?,
|
|
|
|
|
settings,
|
|
|
|
|
}
|
|
|
|
|
.render()?,
|
|
|
|
|
)
|
|
|
|
|
.into_response())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct AddSelectionPageForm {
|
|
|
|
|
column: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn add_selection_page_post(
|
|
|
|
|
State(settings): State<Settings>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
|
Path(LensPagePath {
|
|
|
|
|
base_id,
|
|
|
|
|
class_oid,
|
|
|
|
|
lens_id,
|
|
|
|
|
}): Path<LensPagePath>,
|
|
|
|
|
Form(form): Form<AddSelectionPageForm>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
dbg!(&form);
|
|
|
|
|
// FIXME auth
|
|
|
|
|
// FIXME csrf
|
|
|
|
|
|
|
|
|
|
let lens = Lens::fetch_by_id(lens_id, &mut *app_db)
|
|
|
|
|
.await?
|
|
|
|
|
.ok_or(not_found!("lens not found"))?;
|
|
|
|
|
Selection::insertable_builder()
|
|
|
|
|
.lens_id(lens.id)
|
|
|
|
|
.attr_filters(vec![AttrFilter::NameEq(form.column)])
|
|
|
|
|
.build()?
|
|
|
|
|
.insert(&mut *app_db)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(Redirect::to(&format!(
|
|
|
|
|
"{0}/d/{base_id}/r/{class_oid}/l/{lens_id}/",
|
|
|
|
|
settings.root_path
|
|
|
|
|
))
|
|
|
|
|
.into_response())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn update_lens_page_post(
|
|
|
|
|
State(settings): State<Settings>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
|
Path(LensPagePath {
|
|
|
|
|
base_id,
|
|
|
|
|
class_oid,
|
|
|
|
|
lens_id,
|
|
|
|
|
}): Path<LensPagePath>,
|
|
|
|
|
Form(form): Form<HashMap<String, String>>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
dbg!(&form);
|
|
|
|
|
// FIXME auth
|
|
|
|
|
// FIXME csrf
|
|
|
|
|
|
|
|
|
|
Ok(Redirect::to(&format!(
|
|
|
|
|
"{0}/d/{base_id}/r/{class_oid}/l/{lens_id}/",
|
|
|
|
|
settings.root_path
|
|
|
|
|
))
|
|
|
|
|
.into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 16:54:51 -07:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct UpdateValuePageForm {
|
|
|
|
|
column: String,
|
|
|
|
|
pkeys: HashMap<String, Encodable>,
|
|
|
|
|
value: Encodable,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn update_value_page_post(
|
|
|
|
|
State(settings): State<Settings>,
|
|
|
|
|
State(mut base_pooler): State<BasePooler>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
|
Path(LensPagePath {
|
|
|
|
|
base_id,
|
|
|
|
|
class_oid,
|
|
|
|
|
lens_id: _,
|
|
|
|
|
}): Path<LensPagePath>,
|
|
|
|
|
Json(body): Json<UpdateValuePageForm>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
// FIXME auth
|
|
|
|
|
// FIXME csrf
|
|
|
|
|
|
|
|
|
|
let base = Base::fetch_by_id(base_id, &mut *app_db)
|
|
|
|
|
.await?
|
|
|
|
|
.ok_or(not_found!("no base found with that id"))?;
|
|
|
|
|
|
|
|
|
|
let mut client = base_pooler.acquire_for(base_id).await?;
|
|
|
|
|
|
|
|
|
|
let class = PgClass::fetch_by_oid(Oid(class_oid), &mut *client)
|
|
|
|
|
.await?
|
|
|
|
|
.ok_or(not_found!("unable to load table"))?;
|
|
|
|
|
let namespace = class.fetch_namespace(&mut *client).await?;
|
|
|
|
|
|
|
|
|
|
let pkey_attrs = fetch_primary_keys_for_rel(Oid(class_oid), &mut *client).await?;
|
|
|
|
|
|
|
|
|
|
body.pkeys
|
|
|
|
|
.get(&pkey_attrs.first().unwrap().attname)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.bind_onto(body.value.bind_onto(query(&format!(
|
|
|
|
|
r#"update {0}.{1} set {2} = $1 where {3} = $2"#,
|
|
|
|
|
escape_identifier(&namespace.nspname),
|
|
|
|
|
escape_identifier(&class.relname),
|
|
|
|
|
escape_identifier(&body.column),
|
|
|
|
|
escape_identifier(&pkey_attrs.first().unwrap().attname),
|
|
|
|
|
))))
|
|
|
|
|
.execute(&mut *client)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(Json(json!({ "ok": true })).into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 14:37:03 -07:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct ViewerPagePath {
|
|
|
|
|
base_id: Uuid,
|
|
|
|
|
class_oid: u32,
|
|
|
|
|
lens_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn viewer_page(
|
|
|
|
|
State(settings): State<Settings>,
|
|
|
|
|
State(mut base_pooler): State<BasePooler>,
|
|
|
|
|
AppDbConn(mut app_db): AppDbConn,
|
|
|
|
|
CurrentUser(current_user): CurrentUser,
|
|
|
|
|
Path(params): Path<ViewerPagePath>,
|
|
|
|
|
) -> Result<Response, AppError> {
|
|
|
|
|
todo!("not yet implemented");
|
|
|
|
|
}
|