diff --git a/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java index 4ab9e92bb77464e9db9d15fef46ee04b397053b2..a7a43bc1486974b1a03aff0332f8ec18e843a7cd 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java @@ -20,30 +20,21 @@ import es.uvigo.esei.daa.entities.Pet; */ public class PetDAO extends DAO { private final static Logger LOG = Logger.getLogger(PetDAO.class.getName()); - //TODO está puesto a 1 para testeos - private int ownerID = 1; - - public int getOwnerID(){ - return ownerID; - } - public void setOwnerID(int ownerID) {this.ownerID=ownerID;} - - public PetDAO(int ownerID) { - this.ownerID = ownerID; - } public PetDAO(){} /** * Returns a pet stored persisted in the system. * + * + * @param ownerID necessary for the rowToEntity method * @param id identifier of the pet. * @return a pet with the provided identifier. * @throws DAOException if an error happens while retrieving the pet. * @throws IllegalArgumentException if the provided id does not corresponds * with any persisted pet. */ - public Pet get(int id) + public Pet get(int ownerID, int id) throws DAOException, IllegalArgumentException { try (final Connection conn = this.getConnection()) { final String query = "SELECT * FROM pet WHERE id=?"; @@ -53,7 +44,7 @@ public class PetDAO extends DAO { try (final ResultSet result = statement.executeQuery()) { if (result.next()) { - return rowToEntity(result); + return rowToEntity(ownerID ,result); } else { throw new IllegalArgumentException("Invalid id"); } @@ -67,11 +58,11 @@ public class PetDAO extends DAO { /** * Returns a list with all the pets persisted in the system. - * + * @param ownerID id of the owner of the pets to list * @return a list with all the pet persisted in the system. * @throws DAOException if an error happens while retrieving the pets. */ - public List list() throws DAOException { + public List list(int ownerID) throws DAOException { try (final Connection conn = this.getConnection()) { final String query = "SELECT * FROM pet WHERE ownerID=?"; @@ -82,7 +73,7 @@ public class PetDAO extends DAO { final List petList = new LinkedList<>(); while (result.next()) { - petList.add(rowToEntity(result)); + petList.add(rowToEntity(ownerID, result)); } return petList; @@ -98,12 +89,13 @@ public class PetDAO extends DAO { * Persists a new pet in the system. An identifier will be assigned * automatically to the new pet. * + * @param ownerID id of the owner of the pet. * @param name name of the new pet. Can't be {@code null}. * @return a {@link Pet} entity representing the persisted pet. * @throws DAOException if an error happens while persisting the new pet. * @throws IllegalArgumentException if the name is {@code null}. */ - public Pet add(String name) + public Pet add(int ownerID, String name) throws DAOException, IllegalArgumentException { if (name == null) { throw new IllegalArgumentException("name can't be null"); @@ -194,7 +186,7 @@ public class PetDAO extends DAO { } } - private Pet rowToEntity(ResultSet row) throws SQLException { + private Pet rowToEntity(int ownerID, ResultSet row) throws SQLException { return new Pet( row.getInt("id"), row.getString("name"), diff --git a/src/main/java/es/uvigo/esei/daa/rest/PetResource.java b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java index 833ae85d5fc8c767568a3544d1a6a577daa39d77..80afa56fdcd0a6df3eeb5053a9c664865dcac336 100644 --- a/src/main/java/es/uvigo/esei/daa/rest/PetResource.java +++ b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java @@ -1 +1 @@ -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.PetDAO; import es.uvigo.esei.daa.entities.Pet; /** * REST resource for managing pets. * * @author Martín Vázquez Torres */ @Path("/pet") @Produces(MediaType.APPLICATION_JSON) public class PetResource { private final static Logger LOG = Logger.getLogger(PetResource.class.getName()); private final PetDAO dao; /** * Constructs a new instance of {@link PetResource}. */ //TODO Este constructor tiene que meter aqui el señor id de la url y ya public PetResource() { this(new PetDAO()); } // Needed for testing purposes (parece ser) PetResource(PetDAO dao) { this.dao = dao; } /** * Returns a pet with the provided identifier. * * @param id the identifier of the pet to retrieve. * @return a 200 OK response with a pet that has the provided identifier. * If the identifier does not corresponds with any user, a 400 Bad Request * response with an error message will be returned. If an error happens * while retrieving the list, a 500 Internal Server Error response with an * error message will be returned. */ @GET @Path("/{id}") public Response get( @PathParam("id") int id ) { try { final Pet pet = this.dao.get(id); 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(); } } /** * Returns the complete list of the pets stored in the system. * * @return a 200 OK response with the complete list of pets stored in the * system. If an error happens while retrieving the list, a 500 Internal * Server Error response with an error message will be returned. */ @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(); } } /** * Sets the ownerID for this webpage. * * @param ownerID owner id for the webpage. * @return ok response */ @POST @Path("/{id}") public Response cambiarDAO(@PathParam("id") int ownerID) { dao.setOwnerID(ownerID); return Response.ok(ownerID).build(); } /** * Creates a new pet in the system. * * @param name the name of the new pet. * @return a 200 OK response with a pet that has been created. If the * name is not provided, a 400 Bad Request response with an * error message will be returned. If an error happens while retrieving the * list, a 500 Internal Server Error response with an error message will be * returned. */ @POST public Response add( @FormParam("name") String name ) { try { final Pet newPet = this.dao.add(name); 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(); } } /** * Modifies the data of a pet. * * @param id identifier of the pet to modify. * @param name the new name of the pet. * @return a 200 OK response with a pet that has been modified. If the * identifier does not corresponds with any user or the name is * not provided, a 400 Bad Request response with an error message will be * returned. If an error happens while retrieving the list, a 500 Internal * Server Error response with an error message will be returned. */ @PUT @Path("/{id}") public Response modify( @PathParam("id") int id, @FormParam("name") String name ) { try { final Pet modifiedPet = new Pet(id, name, dao.getOwnerID()); this.dao.modify(modifiedPet); return Response.ok(modifiedPet).build(); } catch (NullPointerException npe) { final String message = String.format("Invalid data for pet (name: %s)", name); LOG.log(Level.FINE, message); return Response.status(Response.Status.BAD_REQUEST) .entity(message) .build(); } catch (IllegalArgumentException iae) { LOG.log(Level.FINE, "Invalid pet id 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(); } } /** * Deletes a pet from the system. * * @param id the identifier of the pet to be deleted. * @return a 200 OK response with the identifier of the pet that has * been deleted. If the identifier does not corresponds with any user, a 400 * Bad Request response with an error message will be returned. If an error * happens while retrieving the list, a 500 Internal Server Error response * with an error message will be returned. */ @DELETE @Path("/{id}") public Response delete( @PathParam("id") int id ) { try { this.dao.delete(id); return Response.ok(id).build(); } catch (IllegalArgumentException iae) { LOG.log(Level.FINE, "Invalid pet id 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(); } } } \ No newline at end of file +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.PetDAO; import es.uvigo.esei.daa.entities.Pet; /** * REST resource for managing pets. * * @author Martín Vázquez Torres */ @Path("/pet") @Produces(MediaType.APPLICATION_JSON) public class PetResource { private final static Logger LOG = Logger.getLogger(PetResource.class.getName()); private final PetDAO dao; /** * Constructs a new instance of {@link PetResource}. */ //TODO Este constructor tiene que meter aqui el señor id de la url y ya public PetResource() { this(new PetDAO()); } // Needed for testing purposes (parece ser) PetResource(PetDAO dao) { this.dao = dao; } /** * Returns a pet with the provided identifier. * * @param id the identifier of the pet to retrieve. * @return a 200 OK response with a pet that has the provided identifier. * If the identifier does not corresponds with any user, a 400 Bad Request * response with an error message will be returned. If an error happens * while retrieving the list, a 500 Internal Server Error response with an * error message will be returned. */ @GET @Path("/{ownerID}/{id}") public Response get( @PathParam("ownerID") int ownerID, @PathParam("id") int id ) { try { final Pet pet = this.dao.get(ownerID, id); 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(); } } /** * Returns the complete list of the pets stored in the system. * * @return a 200 OK response with the complete list of pets stored in the * system. If an error happens while retrieving the list, a 500 Internal * Server Error response with an error message will be returned. */ @GET @Path("/{ownerID}") public Response list(@PathParam("ownerID") int ownerID) { try { return Response.ok(this.dao.list(ownerID)).build(); } catch (DAOException e) { LOG.log(Level.SEVERE, "Error listing pets", e); return Response.serverError().entity(e.getMessage()).build(); } } /* /** * Sets the ownerID for this webpage. * * @param ownerID owner id for the webpage. * @return ok response @POST @Path("/{id}") public Response cambiarDAO(@PathParam("id") int ownerID) { dao.setOwnerID(ownerID); return Response.ok(ownerID).build(); } */ /** * Creates a new pet in the system. * * @param name the name of the new pet. * @return a 200 OK response with a pet that has been created. If the * name is not provided, a 400 Bad Request response with an * error message will be returned. If an error happens while retrieving the * list, a 500 Internal Server Error response with an error message will be * returned. */ @POST @Path("/{ownerID}") public Response add( @PathParam("ownerID") int ownerID, @FormParam("name") String name ) { try { final Pet newPet = this.dao.add(ownerID, name); 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(); } } /** * Modifies the data of a pet. * * @param id identifier of the pet to modify. * @param name the new name of the pet. * @return a 200 OK response with a pet that has been modified. If the * identifier does not corresponds with any user or the name is * not provided, a 400 Bad Request response with an error message will be * returned. If an error happens while retrieving the list, a 500 Internal * Server Error response with an error message will be returned. */ @PUT @Path("/{ownerID}/{id}") public Response modify( @PathParam("id") int id, @PathParam("ownerID") int ownerID, @FormParam("name") String name ) { try { final Pet modifiedPet = new Pet(id, name, ownerID); this.dao.modify(modifiedPet); return Response.ok(modifiedPet).build(); } catch (NullPointerException npe) { final String message = String.format("Invalid data for pet (name: %s)", name); LOG.log(Level.FINE, message); return Response.status(Response.Status.BAD_REQUEST) .entity(message) .build(); } catch (IllegalArgumentException iae) { LOG.log(Level.FINE, "Invalid pet id 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(); } } /** * Deletes a pet from the system. * * @param id the identifier of the pet to be deleted. * @return a 200 OK response with the identifier of the pet that has * been deleted. If the identifier does not corresponds with any user, a 400 * Bad Request response with an error message will be returned. If an error * happens while retrieving the list, a 500 Internal Server Error response * with an error message will be returned. */ @DELETE @Path("/{ownerID}/{id}") public Response delete( @PathParam("id") int id ) { try { this.dao.delete(id); return Response.ok(id).build(); } catch (IllegalArgumentException iae) { LOG.log(Level.FINE, "Invalid pet id 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(); } } } \ No newline at end of file diff --git a/src/main/webapp/js/dao/pet.js b/src/main/webapp/js/dao/pet.js index 628d28d49f7b96ee9cc21a01f8375148fd773d2a..30a0c566cd5a5327b91e1632525d3a56dc08569b 100644 --- a/src/main/webapp/js/dao/pet.js +++ b/src/main/webapp/js/dao/pet.js @@ -12,6 +12,11 @@ var PetDAO = (function() { }; function PetDAO() { + //This method serves as initialization for the resourcePath var fot the current webpage + this.addToResourcePath = function(ownerID) { + resourcePath = resourcePath + ownerID + "/"; + } + this.listPets = function(done, fail, always) { requestByAjax({ url: resourcePath, @@ -19,14 +24,6 @@ var PetDAO = (function() { }, done, fail, always); }; - this.setOwnerIDPetDAO = function(ownerID, done, fail, always) { - requestByAjax({ - url: resourcePath + ownerID, - type: 'POST', - data: ownerID - }, done, fail, always); - }; - this.addPet = function(pet, done, fail, always) { requestByAjax({ url: resourcePath, diff --git a/src/main/webapp/js/view/pet.js b/src/main/webapp/js/view/pet.js index 7d2bb508ed3196d19f6d46df116aed367ea0245b..b73750b77b9ccda20ff37a1efae4c26a151a6fce 100644 --- a/src/main/webapp/js/view/pet.js +++ b/src/main/webapp/js/view/pet.js @@ -21,8 +21,9 @@ var PetView = (function() { dao = petDao; self = this; - dao.setOwnerIDPetDAO(getQueryParameter('id')); - + //This has to be executed prior to everything for the correct execution of every other method in dao + dao.addToResourcePath(getQueryParameter("id")); + insertPetForm($('#' + formContainerId)); insertPetList($('#' + listContainerId)); diff --git a/src/main/webapp/pet.html b/src/main/webapp/pet.html index ae40c843ce6efd69907883a70682dee24fe36776..fdafdc14aa5f2f99cbf4661dd083a4631f4d5b10 100644 --- a/src/main/webapp/pet.html +++ b/src/main/webapp/pet.html @@ -6,7 +6,7 @@
-

People

+

Pets

Logout Back