Created
November 8, 2018 14:42
-
-
Save erichelgeson/59fed6478cb3d43c4d64e8f1a1b92dfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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