<cfcomponent
extends="BaseAPI"
output="false"
hint="I am the public API for contacts.">
<cffunction
name="AddContact"
access="remote"
returntype="struct"
returnformat="json"
output="false"
hint="I add the given contact.">
<cfargument
name="Name"
type="string"
required="true"
hint="I am the name of the contact."
/>
<cfargument
name="Hair"
type="string"
required="true"
hint="I am the hair color of the contact."
/>
<cfset var LOCAL = {} />
<cfset LOCAL.Response = THIS.GetNewResponse() />
<cfif NOT Len( ARGUMENTS.Name )>
<cfset ArrayAppend(
LOCAL.Response.Errors,
"Please enter a contact name."
) />
</cfif>
<cfif NOT Len( ARGUMENTS.Hair )>
<cfset ArrayAppend(
LOCAL.Response.Errors,
"Please enter a contact hair color."
) />
</cfif>
<cfif NOT ArrayLen( LOCAL.Response.Errors )>
<cfset LOCAL.Contact = {
ID = CreateUUID(),
Name = ARGUMENTS.Name,
Hair = ARGUMENTS.Hair
} />
<cfset ArrayAppend(
APPLICATION.Contacts,
LOCAL.Contact
) />
<cfset LOCAL.Response.Data = LOCAL.Contact />
</cfif>
<cfif ArrayLen( LOCAL.Response.Errors )>
<cfset LOCAL.Response.Success = false />
</cfif>
<cfreturn LOCAL.Response />
</cffunction>
<cffunction
name="DeleteContact"
access="remote"
returntype="struct"
returnformat="json"
output="false"
hint="I delete the contact with the given ID.">
<cfargument
name="ID"
type="string"
required="true"
hint="I am the ID of the contact to delete."
/>
<cfset var LOCAL = {} />
<cfset LOCAL.Response = THIS.GetNewResponse() />
<cfset LOCAL.ContactIndex = 0 />
<cfloop
index="LOCAL.Index"
from="1"
to="#ArrayLen( APPLICATION.Contacts )#"
step="1">
<cfif (APPLICATION.Contacts[ LOCAL.Index ].ID EQ ARGUMENTS.ID)>
<cfset LOCAL.ContactIndex = LOCAL.Index />
</cfif>
</cfloop>
<cfif NOT LOCAL.ContactIndex>
<cfset ArrayAppend(
LOCAL.Response.Errors,
"The given contact could not be found."
) />
</cfif>
<cfif NOT ArrayLen( LOCAL.Response.Errors )>
<cfset LOCAL.Contact = APPLICATION.Contacts[ LOCAL.ContactIndex ] />
<cfset ArrayDeleteAt(
APPLICATION.Contacts,
LOCAL.ContactIndex
) />
<cfset LOCAL.Response.Data = LOCAL.Contact />
</cfif>
<cfif ArrayLen( LOCAL.Response.Errors )>
<cfset LOCAL.Response.Success = false />
</cfif>
<cfreturn LOCAL.Response />
</cffunction>
<cffunction
name="GetContacts"
access="remote"
returntype="struct"
returnformat="json"
output="false"
hint="I return the collection of contacts.">
<cfset var LOCAL = {} />
<cfset LOCAL.Response = THIS.GetNewResponse() />
<cfset LOCAL.Response.Data = APPLICATION.Contacts />
<cfreturn LOCAL.Response />
</cffunction>
</cfcomponent>