package com.phonoforce.tramory.rest.resources;
import com.phonoforce.tramory.Server;
import com.phonoforce.tramory.entities.IEntity;
import javax.ws.rs.*;
public class Resource<T extends IEntity> {
private Server server;
private Class<T> clazz;
public Resource(Class<T> clazz) {
this.clazz = clazz;
this.server = Server.getInstance();
}
@GET
@Path("/{entity_id}")
@Produces("application/json")
public T get(@PathParam("entity_id") int id) {
return this.server.getEntityById(this.clazz, id);
}
@POST
@Produces("application/json")
@Consumes("application/json")
public T create(T entity) {
this.server.insertEntity(entity);
return entity; // TODO: http status code
}
@PUT
@Path("/{entity_id}")
@Produces("application/json")
@Consumes("application/json")
public T update(@PathParam("entity_id") int id, T entity) {
if (id != entity.getId() && entity.getId() != 0){
System.out.println("ID = " + id);
return null; // TODO (error message for URL ID doesnt match entity id)
}
this.server.insertIntoDB(entity);
return entity; // TODO: http status code
}
@DELETE
@Path("/{entity_id}")
@Produces("application/json")
public T delete(@PathParam("entity_id") int id) {
return this.server.dropEntityById(this.clazz, id);
}
}