We left off after writing the test for the
insertPost()
method. Now, we're going to make
that test pass by writing the code for it. First you'll need to create PostEntity.cfc in the xorblog/cfcs/src
directory, and make sure to surround it in the proper
<cfcomponent>
tags.
What follows is that code:
<cffunction name="insertPost"
output="false"
returntype="numeric"
access="public"
>
<cfargument name="name"
required="true"
type="string"
>
<cfargument name="meat"
required="true"
type="string"
>
<cfargument name="originalDate"
required="true"
type="date"
>
<cfargument name="author"
required="true"
type="string"
>
<cfset var local = structNew()>
<cftransaction>
<cfquery name="local.ins"
datasource="#variables._datasource#"
>
insert into post
(name, meat, originalDate, lastModifiedDate, author)
values
(<cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.name#"
>
,
<cfqueryparam cfsqltype="cf_sql_longvarchar"
value="#arguments.meat#"
>
,
<cfqueryparam cfsqltype="cf_sql_timestamp"
value="#arguments.originalDate#"
>
,
<cfqueryparam cfsqltype="cf_sql_timestamp"
value="#arguments.originalDate#"
>
,
<cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.author#"
>
,
</cfquery>
<cfquery name="local.result"
datasource="#_datasource#"
>
select max(id) as newID from post
where originalDate=<cfqueryparam cfsqltype="cf_sql_timestamp"
value="#arguments.originalDate#"
>
and name=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.name#"
>
</cfquery>
</cftransaction>
<cfif local.result.recordcount is 0
>
<cfthrow message="The new post was not properly inserted."
>
</cfif>
<cfreturn local.result.newID>
</cffunction>
There isn't really anything special here, unless you are new to Coldfusion. If that's the case, you'll
want to take note of the
<cfqueryparam>
tag - using it is considered a "best practice" by
most (if not all) experienced Coldfusion developers.
The other item of note is that if you were to run this code by itself,
it still wouldn't work, since we haven't defined
variables._datasource
. Many developers
would do this in a function called
init()
that they call each time they create an object. I've
done it as well.
I suppose if you were rigorously following the
YAGNI principle, you might wait until creating the next
method that would use that variable before defining it. I certainly like YAGNI, but my
OCD is not so bad that I won't occasionally allow
my
ESP to tell me that I'm going to use something,
even if I don't yet need it. With that said, I try only do it in the most obvious of cases, such as this one.
Now that we've written the code for
insertPost()
, its time to run the test again. Doing so,
I see that I have two test that run green (this one, and our
test_hookup()
from earlier.
We've gone red-green, so now it's time to refactor. Unfortunately, I don't see any places to do that
yet, but I think they'll reveal themselves next time when we write
our second test and second method in
PostEntity
. (To be continued...)
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
There are no comments for this entry yet.
Leave a comment