Skip to content

Commit

Permalink
move logic from controller to service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Brown committed Jun 20, 2012
1 parent 80cfa68 commit 76e2568
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
29 changes: 2 additions & 27 deletions grails-app/controllers/org/grails/twitter/StatusController.groovy
Expand Up @@ -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
Expand All @@ -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
}
}
19 changes: 19 additions & 0 deletions grails-app/services/org/grails/twitter/StatusService.groovy
Expand Up @@ -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)
}
Expand Down

0 comments on commit 76e2568

Please sign in to comment.