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 42299e5c2e5746aa21a48092025554eefaf04922..4ab9e92bb77464e9db9d15fef46ee04b397053b2 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java @@ -20,17 +20,20 @@ import es.uvigo.esei.daa.entities.Pet; */ public class PetDAO extends DAO { private final static Logger LOG = Logger.getLogger(PetDAO.class.getName()); - //TODO junto con lo de justo debajo + //TODO está puesto a 1 para testeos private int ownerID = 1; public int getOwnerID(){ return ownerID; } - /*//TODO ESTO IGUAL NO FUNCIONA LO TESTEO LUEGO - public PetDAO(int 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. * @@ -70,9 +73,11 @@ public class PetDAO extends DAO { */ public List list() throws DAOException { try (final Connection conn = this.getConnection()) { - final String query = "SELECT * FROM pet"; + final String query = "SELECT * FROM pet WHERE ownerID=?"; try (final PreparedStatement statement = conn.prepareStatement(query)) { + statement.setInt(1, ownerID); + try (final ResultSet result = statement.executeQuery()) { final List petList = new LinkedList<>(); @@ -193,9 +198,7 @@ public class PetDAO extends DAO { return new Pet( row.getInt("id"), row.getString("name"), - //TODO EXTREMO ESTO ES PROVISIONAL ownerID - //TODO REPITO PROVISIONAL MECA ); } } 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 1d5fccf255addc7ce702006b1953bb09829244f4..833ae85d5fc8c767568a3544d1a6a577daa39d77 100644 --- a/src/main/java/es/uvigo/esei/daa/rest/PetResource.java +++ b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java @@ -1,207 +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}. - */ - 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(); - } - } - - /** - * 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(); - } - } -} +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 diff --git a/src/main/webapp/js/dao/pet.js b/src/main/webapp/js/dao/pet.js index a60921d2495a23035b35f98979a8768ebe336cf7..5d97cebec8691d50307e119131f5ff864e173004 100644 --- a/src/main/webapp/js/dao/pet.js +++ b/src/main/webapp/js/dao/pet.js @@ -1,4 +1,4 @@ -var PetDAO = (function() { +var PetDAO = (function(ownerID) { var resourcePath = "rest/pet/"; var requestByAjax = function(data, done, fail, always) { done = typeof done !== 'undefined' ? done : function() {}; @@ -11,6 +11,18 @@ var PetDAO = (function() { .always(always); }; + var setOwnerIDPetDAO = function(ownerID, done, fail, always) { + var query = window.location.search.substring(1); + var split = query.split("="); + var getID = decodeURIComponent(split[1]); + //alert(getID); + requestByAjax({ + url: resourcePath + getID, + type: 'POST', + data: getID + }, done, fail, always); + }(); + function PetDAO() { this.listPets = function(done, fail, always) { requestByAjax({ @@ -19,6 +31,18 @@ var PetDAO = (function() { }, done, fail, always); }; + this.setOwnerIDPetDAO = function(ownerID, done, fail, always) { + var query = window.location.search.substring(1); + var split = query.split("="); + var getID = decodeURIComponent(split[1]); + //alert(getID); + requestByAjax({ + url: resourcePath + getID, + type: 'POST', + data: getID + }, 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 f9878ffea249390c422b9f5b815347c310e5d954..5ce579318349d15bec65dd7f16c30bea998341da 100644 --- a/src/main/webapp/js/view/pet.js +++ b/src/main/webapp/js/view/pet.js @@ -13,7 +13,7 @@ var PetView = (function() { function PetView(petDao, formContainerId, listContainerId) { dao = petDao; self = this; - + dao.setOwnerIDPetDAO(); insertPetForm($('#' + formContainerId)); insertPetList($('#' + listContainerId)); diff --git a/src/main/webapp/pet.html b/src/main/webapp/pet.html index 43288759482c2689ae2ce096eb3bee516a5e3630..ae40c843ce6efd69907883a70682dee24fe36776 100644 --- a/src/main/webapp/pet.html +++ b/src/main/webapp/pet.html @@ -7,7 +7,7 @@

People

- Logout + Logout Back
@@ -15,6 +15,7 @@