Skip to content

Instantly share code, notes, and snippets.

@dilunika
Created January 28, 2017 09:04
Show Gist options
  • Save dilunika/df64f59bd947c8386b64fb06962e1792 to your computer and use it in GitHub Desktop.
Save dilunika/df64f59bd947c8386b64fb06962e1792 to your computer and use it in GitHub Desktop.
public class VehicleOwnerRoute {
public static void initialize(Router router) {
router.route("/api/vehicleowners*").handler(BodyHandler.create());
router.post("/api/vehicleowners").handler(VehicleOwnerRoute::create);
}
public static void create(RoutingContext routingContext) {
VehicleOwner vo = Json.decodeValue(routingContext.getBodyAsString(), VehicleOwner.class);
if(vo == null) {
ErrorResponse err = new ErrorResponse("Empty content is not allowed.");
generateResponse(routingContext, 400, err);
} else {
ValidationResult validationResult = vo.validate();
if(validationResult.isValid()) {
VehicleOwnerService.insert(vo).subscribe(rowsUpdated -> {
if(rowsUpdated == 1){
generateResponse(routingContext, 201, null);
} else {
generateResponse(routingContext, 500, "Failed to add Vehicle Owner. Could not persist.");
}
}, err -> {
err.printStackTrace();
generateResponse(routingContext, 500, "Failed to add Vehicle Owner. Internal Error");
});
} else {
ErrorResponse err = new ErrorResponse(validationResult.getErrorMessages());
generateResponse(routingContext, 400, err);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment