096 bookmarks

This commit is contained in:
msyu
2025-04-22 14:29:25 +03:00
parent 6bedde8e15
commit 9f46c9cd67
3 changed files with 274 additions and 1 deletions
+1 -1
View File
@@ -51,7 +51,7 @@
//variables.framework.docs={}; //variables.framework.docs={};
variables.framework.docs.APIName="Deck API"; variables.framework.docs.APIName="Deck API";
variables.framework.docs.APIVersion="0.095"; variables.framework.docs.APIVersion="0.096";
variables.framework.globalHeaders = structNew(); variables.framework.globalHeaders = structNew();
variables.framework.globalHeaders["Access-Control-Expose-Headers"] = "Location"; variables.framework.globalHeaders["Access-Control-Expose-Headers"] = "Location";
+72
View File
@@ -0,0 +1,72 @@
<cfcomponent extends="taffy.core.resource" taffy:uri="/bookmarks/bookmarks/{bookmarkUid}">
<cfsilent>
<cfimport prefix="m" taglib="../lib"/>
<cfset this.helper=CreateObject("component","lib.rest_api_helper")/><!---*** странно, почему мы его видим?---><!---вынести в апп?--->
</cfsilent>
<cffunction name="patch">
<cfargument name="bookmarkUid" type="string" required=true hint="type:guid"/>
<cfargument name="bookmark" type="string" required=false hint="type:string"/>
<cfargument name="descr" type="string" required=false hint="type:string (no cleanup here!)"/>
<cfargument name="url" type="string" required=false hint="type:string description: url"/><!--- чистить против XSS --->
<cfargument name="iconUrl" type="string" required=false hint="type:string description: url иконки"/><!--- чистить против XSS --->
<cfargument name="sort" type="string" required=false hint="type:integer description: целое число для сортировки"/>
<cfargument name="isEnabled" type="string" required=false hint="type:boolean description: вкл"/>
<!--- arguments.contragentId injected implicitly --->
<!--- arguments.usrId injected implicitly --->
<!--- ****** проверка уникальности --->
<cftry>
<cfset this.helper.keyExistsAndValid(arguments, "bookmarkUid", "guid")/>
<cfquery name="local.qSave">
update bookmark set
dt_updated=<cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#"/>
,updater_id=<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.usrId#"/>
<cfif structKeyExists(arguments,"bookmark")>
,bookmark=<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.bookmark)#" />
</cfif>
<cfif structKeyExists(arguments,"descr")>
,descr=<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.descr)#" />
</cfif>
<cfif structKeyExists(arguments,"url")>
,url=<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.url)#" />
</cfif>
<cfif structKeyExists(arguments,"iconUrl")>
,icon_url=<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.iconUrl)#" />
</cfif>
<cfif structKeyExists(arguments,"sort")>
,sort=<cfqueryparam cfsqltype="cf_sql_integer" value="#cleanInput(arguments.sort)#" />
</cfif>
<cfif structKeyExists(arguments,"isEnabled")>
,is_enabled=<cfqueryparam cfsqltype="cf_sql_bit" value="#cleanInput(arguments.isEnabled)#" />
</cfif>
where
contragent_id=<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.contragentId#"/><!--- isolation --->
AND bookmark_uid=<cfqueryparam cfsqltype="cf_sql_other" value="#arguments.bookmarkUid#"/>;
--select @@rowcount as cnt;
</cfquery>
<!--- <cfif local.qSave.cnt EQ 0>
<cfreturn noData().withStatus(404, "Record Not Found") />
</cfif> --->
<cfcatch type="invalidParamValue">
<cfreturn representationOf(this.helper.formatBadRequestError(cfcatch)).withStatus(400)/>
</cfcatch>
</cftry>
<cfreturn noData().withStatus(204, "No Content") />
</cffunction>
<cffunction name="cleanInput">
<cfargument name="s" type="string"/>
<cfreturn htmlEditFormat(s)/>
</cffunction>
</cfcomponent>
+201
View File
@@ -0,0 +1,201 @@
<cfcomponent extends="taffy.core.resource" taffy:uri="/bookmarks/bookmarks">
<cfsilent>
<cfimport prefix="m" taglib="../lib"/>
<cfset this.helper=CreateObject("component","lib.rest_api_helper")/><!---*** странно, почему мы его видим?---><!---вынести в апп?--->
</cfsilent>
<!---спецификация полей, пригодных для фильтрации (?и сортировки)--->
<cfset this.fieldsSpec={
bookmark_uid={prefix="b", type="guid"},
dt_created={prefix="b", type="date"},
dt_updated={prefix="b", type="date"},
contragent_id={prefix="b", type="integer"},
bookmark={prefix="b", type="string"},
url={prefix="b", type="string"},
icon_url={prefix="b", type="string"},
descr={prefix="b", type="integer"},
is_enabled={prefix="b", type="boolean"},
sort={prefix="b", type="integer"}
}
/>
<!--- предполагается показывать букмарки только "настоящим" клиентам, поэтому используем contragent_id как признак своих--->
<cffunction name="get" hint="Список букмарков">
<cfargument name="pageSize" type="string" hint="type:integer" default="100"/>
<cfargument name="page" type="string" hint="type:integer" default="1"/>
<cfargument name="orderBy" type="string" hint="type:string, description:comma-separated list of fields to sort by, example:fld1.ASC,fld2.DESC,fld3.ASC" default=""/>
<cftry> <!--- отладка на время проблемы 404 --->
<cfset local={}/>
<!---parse and validate request parameters--->
<cftry>
<cfset this.helper.validateField(arguments, "pageSize", "integer")/>
<cfset this.helper.validateField(arguments, "page", "integer")/>
<!---мы мирно игнорируем поля, отсутствующие в спецификации, что позволяет не делать исключения для orderBy и т.п.--->
<cfset var filter=this.helper.parseFilterParams(this.fieldsSpec)/>
<!--- <cfset var order=this.helper.parseOrderBy(this.fieldsSpec, arguments.orderBy)/> --->
<cfcatch type="invalidParamValue"><cfreturn representationOf("Проверка связи 3")>
<cfreturn representationOf(this.helper.formatBadRequestError(cfcatch)).withStatus(400)/>
</cfcatch>
<cfcatch type="any">
<cfreturn representationOf(cfcatch)/>
</cfcatch>
</cftry>
<cfset var out={}/>
<cfset var maxrows=arguments.pageSize*arguments.page/>
<cfset var startrow=arguments.pageSize*(arguments.page-1)+1/>
<cfset request.locateIamService()/>
<cfquery name="local.qRead" result="local.result">
select
<m:field_set titleMapOut="local.titleMap" lengthOut="local.fieldCount">
<m:field>b.bookmark_uid::text as bookmark_uid</m:field>
<m:field>b.bookmark</m:field>
<m:field>b.descr</m:field>
<m:field>b.url</m:field>
<m:field>b.icon_url</m:field>
<m:field>b.contragent_id</m:field>
<m:field formatter=#request.castToBool#>b.is_enabled</m:field>
<!--- <m:field>k.external_uid</m:field> --->
<m:field>to_char(b.dt_created, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') as dt_created</m:field>
<m:field>to_char(b.dt_updated, 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZHTZM') as dt_updated</m:field>
</m:field_set>
from bookmark b
/*left outer join contragent k on (b.contragent_id=k.contragent_id)*/
where 1=1 <m:filter_build filter=#filter#/>
AND (b.contragent_id = <cfqueryparam cfsqltype="cf_sql_integer" value=#arguments.contragentId# null=#!isNumeric(arguments.contragentId)#/>)
order by sort desc
<!--- <m:order_build sortCollection=#this.helper.parseNumericOrder(local.titleMap, arguments.orderBy)# fieldCount=0/>--->
limit #maxrows#
</cfquery>
<!--- <cfdump var=#local.qRead#/><cfabort/> --->
<cfquery name="local.qTotal">
select count(*) as cnt
from bookmark b
/*left outer join contragent k on (b.contragent_id=k.contragent_id)*/
where 1=1
AND (b.contragent_id = <cfqueryparam cfsqltype="cf_sql_integer" value=#arguments.contragentId# null=#!isNumeric(arguments.contragentId)#/>)
</cfquery>
<cfset "out.queryDurationMs"=getTickCount() - request.startTickCount/>
<cfset "out.total"=#local.qTotal.cnt#/>
<cfset "out.resultSetSize"=#local.qRead.recordCount#/>
<cfset "out.pageSize"=#arguments.pageSize*1#/>
<cfset "out.page"=#arguments.page*1#/>
<cfset "out.orderBy"=#arguments.orderBy#/>
<cfset var resultCollection=[]/>
<cfloop query=#local.qRead# startRow=#startrow# endRow=#(startrow+maxrows-1)#>
<cfset var rec={}/>
<cfset this.helper.appendRecord(rec, "", local.titleMap, local.qRead, this.helper.snake2camel)/>
<cfset arrayAppend(resultCollection, rec)/>
</cfloop>
<cfset "out.size"=#arrayLen(resultCollection)#/>
<cfset "out.results"=#resultCollection#/>
<cfset "out.runDurationMs"=getTickCount()-request.startTickCount/>
<!---<cfset "out.sql"=#local.result.sql#/>--->
<cfreturn representationOf(out)/>
<cfcatch type="any">
<cfreturn representationOf(cfcatch)/>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="post" hint="Создание нового букмарка.">
<!--- <cfargument name="bookmarkUid" type="string" required=true hint="type:guid"/> --->
<!--- <cfargument name="svcOperationCfsParamId" type="string" required=true hint="type:integer"/> --->
<cfargument name="bookmark" type="string" required=true hint="type:string description: заголовок"/><!--- чистить против XSS --->
<cfargument name="descr" type="string" required=false default="" hint="type:boolean description: пользовательское примечание"/>
<cfargument name="url" type="string" required=false default="" hint="type:string description: url"/><!--- чистить против XSS --->
<cfargument name="iconUrl" type="string" required=false default="" hint="type:string description: url иконки"/><!--- чистить против XSS --->
<cfargument name="sort" type="string" required=false default="100" hint="type:integer description: целое число для сортировки"/>
<cfargument name="isEnabled" type="string" required=false default="1" hint="type:boolean description: вкл"/>
<!--- arguments.contragentId injected implicitly --->
<!--- arguments.usrId injected implicitly --->
<cftry>
<cfset this.helper.validateField(arguments, "bookmarkUid", "guid")/>
<cfset this.helper.validateField(arguments, "sort", "integer")/>
<cfset this.helper.validateField(arguments, "isEnabled", "boolean")/>
<cfcatch type="invalidParamValue">
<cfreturn representationOf(this.helper.formatBadRequestError(cfcatch)).withStatus(400)/>
</cfcatch>
</cftry>
<cfset local={}/>
<cfset local.bookmarkUid=#createGUID()#/>
<cftry>
<cfquery name="local.qSave">
insert into bookmark (
bookmark_uid,contragent_id, bookmark,url,icon_url,descr,sort,is_enabled,dt_created,creator_id
) values (
<cfqueryparam cfsqltype="cf_sql_other" value="#local.bookmarkUid#"/>
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.contragentId#"/>
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.bookmark)#"/>
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.descr)#"/>
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.url)#"/>
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.iconUrl)#"/>
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.sort#"/>
,<cfqueryparam cfsqltype="cf_sql_bit" value="#arguments.isEnabled#" null=#!isValid("boolean",arguments.isEnabled)#/>
,<cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#" />
,<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.usrId#" />
);
</cfquery>
<!--- <cfcatch type="invalidParamValue">
<cfreturn representationOf(this.helper.formatBadRequestError(cfcatch)).withStatus(400)/>
</cfcatch> --->
<cfcatch type="any">
<cfreturn representationOf(this.helper.formatException(cfcatch, "Internal Error")).withStatus(500)/>
</cfcatch>
</cftry>
<cfreturn noData()
.withHeaders({location: "./#local.bookmarkUid#"})
.withStatus(201, "Created")
/>
</cffunction>
<cfscript>
function plain2htm(s) {
return replace(replace(s, chr(13),'',"ALL"),chr(10),'<br/>', "ALL");
}
//request.plain2htm = plain2htm;
function htm2plain(s) {
return replaceNoCase(s, '<br/>', '#chr(13)##chr(10)#', "ALL");
}
//request.htm2plain = htm2plain;
function cleanHtm(s) {
return replaceList(s, '<,>,"', '&lt;,&gt;,&quot;');
}
//request.cleanHtm = cleanHtm;
</cfscript>
<cffunction name="plain2HtmClean">
<cfargument name="s" type="string"/>
<cfreturn plain2htm(cleanHtm(s))/>
</cffunction>
<cffunction name="cleanInput">
<cfargument name="s" type="string"/>
<cfreturn htmlEditFormat(s)/>
</cffunction>
</cfcomponent>