Añadida toda la logica necesaria para la gestión de mascotas (Entidad, DAO, REST)

parent 0d4ded04
...@@ -11,6 +11,7 @@ import javax.ws.rs.ApplicationPath; ...@@ -11,6 +11,7 @@ import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application; import javax.ws.rs.core.Application;
import es.uvigo.esei.daa.rest.PeopleResource; import es.uvigo.esei.daa.rest.PeopleResource;
import es.uvigo.esei.daa.rest.PetsResource;
import es.uvigo.esei.daa.rest.UsersResource; import es.uvigo.esei.daa.rest.UsersResource;
/** /**
...@@ -26,7 +27,8 @@ public class DAAExampleApplication extends Application { ...@@ -26,7 +27,8 @@ public class DAAExampleApplication extends Application {
public Set<Class<?>> getClasses() { public Set<Class<?>> getClasses() {
return Stream.of( return Stream.of(
PeopleResource.class, PeopleResource.class,
UsersResource.class UsersResource.class,
PetsResource.class
).collect(toSet()); ).collect(toSet());
} }
......
package es.uvigo.esei.daa.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import es.uvigo.esei.daa.entities.Person;
import es.uvigo.esei.daa.entities.Pet;
public class PetsDAO extends DAO{
private final static Logger LOG = Logger.getLogger(PetsDAO.class.getName());
public List<Pet> list(int personID) throws DAOException {
try (final Connection conn = this.getConnection()) {
final String query = "SELECT * FROM pet WHERE owner=?";
try (final PreparedStatement statement = conn.prepareStatement(query)) {
statement.setInt(1, personID);
try (final ResultSet result = statement.executeQuery()) {
final List<Pet> pets = new LinkedList<>();
while (result.next()) {
pets.add(rowToEntity(result));
}
return pets;
}
}
} catch (SQLException e) {
LOG.log(Level.SEVERE, "Error listing pets", e);
throw new DAOException(e);
}
}
public Pet add(String name, int owner) throws DAOException, IllegalArgumentException {
if (name == null) {
throw new IllegalArgumentException("name can't be null");
}
try (Connection conn = this.getConnection()) {
final String query = "INSERT INTO pet VALUES(null, ?, ?)";
try (PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
statement.setString(1, name);
statement.setInt(2, owner);
if (statement.executeUpdate() == 1) {
try (ResultSet resultKeys = statement.getGeneratedKeys()) {
if (resultKeys.next()) {
return new Pet(resultKeys.getInt(1), name, new PeopleDAO().get(owner));
} else {
LOG.log(Level.SEVERE, "Error retrieving inserted id");
throw new SQLException("Error retrieving inserted id");
}
}
} else {
LOG.log(Level.SEVERE, "Error inserting value");
throw new SQLException("Error inserting value");
}
}
} catch (SQLException e) {
LOG.log(Level.SEVERE, "Error adding a pet", e);
throw new DAOException(e);
}
}
public void delete(int id) throws DAOException, IllegalArgumentException {
try (final Connection conn = this.getConnection()) {
final String query = "DELETE FROM pet WHERE id=?";
try (final PreparedStatement statement = conn.prepareStatement(query)) {
statement.setInt(1, id);
if (statement.executeUpdate() != 1) {
throw new IllegalArgumentException("Invalid id");
}
}
} catch (SQLException e) {
LOG.log(Level.SEVERE, "Error deleting a pet", e);
throw new DAOException(e);
}
}
public void modify(Pet pet) throws DAOException, IllegalArgumentException {
if (pet == null) {
throw new IllegalArgumentException("pet can't be null");
}
try (Connection conn = this.getConnection()) {
final String query = "UPDATE pet SET name=? WHERE id=?";
try (PreparedStatement statement = conn.prepareStatement(query)) {
statement.setString(1, pet.getName());
statement.setInt(2, pet.getId());
if (statement.executeUpdate() != 1) {
throw new IllegalArgumentException("name can't be null");
}
}
} catch (SQLException e) {
LOG.log(Level.SEVERE, "Error modifying a pet", e);
throw new DAOException();
}
}
private Pet rowToEntity(ResultSet row) throws SQLException, IllegalArgumentException, DAOException {
return new Pet(row.getInt("id"), row.getString("name"), new PeopleDAO().get(row.getInt("owner")));
}
}
package es.uvigo.esei.daa.entities;
import static java.util.Objects.requireNonNull;
public class Pet {
private int id;
private String name;
private Person owner;
Pet() {}
public Pet(int id, String name, Person owner) {
this.id = id;
this.setName(name);
this.setOwner(owner);
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = requireNonNull(name, "Name can't be null");
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = requireNonNull(owner, "Owner can't be null");
}
@Override
public int hashCode() {
final int prime = 41;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Pet))
return false;
Pet other = (Pet) obj;
if (id != other.id)
return false;
return true;
}
}
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.PetsDAO;
import es.uvigo.esei.daa.entities.Person;
import es.uvigo.esei.daa.entities.Pet;
@Path("/people/{idOwner}/pets")
@Produces(MediaType.APPLICATION_JSON)
public class PetsResource {
private final static Logger LOG = Logger.getLogger(PeopleResource.class.getName());
private final PetsDAO petDAO;
public PetsResource() {
this(new PetsDAO());
}
PetsResource(PetsDAO petsDAO) {
this.petDAO = petsDAO;
}
@GET
public Response getPets( @PathParam("idOwner") int id ) {
try {
return Response.ok(this.petDAO.list(id)).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, @PathParam("idOwner") int owner) {
try {
final Pet newPet = this.petDAO.add(name, owner);
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 id, @FormParam("name") String name, @PathParam("idOwner") int idOwner) {
try {
Person owner = new PeopleDAO().get(idOwner);
final Pet modifiedPet = new Pet(id, name, owner);
this.petDAO.modify(modifiedPet);
return Response.ok(modifiedPet).build();
} catch (NullPointerException npe) {
final String message = String.format("Invalid data for pet ");
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();
}
}
@DELETE
@Path("/{idPet}")
public Response delete(@PathParam("idPet") String idPet) {
try {
this.petDAO.delete(Integer.parseInt(idPet));
return Response.ok(Integer.parseInt(idPet)).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();
}
}
}
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