From 76e2568f851122cdc7ab75df310a181b627d438c Mon Sep 17 00:00:00 2001 From: Jeff Brown Date: Wed, 20 Jun 2012 17:14:41 -0600 Subject: [PATCH] move logic from controller to service --- .../grails/twitter/StatusController.groovy | 29 ++----------------- .../org/grails/twitter/StatusService.groovy | 19 ++++++++++++ 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/grails-app/controllers/org/grails/twitter/StatusController.groovy b/grails-app/controllers/org/grails/twitter/StatusController.groovy index 1520600..eb259d3 100755 --- a/grails-app/controllers/org/grails/twitter/StatusController.groovy +++ b/grails-app/controllers/org/grails/twitter/StatusController.groovy @@ -7,18 +7,16 @@ import org.grails.twitter.auth.Person @Secured('IS_AUTHENTICATED_FULLY') class StatusController { - def springSecurityService def statusService - def twitterCache def index() { - def messages = currentUserTimeline() + def messages = statusService.currentUserTimeline() [statusMessages: messages] } def updateStatus(String message) { statusService.updateStatus message - def messages = currentUserTimeline() + def messages = statusService.currentUserTimeline() def content = twitter.renderMessages messages: messages render content @@ -28,27 +26,4 @@ class StatusController { statusService.follow id redirect action: 'index' } - - private lookupPerson() { - Person.get(springSecurityService.principal.id) - } - - private currentUserTimeline() { - def messages = twitterCache[springSecurityService.principal.username] - if (!messages) { - log.debug "No messages found in cache for user <${springSecurityService.principal.username}>. Querying database..." - def per = lookupPerson() - def query = Status.whereAny { - author { username == per.username } - if(per.followed) author in per.followed - }.order 'dateCreated', 'desc' - messages = query.list(max: 10) - - twitterCache[springSecurityService.principal.username] = messages - } - else { - log.debug "Status messages loaded from cache for user <${springSecurityService.principal.username}>." - } - messages - } } diff --git a/grails-app/services/org/grails/twitter/StatusService.groovy b/grails-app/services/org/grails/twitter/StatusService.groovy index 0f1460a..ed0bc9f 100755 --- a/grails-app/services/org/grails/twitter/StatusService.groovy +++ b/grails-app/services/org/grails/twitter/StatusService.groovy @@ -38,6 +38,25 @@ class StatusService { } } + def currentUserTimeline() { + def messages = twitterCache[springSecurityService.principal.username] + if (!messages) { + log.debug "No messages found in cache for user <${springSecurityService.principal.username}>. Querying database..." + def per = lookupPerson() + def query = Status.whereAny { + author { username == per.username } + if(per.followed) author in per.followed + }.order 'dateCreated', 'desc' + messages = query.list(max: 10) + + twitterCache[springSecurityService.principal.username] = messages + } + else { + log.debug "Status messages loaded from cache for user <${springSecurityService.principal.username}>." + } + messages + } + private lookupPerson() { Person.get(springSecurityService.principal.id) }