You need to sign in or sign up before continuing.
Commit b03b9a0c authored by Iago Gómez Salgado's avatar Iago Gómez Salgado

Entity, DAO and Resources for Pets

parent 02a11b5a
package es.uvigo.esei.daa.dao;
import es.uvigo.esei.daa.entities.Pet;
public class PetDAO extends GenericDAO<Pet> {
}
package es.uvigo.esei.daa.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.validation.constraints.Size;
@Entity
public class Pet implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Size(min = 0, max = 30)
@Column(length = 30, nullable = false)
String name;
@Size(min = 0, max = 30)
@Column(length = 30, nullable = false)
private String kind;
@Size(min = 0, max = 30)
@Column(length = 30, nullable = false)
private String breed;
@ManyToOne
Person owner;
Pet(){}
public Pet(int id, String name, String kind, String breed, Person owner) {
this.setName(name);
this.setKind(kind);
this.setBreed(breed);
this.setOwner(owner);
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (int) (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 javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
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.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.List;
import es.uvigo.esei.daa.dao.PetDAO;
import es.uvigo.esei.daa.entities.Pet;
@Path("/pets")
@Produces(MediaType.APPLICATION_JSON)
public class PetsResource {
@Inject PetDAO petDAO;
@Context
private UriInfo uriInfo;
public PetsResource() {
}
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response searchPets() {
List<Pet> pets = petDAO.buscarTodos();
GenericEntity<List<Pet>> entidadJSON = new GenericEntity<List<Pet>>(pets) {};
return Response.ok(entidadJSON).build();
}
@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("{id:[0­9]+}")
public Response buscarAutor(@PathParam("id") Long id) {
Pet pet = petDAO.buscarPorId(id);
if (pet != null) {
return Response.ok(pet).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
@POST
@Consumes({MediaType.APPLICATION_JSON})
public Response createPet(Pet pet) {
try {
Pet newPet = petDAO.crear(pet);
URI newPetURI = uriInfo.getAbsolutePathBuilder().path(newPet.getId().toString()).build();
return Response.created(newPetURI).build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@PUT
@Consumes({MediaType.APPLICATION_JSON})
@Path("{id:[0­9]+}")
public Response updatePet(Pet pet) {
try {
petDAO.actualizar(pet);
return Response.noContent().build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@DELETE
@Path("{id:[0­9]+}")
public Response deletePet(@PathParam("id") Long id) {
Pet pet = petDAO.buscarPorId(id);
if (pet != null) {
petDAO.eliminar(pet);
return Response.noContent().build();
} else {
return Response.status(Response.Status.NOT_FOUND).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