Last active
May 3, 2017 16:39
-
-
Save arcesino/5732227 to your computer and use it in GitHub Desktop.
Grails: Split & bind comma separated string to List<String>
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
class SkuProfilesController { | |
def skuProfileService | |
def show() { | |
paramsSplit(['ids', 'fields']) | |
def cmd = bindData(new ListSkuProfilesCommand(), params) | |
if (!cmd.validate()) { | |
throw new EntityValidationException(cmd.errors) | |
} | |
restpond skuProfileService.findAllSkuProfilesByVerticalAndFields(cmd) | |
} | |
private void paramsSplit(List<String> splitableParams) { | |
splitableParams.each { splitableParam -> | |
def commaSeparatedString = params[splitableParam]?.toString() ?: '' | |
params[splitableParam] = commaSeparatedString.split(',').toList() | |
} | |
} | |
} | |
@Validateable | |
class ListSkuProfilesCommand { | |
Long verticalId | |
List<Long> ids | |
List<String> fields | |
static constraints = { | |
verticalId nullable: false | |
ids nullable: false, size: 1..20 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worth to mention that validation are still enforced without problem