Add PetResource and modify DAAExampleApplication

parent 5a372e59
...@@ -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.PetResource;
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,
PetResource.class
).collect(toSet()); ).collect(toSet());
} }
......
...@@ -16,6 +16,7 @@ import javax.ws.rs.core.Response; ...@@ -16,6 +16,7 @@ import javax.ws.rs.core.Response;
import es.uvigo.esei.daa.dao.DAOException; import es.uvigo.esei.daa.dao.DAOException;
import es.uvigo.esei.daa.dao.PeopleDAO; import es.uvigo.esei.daa.dao.PeopleDAO;
import es.uvigo.esei.daa.dao.PetDAO;
import es.uvigo.esei.daa.entities.Person; import es.uvigo.esei.daa.entities.Person;
/** /**
...@@ -208,4 +209,34 @@ public class PeopleResource { ...@@ -208,4 +209,34 @@ public class PeopleResource {
.build(); .build();
} }
} }
@GET
@Path("/{id}/pets")
public Response getPets(
@PathParam("id") int idMaster
) {
try {
return Response.ok((new PetDAO()).listAllPetsOfPerson(idMaster)).build();
} catch (DAOException e) {
LOG.log(Level.SEVERE, "Error listing pets of the person", 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.PeopleDAO;
import es.uvigo.esei.daa.dao.PetDAO;
import es.uvigo.esei.daa.entities.Person;
import es.uvigo.esei.daa.entities.Pet;
@Path("/pet")
@Produces(MediaType.APPLICATION_JSON)
public class PetResource {
private final static Logger LOG = Logger.getLogger(PeopleResource.class.getName());
private final PetDAO dao;
public PetResource() {
this(new PetDAO());
}
public PetResource(PetDAO dao) {
this.dao = dao;
}
@GET
@Path("/{idPet}")
public Response get(
@PathParam("idPet") int idPet
) {
try {
final Pet pet = this.dao.get(idPet);
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();
}
}
@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();
}
}
@POST
public Response add(
@FormParam("name") String name,
@FormParam("idMaster") int idMaster
) {
try {
final Pet newPet = this.dao.add(name, idMaster);
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 idPet,
@FormParam("name") String name,
@FormParam("idMaster") int idMaster
) {
try {
final Pet modifiedPet = new Pet(idPet, name, idMaster);
this.dao.modify(modifiedPet);
return Response.ok(modifiedPet).build();
} catch (NullPointerException npe) {
final String message = String.format("Invalid data for pet (name: %s, idMaster: %s)", name, idMaster);
LOG.log(Level.FINE, message);
return Response.status(Response.Status.BAD_REQUEST)
.entity(message)
.build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid idPet 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") int idPet
) {
try {
this.dao.delete(idPet);
return Response.ok(idPet).build();
} catch (IllegalArgumentException iae) {
LOG.log(Level.FINE, "Invalid idPet 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