Commit c07ce0ea authored by cyanide4all's avatar cyanide4all

class

parent 3f752435
......@@ -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<Pet> 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<Pet> 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
);
}
}
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
......
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,
......
......@@ -13,7 +13,7 @@ var PetView = (function() {
function PetView(petDao, formContainerId, listContainerId) {
dao = petDao;
self = this;
dao.setOwnerIDPetDAO();
insertPetForm($('#' + formContainerId));
insertPetList($('#' + listContainerId));
......
......@@ -7,7 +7,7 @@
<body>
<div id="pet-container">
<h1>People</h1>
<a id="#logout" href="logout">Logout</a>
<a id="#logout" href="logout">Logout</a> <a id="#atras" href="main.html">Back</a>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
......@@ -15,6 +15,7 @@
<script type="text/javascript" src="js/view/pet.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var view = new PetView(new PetDAO(), 'pet-container', 'pet-container');
view.init();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment