api.cfm — простой REST через ?action=
This commit is contained in:
@@ -14,6 +14,10 @@
|
|||||||
|
|
||||||
<cfset this.datasource = "baza"/><!--- default datasource name --->
|
<cfset this.datasource = "baza"/><!--- default datasource name --->
|
||||||
<cfset getDS(this.datasource,"baza")/><!--- datasource name, environment variable prefix without "_" --->
|
<cfset getDS(this.datasource,"baza")/><!--- datasource name, environment variable prefix without "_" --->
|
||||||
|
|
||||||
|
<!--- REST API --->
|
||||||
|
<cfset this.restEnabled = true/>
|
||||||
|
<cfset this.restSettings = {skipCFCWithError: true}/>
|
||||||
|
|
||||||
<!--- Define the page request properties. --->
|
<!--- Define the page request properties. --->
|
||||||
<cfsetting
|
<cfsetting
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
<cfsetting showdebugoutput="no" enablecfoutputonly="yes">
|
||||||
|
<cfheader name="Content-Type" value="application/json; charset=utf-8">
|
||||||
|
|
||||||
|
<cfset result = {ok: false, error: "unknown action"}>
|
||||||
|
<cfset action = url.keyExists("action") ? url.action : "">
|
||||||
|
<cftry>
|
||||||
|
<cfif action EQ "test">
|
||||||
|
<cfquery name="q" datasource="baza">SELECT 1 as ok</cfquery>
|
||||||
|
<cfset result = {ok: true, message: "PostgreSQL connected"}>
|
||||||
|
|
||||||
|
<cfelseif action EQ "tables">
|
||||||
|
<cfquery name="q" datasource="baza">
|
||||||
|
SELECT table_name FROM information_schema.tables
|
||||||
|
WHERE table_schema = 'public' ORDER BY table_name
|
||||||
|
</cfquery>
|
||||||
|
<cfset var tables = []>
|
||||||
|
<cfloop query="q">
|
||||||
|
<cfset arrayAppend(tables, q.table_name)>
|
||||||
|
</cfloop>
|
||||||
|
<cfset result = {ok: true, tables: tables}>
|
||||||
|
|
||||||
|
<cfelseif action EQ "schema">
|
||||||
|
<cfset var ddl = [
|
||||||
|
"CREATE TABLE IF NOT EXISTS documents (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), filename TEXT NOT NULL, mime_type TEXT NOT NULL, original_bytes BYTEA, parsed_text TEXT, elements_json JSONB, status TEXT DEFAULT 'uploaded', error_message TEXT, created_at TIMESTAMPTZ DEFAULT now())",
|
||||||
|
"CREATE TABLE IF NOT EXISTS contracts (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), number TEXT NOT NULL, client TEXT, date_signed DATE, status TEXT DEFAULT 'active', created_at TIMESTAMPTZ DEFAULT now())",
|
||||||
|
"CREATE TABLE IF NOT EXISTS supplements (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), contract_id UUID REFERENCES contracts(id), number TEXT, date_signed DATE, type TEXT DEFAULT 'initial', document_id UUID REFERENCES documents(id), created_at TIMESTAMPTZ DEFAULT now())",
|
||||||
|
"CREATE TABLE IF NOT EXISTS spec_rows (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), supplement_id UUID REFERENCES supplements(id), row_num INTEGER, name TEXT, price NUMERIC, qty NUMERIC, sum NUMERIC, date_start DATE, date_end DATE, created_at TIMESTAMPTZ DEFAULT now())",
|
||||||
|
"CREATE TABLE IF NOT EXISTS spec_history (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), contract_id UUID REFERENCES contracts(id), supplement_id UUID REFERENCES supplements(id), row_num INTEGER, change_type TEXT NOT NULL, old_values JSONB, new_values JSONB, created_at TIMESTAMPTZ DEFAULT now())"
|
||||||
|
]>
|
||||||
|
<cfset var results = []>
|
||||||
|
<cfloop array="#ddl#" index="sql">
|
||||||
|
<cftry>
|
||||||
|
<cfquery datasource="baza">#preserveSingleQuotes(sql)#</cfquery>
|
||||||
|
<cfset arrayAppend(results, "OK")>
|
||||||
|
<cfcatch><cfset arrayAppend(results, cfcatch.message)></cfcatch>
|
||||||
|
</cftry>
|
||||||
|
</cfloop>
|
||||||
|
<cfset result = {ok: true, results: results}>
|
||||||
|
|
||||||
|
<cfelseif action EQ "query">
|
||||||
|
<cfif NOT url.keyExists("sql")>
|
||||||
|
<cfset result = {ok: false, error: "sql parameter required"}>
|
||||||
|
<cfelse>
|
||||||
|
<cfquery name="q" datasource="baza">#preserveSingleQuotes(url.sql)#</cfquery>
|
||||||
|
<cfset var cols = q.getColumnNames()>
|
||||||
|
<cfset var rows = []>
|
||||||
|
<cfloop query="q">
|
||||||
|
<cfset var row = {}>
|
||||||
|
<cfloop array="#cols#" index="c">
|
||||||
|
<cfset row[c] = q[c][q.currentRow]>
|
||||||
|
</cfloop>
|
||||||
|
<cfset arrayAppend(rows, row)>
|
||||||
|
</cfloop>
|
||||||
|
<cfset result = {ok: true, columns: cols, rows: rows, count: q.recordCount}>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cfelseif action EQ "execute" AND cgi.request_method EQ "POST">
|
||||||
|
<cfset var body = deserializeJSON(toString(getHttpRequestData().content))>
|
||||||
|
<cfif NOT structKeyExists(body, "sql")>
|
||||||
|
<cfset result = {ok: false, error: "sql required"}>
|
||||||
|
<cfelse>
|
||||||
|
<cfquery name="q" datasource="baza">#preserveSingleQuotes(body.sql)#</cfquery>
|
||||||
|
<cfset result = {ok: true}>
|
||||||
|
</cfif>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cfcatch><cfset result = {ok: false, error: cfcatch.message}></cfcatch>
|
||||||
|
</cftry>
|
||||||
|
|
||||||
|
<cfoutput>#serializeJSON(result)#</cfoutput>
|
||||||
Reference in New Issue
Block a user