Search code examples
grailsforumgrails-controller

Creating a standard topic


I'm having some problems with the creation of a web forum using grails. In my Controller, I need to create a standard topic for the website work, I'm using a code of a tutorial. So my question is: how do I create a standard Topic in order to this code work?

The part that I need to create is at line 11.

Controller:

class ForumController {
def springSecurityService

def home() {
    [sections:Section.listOrderByTitle()]
}

def topic(long topicId) {
    Topic topic = Topic.get(topicId)

    if (topic == null){


    }


    params.max = 10
    params.sort = 'createDate'
    params.order = 'desc'

    [threads:DiscussionThread.findAllByTopic(topic, params),
     numberOfThreads:DiscussionThread.countByTopic(topic), topic:topic]
}

def thread(long threadId) {
    DiscussionThread thread = DiscussionThread.get(threadId)

    params.max = 10
    params.sort = 'createDate'
    params.order = 'asc'

    [comments:Comment.findAllByThread(thread, params),
     numberOfComments:Comment.countByThread(thread), thread:thread]

}


@Secured(['ROLE_USER'])
def postReply(long threadId, String body) {
    def offset = params.offset
    if (body != null && body.trim().length() > 0) {
        DiscussionThread thread = DiscussionThread.get(threadId)
        def commentBy = springSecurityService.currentUser
        new Comment(thread:thread, commentBy:commentBy, body:body).save()

        // go to last page so user can view his comment
        def numberOfComments = Comment.countByThread(thread)
        def lastPageCount = numberOfComments % 10 == 0 ? 10 : numberOfComments % 10
        offset = numberOfComments - lastPageCount
    }
    redirect(action:'thread', params:[threadId:threadId, offset:offset])
}
}

Solution

  • Currently you're trying to find an instance of the Topic domain class corresponding to the provided topicId first, and then you check to see if the topic is null.

    This is an issue as if the topicId is null, the lookup will fail and throw a null pointer exception.

    To fix this you simply wrap the lookup in an if-null check as displayed below to ensure you actually have a valid topicId.

    Your other question (how to actually set a default) is a bit more intuitive. If no topic is found, simply create one with default a default constructor or provide key:value pairs to the constructor. [see code below for an example]. For more information on the Grails Object Relational Mapping system you should check out their documentation.

    def topic(long topicId) {
        Topic topic
    
        /* If you have a valid topicId perform a lookup. */
        if (topicId != null){
            topic = Topic.get(topicId)
        }
    
        /* If the topic hasn't been set correctly, create one with default values. */
        if (topic == null) {
            topic = new Topic()
           /* You may want to have a look at the grails docs to see how this works. */
            toipic = new Topic(name: "Default", priority: "Highest")
        }
    
        params.max = 10
        params.sort = 'createDate'
        params.order = 'desc'
    
        [threads:DiscussionThread.findAllByTopic(topic, params),
         numberOfThreads:DiscussionThread.countByTopic(topic), topic:topic]
    }