Open avbravo opened 3 months ago
@Microservices(security=SecurityType.YES,,rolesAllowed={"admin","guest"}) public interface PersonaController extends MicroServicesTemplate<Persona,String> { @Endpoint(path="name",type=HttpType.GET, security=SecurityType.NONE,rolesAllowed={"admin","guest"}) List<Persona> findByName(String name); }
@Path("persona") @RequestScoped public class PersonaControllerImpl implements Serializable{ @Inject PersonaRepository personaRepository; @GET public List<Persona> findAll() { return personaRepository.findAll(); } @GET @Path("id") public Persona findById(@QueryParam("id") String id) { return personaRepository.findByPk(new ObjectId(id)).orElseThrow( () -> new WebApplicationException("No hay persona con idpersona " + id, Response.Status.NOT_FOUND)); } @POST public Response save( @RequestBody(description = "Crea una persona.", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Persona.class))) Persona persona) { Optional<Persona> personaOptional = personaRepository.save(persona); if (personaOptional.isPresent()) { return Response.status(201).entity(personaOptional.get()).build(); } else { return Response.status(400).entity("Error " + personaRepository.getJmoordbException().getLocalizedMessage()).build(); } } @PUT public Response update( @RequestBody(description = "Actualiza persona", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Persona.class))) Persona persona) { if (personaRepository.update(persona)) { return Response.status(201).entity(persona).build(); } else { return Response.status(400).entity("Error " + personaRepository.getJmoordbException().getLocalizedMessage()).build(); } } @DELETE @Path("id") public Response delete(@QueryParam("id") String id) { if (personaRepository.deleteByPk(new ObjectId(id)) == 0L) { return Response.status(201).entity(Boolean.TRUE).build(); } else { return Response.status(400).entity("Error " + personaRepository.getJmoordbException().getLocalizedMessage()).build(); } } @DELETE @Path("deletemany") public Response deleteMany(@QueryParam("filter") String filter, @QueryParam("idestacion") Long idestacion, @QueryParam("anio") Integer anio, @QueryParam("mes") Integer mes) { Search search = DocumentUtil.convertForLookup(filter, "", 0, 0); if (personaRepository.deleteMany(search) == 0L) { return Response.status(201).entity(Boolean.TRUE).build(); } else { return Response.status(400).entity("Error " + personaRepository.getJmoordbException().getLocalizedMessage()).build(); } } @GET @Path("lookup") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public List<Persona> lookup(@QueryParam("filter") String filter, @QueryParam("sort") String sort, @QueryParam("page") Integer page, @QueryParam("size") Integer size, @QueryParam("idestacion") Long idestacion, @QueryParam("anio") Integer anio, @QueryParam("mes") Integer mes) { List<Persona> suggestions = new ArrayList<>(); try { Search search = DocumentUtil.convertForLookup(filter, sort, page, size); suggestions = personaRepository.lookup(search); } catch (Exception e) { MessagesUtil.error(MessagesUtil.nameOfClassAndMethod() + "error: " + e.getLocalizedMessage()); } System.out.println("Resultado: " + suggestions); return suggestions; } @GET @Path("count") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Long count(@QueryParam("filter") String filter, @QueryParam("sort") String sort, @QueryParam("page") Integer page, @QueryParam("size") Integer size, @QueryParam("idestacion") Long idestacion, @QueryParam("anio") Integer anio, @QueryParam("mes") Integer mes) { Long result = 0L; try{ Search search = DocumentUtil.convertForLookup(filter, sort, page, size); result = personaRepository.count(search); } catch (Exception e) { MessagesUtil.error(MessagesUtil.nameOfClassAndMethod() + "error: " + e.getLocalizedMessage()); } return result; } }
Genera internamente una interface