Created
January 28, 2017 09:04
-
-
Save dilunika/df64f59bd947c8386b64fb06962e1792 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
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