diff --git a/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java b/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java index 2a67f22f93bf402148426fd8becb0ce7c8f37dfc..0df167d323a016d756027fdfaabc42e7c66eace8 100644 --- a/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java +++ b/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java @@ -11,6 +11,7 @@ import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import es.uvigo.esei.daa.rest.PeopleResource; +import es.uvigo.esei.daa.rest.PetResource; import es.uvigo.esei.daa.rest.UsersResource; /** @@ -26,7 +27,8 @@ public class DAAExampleApplication extends Application { public Set> getClasses() { return Stream.of( PeopleResource.class, - UsersResource.class + UsersResource.class, + PetResource.class ).collect(toSet()); } diff --git a/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java b/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java index 09b8834c7b71d5176e012bcd6076d36eb275bb29..da5a7526f74be8e3803058c05b628ef180333844 100644 --- a/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java +++ b/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java @@ -16,6 +16,7 @@ import javax.ws.rs.core.Response; import es.uvigo.esei.daa.dao.DAOException; import es.uvigo.esei.daa.dao.PeopleDAO; +import es.uvigo.esei.daa.dao.PetDAO; import es.uvigo.esei.daa.entities.Person; /** @@ -208,4 +209,34 @@ public class PeopleResource { .build(); } } + + + + + + @GET + @Path("/{id}/pets") + public Response getPets( + @PathParam("id") int idMaster + ) { + try { + return Response.ok((new PetDAO()).listAllPetsOfPerson(idMaster)).build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error listing pets of the person", e); + return Response.serverError().entity(e.getMessage()).build(); + } + } + + + + + + + + + + + + + } diff --git a/src/main/java/es/uvigo/esei/daa/rest/PetResource.java b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java new file mode 100644 index 0000000000000000000000000000000000000000..a0d495b8a0b55a50d8c4cf7823f8199ee3f0ecf8 --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java @@ -0,0 +1,178 @@ +package es.uvigo.esei.daa.rest; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.ws.rs.DELETE; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import es.uvigo.esei.daa.dao.DAOException; +import es.uvigo.esei.daa.dao.PeopleDAO; +import es.uvigo.esei.daa.dao.PetDAO; +import es.uvigo.esei.daa.entities.Person; +import es.uvigo.esei.daa.entities.Pet; + + + +@Path("/pet") +@Produces(MediaType.APPLICATION_JSON) +public class PetResource { + + private final static Logger LOG = Logger.getLogger(PeopleResource.class.getName()); + + private final PetDAO dao; + + + public PetResource() { + this(new PetDAO()); + } + + public PetResource(PetDAO dao) { + this.dao = dao; + } + + + + + + @GET + @Path("/{idPet}") + public Response get( + @PathParam("idPet") int idPet + ) { + try { + final Pet pet = this.dao.get(idPet); + + return Response.ok(pet).build(); + + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid pet id in get method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error getting a pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } + + + @GET + public Response list() { + try { + return Response.ok(this.dao.list()).build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error listing pets", e); + return Response.serverError().entity(e.getMessage()).build(); + } + } + + + + + + @POST + public Response add( + @FormParam("name") String name, + @FormParam("idMaster") int idMaster + ) { + try { + final Pet newPet = this.dao.add(name, idMaster); + + return Response.ok(newPet).build(); + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid pet id in add method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error adding a pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } + + + + @PUT + @Path("/{idPet}") + public Response modify( + @PathParam("idPet") int idPet, + @FormParam("name") String name, + @FormParam("idMaster") int idMaster + ) { + try { + final Pet modifiedPet = new Pet(idPet, name, idMaster); + this.dao.modify(modifiedPet); + + return Response.ok(modifiedPet).build(); + } catch (NullPointerException npe) { + final String message = String.format("Invalid data for pet (name: %s, idMaster: %s)", name, idMaster); + + LOG.log(Level.FINE, message); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(message) + .build(); + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid idPet in modify method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error modifying a Pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } + + + @DELETE + @Path("/{idPet}") + public Response delete( + @PathParam("idPet") int idPet + ) { + try { + this.dao.delete(idPet); + + return Response.ok(idPet).build(); + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid idPet in delete method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error deleting a Pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } + + + + + + + +}