Update your the footer of _rant.cfm
// views/_partials/_rant.cfm
<div class="card-footer">
<cfif auth().guest()>
<button disabled class="btn btn-outline-dark">
#args.rant.getBumps().len()# 👊
</button>
<cfelseif auth().user().hasBumped( args.rant )>
<form method="POST" action="#event.buildLink( "rants.#args.rant.getId()#.bumps" )#" style="display: inline;">
<input type="hidden" name="_method" value="DELETE" />
<button class="btn btn-dark">
#args.rant.getBumps().len()# 👊
</button>
</form>
<cfelse>
<form method="POST" action="#event.buildLink( "rants.#args.rant.getId()#.bumps" )#" style="display: inline;">
<button class="btn btn-outline-dark">
#args.rant.getBumps().len()# 👊
</button>
</form>
</cfif>
<cfif auth().guest()>
<button disabled class="btn btn-outline-dark">
#args.rant.getPoops().len()# 💩
</button>
<cfelseif auth().user().hasPooped( args.rant )>
<form method="POST" action="#event.buildLink( "rants.#args.rant.getId()#.poops" )#" style="display: inline;">
<input type="hidden" name="_method" value="DELETE" />
<button class="btn btn-dark">
#args.rant.getPoops().len()# 💩
</button>
</form>
<cfelse>
<form method="POST" action="#event.buildLink( "rants.#args.rant.getId()#.poops" )#" style="display: inline;">
<button class="btn btn-outline-dark">
#args.rant.getPoops().len()# 💩
</button>
</form>
</cfif>
</div>
route( "rants/:id/bumps" )
.withAction( { "POST" = "create", "DELETE" = "delete" } )
.toHandler( "Bumps" );
route( "rants/:id/poops" )
.withAction( { "POST" = "create", "DELETE" = "delete" } )
.toHandler( "Poops" );
coldbox create handler name="bumps" actions="create,delete"
// handlers/bumps.cfc
component {
property name="reactionService" inject="id";
function create( event, rc, prc ) {
reactionService.bump( rc.id, auth().getUserId() );
relocate( "rants" );
}
function delete( event, rc, prc ) {
reactionService.unbump( rc.id, auth().getUserId() );
relocate( "rants" );
}
}
coldbox create handler name="poops" actions="create,delete"
// handlers/poops.cfc
component {
property name="reactionService" inject="id";
function create( event, rc, prc ) {
reactionService.poop( rc.id, auth().getUserId() );
relocate( "rants" );
}
function delete( event, rc, prc ) {
reactionService.unpoop( rc.id, auth().getUserId() );
relocate( "rants" );
}
}
// models/Bump.cfc
component accessors="true" {
property name="userId";
property name="rantId";
}
// models/Poop.cfc
component accessors="true" {
property name="userId";
property name="rantId";
}
inject the reactionService
and add the following functions
// models/User.cfc
property name="reactionService" inject="id";
function hasBumped( rant ) {
if ( isNull( variables.bumps ) ) {
variables.bumps = reactionService.getBumpsForUser( this );
}
return ! variables.bumps.filter( function( bump ) {
return bump.getRantId() == rant.getId();
} ).isEmpty();
}
function hasPooped( rant ) {
if ( isNull( variables.poops ) ) {
variables.poops = reactionService.getPoopsForUser( this );
}
return ! variables.poops.filter( function( poop ) {
return poop.getRantId() == rant.getId();
} ).isEmpty();
}
add the following functions:
// models/services/ReactionService.cfc
function getBumpsForUser( user ) {
return queryExecute(
"SELECT * FROM `bumps` WHERE `userId` = ?",
[ user.getId() ],
{ returntype = "array" }
).map( function( bump ) {
return populator.populateFromStruct(
newBump(),
bump
)
} );
}
function getPoopsForUser( user ) {
return queryExecute(
"SELECT * FROM `poops` WHERE `userId` = ?",
[ user.getId() ],
{ returntype = "array" }
).map( function( poop ) {
return populator.populateFromStruct(
newPoop(),
poop
)
} );
}
// models/services/ReactionService.cfc
function bump( rantId, userId ) {
queryExecute(
"INSERT INTO `bumps` VALUES (?, ?)",
[ userId, rantId ]
);
}
function unbump( rantId, userId ) {
queryExecute(
"DELETE FROM `bumps` WHERE `userId` = ? AND `rantId` = ?",
[ userId, rantId ]
);
}
function poop( rantId, userId ) {
queryExecute(
"INSERT INTO `poops` VALUES (?, ?)",
[ userId, rantId ]
);
}
function unpoop( rantId, userId ) {
queryExecute(
"DELETE FROM `poops` WHERE `userId` = ? AND `rantId` = ?",
[ userId, rantId ]
);
}
Reinialize the Framework and Test the Site