use actix_web::HttpResponse;
use serde_json::json;
use sqlx::{Pool, Postgres};
use super::super::catalog_errors::schema_internal_fetch_error;
use super::super::catalog_service_loader_query::load_schema_constraint_rows;
use super::super::presentation::schema_constraints as build_schema_constraints_payload;
use super::super::request_params::NormalizedConstraintQuery;
use super::super::schema_backend::SchemaBackend;
pub(in super::super) async fn schema_constraints_response(
backend: &SchemaBackend,
app_state: &crate::AppState,
query: NormalizedConstraintQuery,
) -> HttpResponse {
let rows = match backend {
SchemaBackend::Postgres { pool } => match load_schema_constraint_rows(pool, &query).await {
Ok(rows) => rows,
Err(err) => return schema_internal_fetch_error("constraints", &err),
},
SchemaBackend::D1 { connection_info } => {
match super::super::schema_d1::load_schema_constraint_rows(
app_state,
connection_info,
query.table_name.as_str(),
)
.await
{
Ok(rows) => rows,
Err(resp) => return resp,
}
}
};
HttpResponse::Ok().json(json!({ "constraints": build_schema_constraints_payload(rows) }))
}