Skip to content

Instantly share code, notes, and snippets.

@erichelgeson
Created November 8, 2018 14:42
Show Gist options
  • Save erichelgeson/59fed6478cb3d43c4d64e8f1a1b92dfa to your computer and use it in GitHub Desktop.
Save erichelgeson/59fed6478cb3d43c4d64e8f1a1b92dfa to your computer and use it in GitHub Desktop.
import grails.compiler.GrailsCompileStatic
import groovy.transform.SelfType
import grails.artefact.Controller
import org.springframework.http.HttpStatus
import static org.springframework.http.HttpStatus.*
@SelfType(Controller)
trait ResponseTrait {
void notFound() {
renderStatus NOT_FOUND
}
void ok() {
renderStatus OK
}
void created() {
renderStatus CREATED
}
void badRequest() {
renderStatus BAD_REQUEST
}
void noContent() {
renderStatus NO_CONTENT
}
void renderStatus(HttpStatus status) {
render status: status
}
/*
Takes a validatable object and responds the object or with its errors
Can force validation method as well
*/
void validationRespond(def validatableObj, boolean shouldValidate = false) {
if(validatableObj == null) {
notFound()
return
}
if(shouldValidate) {
validate?.validate()
}
if(validatableObj?.hasErrors()) {
respond(validatableObj.errors)
} else {
respond(validatableObj)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment