Files
svc-api-x/v1/resources/bookmark.cfc
T
2025-05-16 17:15:05 +03:00

118 lines
5.1 KiB
Plaintext

<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="get" hint="Букмарк">
<cfargument name="bookmarkUid" type="string" required=true hint="type:guid"/>
<cfset var local={}/>
<cftry>
<cftry>
<cfset this.helper.validateField(arguments, "bookmarkUid", "guid")/>
<cfcatch type="invalidParamValue">
<cfreturn representationOf(this.helper.formatBadRequestError(cfcatch)).withStatus(400)/>
</cfcatch>
</cftry>
<cfset var out={}/>
<!--- <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>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 b.bookmark_uid=<cfqueryparam cfsqltype="cf_sql_other" value="#arguments.bookmarkUid#" null=#!isValid('guid',arguments.bookmarkUid)#/>
AND (b.contragent_id = <cfqueryparam cfsqltype="cf_sql_integer" value=#arguments.contragentId# null=#!isNumeric(arguments.contragentId)#/> OR b.contragent_id=-1)
</cfquery>
<!--- <cfdump var=#local.qRead#/><cfabort/> --->
<cfset "out.queryDurationMs"=getTickCount() - request.startTickCount/>
<cfset "out.bookmark" = this.helper.appendRecord(structNew("linked"), "", local.titleMap, local.qRead, this.helper.snake2camel)/>
<cfset "out.runDurationMs"=getTickCount()-request.startTickCount/>
<cfreturn representationOf(out)/>
<cfcatch type="any">
<cfreturn representationOf(cfcatch)/>
</cfcatch>
</cftry>
</cffunction>
<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="#arguments.url#"/>
</cfif>
<cfif structKeyExists(arguments,"iconUrl")>
,icon_url=<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.iconUrl#"/>
</cfif>
<cfif structKeyExists(arguments,"sort")>
,sort=<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.sort#"/>
</cfif>
<cfif structKeyExists(arguments,"isEnabled")>
,is_enabled=<cfqueryparam cfsqltype="cf_sql_bit" value="#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>