Files
svc-api-x/v1/resources/bookmark_ls.cfc
T

179 lines
7.0 KiB
Plaintext

<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="string"},
is_enabled={prefix="b", type="boolean"},
sort={prefix="b", type="integer"}
}
/>
<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=""/>
<cfset var local={}/>
<cftry>
<cftry>
<cfset this.helper.validateField(arguments, "pageSize", "integer")/>
<cfset this.helper.validateField(arguments, "page", "integer")/>
<cfset var filter=this.helper.parseFilterParams(this.fieldsSpec)/>
<cfcatch type="invalidParamValue">
<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/>
<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>b.sort</m:field>
<m:field formatter=#request.castToBool#>b.is_enabled</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
where 1=1 <m:filter_build filter=#filter#/>
AND (b.contragent_id = <cfqueryparam cfsqltype="cf_sql_integer" value=#arguments.contragentId# null=#!isNumeric(arguments.contragentId)#/>
OR b.contragent_id=-1)
order by b.contragent_id desc, b.sort asc
limit #maxrows#
</cfquery>
<cfquery name="local.qTotal">
select count(*) as cnt
from bookmark b
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/>
<cfreturn representationOf(out)/>
<cfcatch type="any">
<cfreturn representationOf(cfcatch)/>
</cfcatch>
</cftry>
</cffunction>
<cffunction name="post" hint="Создание нового букмарка.">
<cfargument name="bookmark" type="string" required=true hint="type:string description: заголовок"/>
<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"/>
<cfargument name="iconUrl" type="string" required=false default="" hint="type:string description: url иконки"/>
<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: вкл"/>
<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.url)#"/>
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.iconUrl)#"/>
,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cleanInput(arguments.descr)#"/>
,<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="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");
}
function htm2plain(s) {
return replaceNoCase(s, '<br/>', '#chr(13)##chr(10)#', "ALL");
}
function cleanHtm(s) {
return replaceList(s, '<,>,"', '&lt;,&gt;,&quot;');
}
</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>