Grails: Search and pagination example implemented in MyBlog

Grails: Search and pagination example implemented in MyBlog
[code language=”Groovy”]
#Controller

class PostController {
…..
def list(Integer max) {
def postList
def postTotal
def hisPostList
hisPostList=[]
params.max = Math.min(max ?: 3, 6)

if ( params.searchquery == null) {
postList = Post.list(params)
postTotal = Post.count()
}
else{
postList = Post.findAllByTitleLikeOrBodyLike(‘%’+params.searchquery+’%’,’%’+params.searchquery+’%’,params)
postTotal = Post.findAllByTitleLikeOrBodyLike(‘%’+params.searchquery+’%’,’%’+params.searchquery+’%’).size()
}

if (SecurityUtils.subject.authenticated){
def user = currentUser()
hisPostList = user.blog.posts
}

[postList: postList, postTotal: postTotal, params: params, hisPostList: hisPostList ]
}
……
}

# Template file

<div id="list-post" class="content scaffold-list" role="main">

<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>

<g:render template="/blog/post" var="post" collection="${postList}" />

<div class="pagination">
<g:paginate total="${postTotal}" action="list" params="${params}"/>
</div>
</div>

[/code]