Created
June 29, 2012 15:20
-
-
Save cfvonner/3018575 to your computer and use it in GitHub Desktop.
Sample Components for CFKoans Mocking tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component displayname="Person" hint="I am a person object." output="false" accessors="true" | |
{ | |
// Define the properties for this component | |
property name="personID" type="numeric" getter="true" setter="false" hint="Unique numeric id assigned by the database."; | |
property name="firstName" type="string" getter="true" setter="true" hint="The first name of the person."; | |
property name="lastName" type="string" getter="true" setter="true" hint="The last name of the person."; | |
property name="dateOfBirth" type="date" getter="true" setter="true" hint="The date of birth of the person."; | |
property name="personDAO" type="Person" getter="false" setter="true"; | |
// Define the functions other than implicit getters and setters for this component | |
public any function init( | |
required string firstName='', | |
required string lastName='', | |
required date dateOfBirth=0, | |
numeric personID = 0 | |
) output="false" hint="I am the constructor method." { | |
// Store the properties | |
variables.personID = arguments.personID; | |
this.setFirstName( arguments.firstName ); | |
this.setLastName( arguments.lastName ); | |
this.setDateOfBirth( arguments.dateOfBirth ); | |
// Return this object reference | |
return( this ); | |
} | |
public string function getFullName() output="false" hint="I return the concatenation of firstName and lastName properties." { | |
return variables.firstName & ' ' & variables.lastName; | |
} | |
public numeric function getAge() output="false" hint="I return the age of the Person in years." { | |
var age = DateDiff("yyyy",getDateOfBirth(),now()); | |
return age; | |
} | |
public struct function save() output="false" { | |
var result = variables.personDAO.savePerson( this ); | |
if ( result.success ) { | |
if ( result.action IS "update" ) { | |
return( this ); | |
} else { | |
variables.personID = result.id; | |
return( this ); | |
} | |
} else { | |
return null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent displayname="PersonDAO" hint="I am the person DAO class." output="false"> | |
<cfproperty name="datasource" type="string" displayname="datasource" hint="I am the name of the datasource to use for persistence."> | |
<cffunction name="init" | |
access="public" | |
output="false" | |
hint="I am the constructor method for the PersonDAO class."> | |
<cfargument name="datasource" required="true" type="string" hint="I am the name of the datasource to use in this class."> | |
<!--- Store the properties ---> | |
<cfset variables.datasource = arguments.datasource> | |
<!--- Return this object reference ---> | |
<cfreturn this> | |
</cffunction> | |
<!--- Public Methods ---> | |
<cffunction name="getPersonByID" | |
access="public" | |
output="false" | |
hint="I return a Person bean populated with the information for a specific person."> | |
<cfargument name="personID" | |
required="true" | |
type="numeric" | |
hint="I am the ID of the person you wish to retrieve."> | |
<cfset var qSelect = ''> | |
<cfset var objPerson = ''> | |
<cfquery name="qSelect" datasource="#variables.datasource#"> | |
SELECT Person_ID | |
,firstName | |
,lastName | |
,dateOfBirth | |
FROM Persons | |
WHERE Person_ID = <cfqueryparam value="#arguments.personID#" cfsqltype="cf_sql_integer"> | |
</cfquery> | |
<!--- If qSelect returns a record matching the personID, create a Person bean and return it ---> | |
<cfif qSelect.RecordCount> | |
<cfset objPerson = createObject("component","components.Person") | |
.init( | |
personID = qSelect.Person_ID, | |
firstName = qSelect.firstName, | |
lastName = qSelect.lastName, | |
dateOfBirth = qSelect.dateOfBirth | |
)> | |
</cfif> | |
<cfreturn objPerson> | |
</cffunction> | |
<cffunction name="deletePersonByID" | |
access="public" | |
output="false" | |
returntype="boolean" | |
hint="I delete a person from the database."> | |
<cfargument name="personID" | |
required="true" | |
type="numeric" | |
hint="I am the ID of the person | |
you wish to delete." > | |
<cfset var qDelete = ''> | |
<cfset var bSuccess = true> | |
<cftry> | |
<cfquery name="qDelete" datasource="#variables.datasource#"> | |
DELETE FROM Persons | |
WHERE | |
Person_ID = <cfqueryparam value="#arguments.personID#" | |
cfsqltype="cf_sql_integer"> | |
</cfquery> | |
<cfcatch type="database"> | |
<cfset bSuccess = false> | |
</cfcatch> | |
</cftry> | |
<cfreturn bSuccess> | |
</cffunction> | |
<cffunction name="exists" | |
access="public" | |
output="false" | |
returntype="boolean" | |
hint="I check to see if a specific Person exists within | |
the database, using the personID as a check."> | |
<cfargument name="person" | |
required="true" | |
type="components.Person" | |
hint="I am the Person bean."> | |
<cfset var qExists = ""> | |
<cfquery name="qExists" datasource="#variables.datasource#" maxrows="1"> | |
SELECT Person_ID | |
FROM | |
Persons | |
WHERE | |
Person_ID = <cfqueryparam value="#arguments.person.getPersonID()#" | |
cfsqltype="cf_sql_integer"> | |
</cfquery> | |
<cfif qExists.recordCount> | |
<cfreturn true> | |
<cfelse> | |
<cfreturn false> | |
</cfif> | |
</cffunction> | |
<cffunction name="savePerson" | |
access="public" | |
output="false" | |
returntype="struct" | |
hint="I handle saving a Person, either by creating | |
a new entry or updating an existing one."> | |
<cfargument name="person" | |
required="true" | |
type="components.Person" | |
hint="I am the Person bean."> | |
<cfset var status = StructNew()> | |
<cfset status.success = true> | |
<cfif exists(arguments.person)> | |
<cfset status = updatePerson(arguments.person)> | |
<cfset status.action = "update"> | |
<cfelse> | |
<cfset status = createPerson(arguments.person)> | |
<cfset status.action = "create"> | |
</cfif> | |
<cfreturn status> | |
</cffunction> | |
// Private Methods | |
<cffunction name="createPerson" | |
access="private" | |
output="false" | |
returntype="struct" | |
hint="I insert a new record into the persistence system (database)."> | |
<cfargument name="person" | |
required="true" | |
type="components.Person" | |
hint="I am the Person bean."> | |
<cfset var qInsert = ''> | |
<cfset var iResult = 0> | |
<cfset var status = StructNew()> | |
<cfset status.id = 0> | |
<cfset status.success = true> | |
<cftry> | |
<!--- Insert the property values from the person bean into the database ---> | |
<cfquery name="qInsert" datasource="#variables.datasource#" result="iResult"> | |
INSERT INTO Persons | |
( | |
firstName | |
,lastName | |
,dateofBirth | |
) | |
VALUES | |
( | |
<cfqueryparam value="#arguments.person.getFirstName()#" cfsqltype="cf_sql_varchar"> | |
,<cfqueryparam value="#arguments.person.getLastName()#" cfsqltype="cf_sql_varchar"> | |
,<cfqueryparam value="#arguments.person.getDateOfBirth()#" cfsqltype="cf_sql_date"> | |
) | |
</cfquery> | |
<cfcatch type="database"> | |
<cfset status.success = false> | |
</cfcatch> | |
</cftry> | |
<cfif status.success> | |
<cfset status.id = iResult.generatedKey> | |
</cfif> | |
<cfreturn status> | |
</cffunction> | |
<cffunction name="updatePerson" | |
access="private" | |
output="false" | |
returntype="struct" | |
hint="I update a Persons information."> | |
<cfargument name="person" | |
required="true" | |
type="components.Person" | |
hint="I am the Person bean."> | |
<cfset var qUpdate = ''> | |
<cfset var status = StructNew()> | |
<cfset status.success = true> | |
<cftry> | |
<!--- Update the database using the beans current property values ---> | |
<cfquery name="qUpdate" datasource="#variables.datasource#"> | |
UPDATE Persons | |
SET | |
firstName = <cfqueryparam value="#arguments.person.getFirstName()#" | |
cfsqltype="cf_sql_varchar"> | |
,lastName = <cfqueryparam value="#arguments.person.getLastName()#" | |
cfsqltype="cf_sql_varchar"> | |
,dateOfBirth = <cfqueryparam value="#arguments.person.getDateOfBirth()#" | |
cfsqltype="cf_sql_date"> | |
WHERE | |
Person_ID = <cfqueryparam value="#arguments.person.getPersonID()#" | |
cfsqltype="cf_sql_integer"> | |
</cfquery> | |
<cfcatch type="database"> | |
<cfset status.success = false > | |
</cfcatch> | |
</cftry> | |
<cfreturn status> | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment