n92
n92

Reputation: 7592

How to write conditions in grails createCriteria()

I have a domain class Feedback

class Feedback{
    String name
    String email
    Date createdOn
    Boolean isMailSent
}

I want to fetch all feedbacks with the condition as below 

1) isMailSent ==  false

2) (createdOn - cuurent time) >= 15 minutes 

How to write these conditions in createCriteria() methiod.

what i have tried here

def feedbackList = Feedback.createCriteria().list(
        ) {
            eq('isMailSent ',  false)
            /** here condition (createdOn - cuurent time) >= 15 minutes **/
       }

Upvotes: 0

Views: 271

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

Here is an example of how you can accomplish this. I made this slightly more verbose so it's easier to read.

import groovy.time.TimeCategory

def now = new Date()
def fifteenMinutesAgo = null
use(TimeCategory) {
    fifteenMinutesAgo = now - 15.minutes
}

def feedbackList = Feedback.createCriteria().list() {
  eq('isMailSent', false)
  le('createdOn', fifteeenMinutesAgo)
}

Upvotes: 3

Related Questions