88
99import javax .annotation .PostConstruct ;
1010import javax .validation .ConstraintViolation ;
11+ import javax .validation .ConstraintViolationException ;
1112import javax .validation .Validation ;
1213import javax .validation .Validator ;
1314import javax .validation .ValidatorFactory ;
@@ -88,18 +89,13 @@ public Response obtenerResultados(String grupoPolitico) {
8889 }
8990
9091 @ Override
91- public Response emitirVoto (EmisionVotoDto resource ) {
92+ public Response emitirVoto (EmisionVotoDto resource ) throws ConstraintViolationException {
9293 try {
9394 ValidatorFactory factory = Validation .buildDefaultValidatorFactory ();
9495 Validator validator = factory .getValidator ();
9596 Set <ConstraintViolation <EmisionVotoDto >> violations = validator .validate (resource );
9697 if (violations .size () > 0 ) {
97- StringBuilder errors = new StringBuilder ();
98- for (ConstraintViolation <EmisionVotoDto > violation : violations ) {
99- errors .append (violation .getMessage () + ", " );
100- }
101- return Response .status (Response .Status .BAD_REQUEST ).entity (new EmisionVotoResponse (errors .toString ()))
102- .build ();
98+ throw new ConstraintViolationException (violations );
10399 }
104100
105101 GrupoPolitico grupoPolitico = this .grupoPoliticoService .findByName (resource .getGrupoPolitico ());
@@ -112,6 +108,8 @@ public Response emitirVoto(EmisionVotoDto resource) {
112108 this .votoService .emitirVoto (voto );
113109 VotoDto votoDto = this .mapper .convertFrom (voto , VotoDto .class );
114110 return Response .status (Response .Status .CREATED ).entity (new EmisionVotoResponse (votoDto )).build ();
111+ } catch (ConstraintViolationException e ) {
112+ throw e ;
115113 } catch (Exception e ) {
116114 return Response .status (Response .Status .INTERNAL_SERVER_ERROR )
117115 .entity (new EmisionVotoResponse (e .getMessage ())).build ();
0 commit comments