From dfac988df8b483e3ca378bde80d6342ff4015e15 Mon Sep 17 00:00:00 2001 From: Santi Date: Thu, 7 Mar 2019 00:45:14 +0100 Subject: [PATCH] Added get to pets resource --- .../uvigo/esei/daa/DAAExampleApplication.java | 15 +++-- .../java/es/uvigo/esei/daa/dao/PetsDAO.java | 44 +++++++++++++ .../java/es/uvigo/esei/daa/entities/Pet.java | 64 ++++++++++++++++++ .../es/uvigo/esei/daa/rest/PetsResource.java | 66 +++++++++++++++++++ 4 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java create mode 100644 src/main/java/es/uvigo/esei/daa/entities/Pet.java create mode 100644 src/main/java/es/uvigo/esei/daa/rest/PetsResource.java diff --git a/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java b/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java index 2a67f22..1ee3d31 100644 --- a/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java +++ b/src/main/java/es/uvigo/esei/daa/DAAExampleApplication.java @@ -1,17 +1,17 @@ package es.uvigo.esei.daa; -import static java.util.stream.Collectors.toSet; +import es.uvigo.esei.daa.rest.PeopleResource; +import es.uvigo.esei.daa.rest.PetsResource; +import es.uvigo.esei.daa.rest.UsersResource; +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.stream.Stream; -import javax.ws.rs.ApplicationPath; -import javax.ws.rs.core.Application; - -import es.uvigo.esei.daa.rest.PeopleResource; -import es.uvigo.esei.daa.rest.UsersResource; +import static java.util.stream.Collectors.toSet; /** * Configuration of the REST application. This class includes the resources and @@ -26,7 +26,8 @@ public class DAAExampleApplication extends Application { public Set> getClasses() { return Stream.of( PeopleResource.class, - UsersResource.class + UsersResource.class, + PetsResource.class ).collect(toSet()); } diff --git a/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java new file mode 100644 index 0000000..dd08d21 --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java @@ -0,0 +1,44 @@ +package es.uvigo.esei.daa.dao; + +import es.uvigo.esei.daa.entities.Pet; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class PetsDAO extends DAO { + private final static Logger LOG = Logger.getLogger(PetsDAO.class.getName()); + + + public Pet get(int id) throws DAOException, IllegalArgumentException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pet WHERE id=?"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + statement.setInt(1, id); + + try (final ResultSet result = statement.executeQuery()) { + if (result.next()) { + return rowToEntity(result); + } else { + throw new IllegalArgumentException("Invalid id"); + } + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error getting a person", e); + throw new DAOException(e); + } + } + + private Pet rowToEntity(ResultSet row) throws SQLException { + return new Pet( + row.getInt("id"), + row.getInt("owner"), + row.getString("name") + ); + } +} diff --git a/src/main/java/es/uvigo/esei/daa/entities/Pet.java b/src/main/java/es/uvigo/esei/daa/entities/Pet.java new file mode 100644 index 0000000..d6e6d81 --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -0,0 +1,64 @@ +package es.uvigo.esei.daa.entities; + +public class Pet { + private int id; + private String name; + private int owner; + + + // Constructor needed for the JSON conversion + Pet() {} + + /** + * Constructs a new instance of {@link Pet} + * + * @param id idintefier of the pet + * @param name name of the pet + * @param owner identifier of the pet ownet + */ + public Pet(int id, int owner, String name) { + this.id = id; + this.name = name; + this.owner = owner; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getOwner() { + return owner; + } + + public void setOwner(int owner) { + this.owner = owner; + } + + public boolean equals(Object object) { + if (this == object) return true; + if (!(object instanceof Pet)) return false; + if (!super.equals(object)) return false; + Pet pet = (Pet) object; + return getId() == pet.getId() && + getOwner() == pet.getOwner() && + java.util.Objects.equals(getName(), pet.getName()); + } + + public int hashCode() { + return java.util.Objects.hash(super.hashCode(), getId(), getName(), getOwner()); + } +} + + diff --git a/src/main/java/es/uvigo/esei/daa/rest/PetsResource.java b/src/main/java/es/uvigo/esei/daa/rest/PetsResource.java new file mode 100644 index 0000000..3ddd23e --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/rest/PetsResource.java @@ -0,0 +1,66 @@ +package es.uvigo.esei.daa.rest; + +import es.uvigo.esei.daa.dao.DAOException; +import es.uvigo.esei.daa.dao.PetsDAO; +import es.uvigo.esei.daa.entities.Pet; + +import javax.ws.rs.GET; +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 java.util.logging.Level; +import java.util.logging.Logger; + +/** + * REST resource for managing pets. + * + * @author sgvilar. + */ +@Path("/pets") +@Produces(MediaType.APPLICATION_JSON) +public class PetsResource { + private final static Logger LOG = Logger.getLogger(PetsResource.class.getName()); + + private final PetsDAO dao; + + PetsResource(PetsDAO dao) { + this.dao = dao; + } + + + /** + * Returns a person with the provided identifier. + * + * @param id the identifier of the person to retrieve. + * @return a 200 OK response with a person 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(); + } + } +} -- 2.18.1