Commit 7e143670 authored by michada's avatar michada

RESTful API correction.

RESTful API modified to use PUT, DELETE, POST and GET methods.
parent 7de0b1a9
...@@ -11,6 +11,30 @@ import java.util.List; ...@@ -11,6 +11,30 @@ import java.util.List;
import es.uvigo.esei.daa.entities.Person; import es.uvigo.esei.daa.entities.Person;
public class PeopleDAO extends DAO { public class PeopleDAO extends DAO {
public Person get(int id) throws DAOException {
try (final Connection conn = this.getConnection()) {
final String query = "SELECT * FROM people WHERE id=?";
try (PreparedStatement statement = conn.prepareStatement(query)) {
statement.setInt(1, id);
try (ResultSet result = statement.executeQuery()) {
if (result.next()) {
return new Person(
result.getInt("id"),
result.getString("name"),
result.getString("surname")
);
} else {
throw new DAOException("Person not found");
}
}
}
} catch (SQLException e) {
throw new DAOException(e);
}
}
public List<Person> list() throws DAOException { public List<Person> list() throws DAOException {
try (final Connection conn = this.getConnection()) { try (final Connection conn = this.getConnection()) {
try (Statement statement = conn.createStatement()) { try (Statement statement = conn.createStatement()) {
......
...@@ -5,6 +5,9 @@ public class Person { ...@@ -5,6 +5,9 @@ public class Person {
private String name; private String name;
private String surname; private String surname;
public Person() {
}
public Person(int id, String name, String surname) { public Person(int id, String name, String surname) {
this.id = id; this.id = id;
this.name = name; this.name = name;
......
package es.uvigo.esei.daa.rest; package es.uvigo.esei.daa.rest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
...@@ -20,7 +24,6 @@ public class People { ...@@ -20,7 +24,6 @@ public class People {
} }
@GET @GET
@Path("/list")
public Response list() { public Response list() {
try { try {
return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build(); return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build();
...@@ -29,11 +32,24 @@ public class People { ...@@ -29,11 +32,24 @@ public class People {
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
} }
} }
@GET @GET
@Path("/delete") @Path("/{id}")
public Response get(
@PathParam("id") int id
) {
try {
return Response.ok(this.dao.get(id), MediaType.APPLICATION_JSON).build();
} catch (DAOException e) {
e.printStackTrace();
return Response.serverError().entity(e.getMessage()).build();
}
}
@DELETE
@Path("/{id}")
public Response delete( public Response delete(
@QueryParam("id") int id @PathParam("id") int id
) { ) {
try { try {
this.dao.delete(id); this.dao.delete(id);
...@@ -45,12 +61,12 @@ public class People { ...@@ -45,12 +61,12 @@ public class People {
} }
} }
@GET @PUT
@Path("/modify") @Path("/{id}")
public Response modify( public Response modify(
@QueryParam("id") int id, @PathParam("id") int id,
@QueryParam("name") String name, @FormParam("name") String name,
@QueryParam("surname") String surname @FormParam("surname") String surname
) { ) {
try { try {
return Response.ok(this.dao.modify(id, name, surname)).build(); return Response.ok(this.dao.modify(id, name, surname)).build();
...@@ -60,11 +76,10 @@ public class People { ...@@ -60,11 +76,10 @@ public class People {
} }
} }
@GET @POST
@Path("/add")
public Response add( public Response add(
@QueryParam("name") String name, @FormParam("name") String name,
@QueryParam("surname") String surname @FormParam("surname") String surname
) { ) {
try { try {
return Response.ok(this.dao.add(name, surname)).build(); return Response.ok(this.dao.add(name, surname)).build();
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<display-name>DAAExampleTMP</display-name> xmlns="http://java.sun.com/xml/ns/javaee"
<welcome-file-list> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
<welcome-file>index.html</welcome-file> id="WebApp_ID" version="3.0">
<welcome-file>index.htm</welcome-file> <display-name>DAAExampleTMP</display-name>
<welcome-file>index.jsp</welcome-file> <welcome-file-list>
<welcome-file>default.html</welcome-file> <welcome-file>index.html</welcome-file>
<welcome-file>default.htm</welcome-file> <welcome-file>index.htm</welcome-file>
<welcome-file>default.jsp</welcome-file> <welcome-file>index.jsp</welcome-file>
</welcome-file-list> <welcome-file>default.html</welcome-file>
<resource-ref> <welcome-file>default.htm</welcome-file>
<description>DAA Example DB Connection</description> <welcome-file>default.jsp</welcome-file>
<res-ref-name>jdbc/daaexample</res-ref-name> </welcome-file-list>
<res-type>javax.sql.DataSource</res-type> <resource-ref>
<res-auth>Container</res-auth> <description>DAA Example DB Connection</description>
</resource-ref> <res-ref-name>jdbc/daaexample</res-ref-name>
<servlet> <res-type>javax.sql.DataSource</res-type>
<servlet-name>javax.ws.rs.core.Application</servlet-name> <res-auth>Container</res-auth>
</servlet> </resource-ref>
<servlet-mapping> <servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name> <servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern> <init-param>
</servlet-mapping> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- servlet> <servlet-name>webdav</servlet-name> <servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class>
<init-param> <param-name>debug</param-name> <param-value>0</param-value>
</init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value>
</init-param> <init-param> <param-name>readonly</param-name> <param-value>false</param-value>
</init-param> </servlet> <servlet-mapping> <servlet-name>webdav</servlet-name>
<url-pattern>/*</url-pattern> </servlet-mapping -->
</web-app> </web-app>
\ No newline at end of file
...@@ -3,10 +3,13 @@ function listPeople(done, fail, always) { ...@@ -3,10 +3,13 @@ function listPeople(done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {}; fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {}; always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/list') $.ajax({
.done(done) url: 'rest/people',
.fail(fail) type: 'GET'
.always(always); })
.done(done)
.fail(fail)
.always(always);
} }
function addPerson(person, done, fail, always) { function addPerson(person, done, fail, always) {
...@@ -14,10 +17,14 @@ function addPerson(person, done, fail, always) { ...@@ -14,10 +17,14 @@ function addPerson(person, done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {}; fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {}; always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/add', person) $.ajax({
.done(done) url: 'rest/people',
.fail(fail) type: 'POST',
.always(always); data: person
})
.done(done)
.fail(fail)
.always(always);
} }
function modifyPerson(person, done, fail, always) { function modifyPerson(person, done, fail, always) {
...@@ -25,10 +32,14 @@ function modifyPerson(person, done, fail, always) { ...@@ -25,10 +32,14 @@ function modifyPerson(person, done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {}; fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {}; always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/modify', person) $.ajax({
.done(done) url: 'rest/people/' + person.id,
.fail(fail) type: 'PUT',
.always(always); data: person
})
.done(done)
.fail(fail)
.always(always);
} }
function deletePerson(id, done, fail, always) { function deletePerson(id, done, fail, always) {
...@@ -36,8 +47,11 @@ function deletePerson(id, done, fail, always) { ...@@ -36,8 +47,11 @@ function deletePerson(id, done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {}; fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {}; always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/delete', { 'id': id }) $.ajax({
.done(done) url: 'rest/people/' + id,
.fail(fail) type: 'DELETE',
.always(always); })
.done(done)
.fail(fail)
.always(always);
} }
\ No newline at end of file
...@@ -79,21 +79,22 @@ function enableForm() { ...@@ -79,21 +79,22 @@ function enableForm() {
$(peopleFormQuery + ' input').prop('disabled', false); $(peopleFormQuery + ' input').prop('disabled', false);
} }
function showErrorMessage(jqxhr, textStatus, error) {
alert(textStatus + ": " + error);
}
function resetForm() { function resetForm() {
$(peopleFormQuery)[0].reset(); $(peopleFormQuery)[0].reset();
$(peopleFormQuery + ' input[name="id"]').val(''); $(peopleFormQuery + ' input[name="id"]').val('');
$('#btnSubmit').val('Crear'); $('#btnSubmit').val('Crear');
} }
function showErrorMessage(jqxhr, textStatus, error) {
alert(textStatus + ": " + error);
}
function addRowListeners(person) { function addRowListeners(person) {
$('#person-' + person.id + ' a.edit').click(function() { $('#person-' + person.id + ' a.edit').click(function() {
personToForm(rowToPerson(person.id)); personToForm(rowToPerson(person.id));
$('input#btnSubmit').val('Modificar'); $('input#btnSubmit').val('Modificar');
}); });
$('#person-' + person.id + ' a.delete').click(function() { $('#person-' + person.id + ' a.delete').click(function() {
if (confirm('Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?')) { if (confirm('Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?')) {
deletePerson(person.id, deletePerson(person.id,
......
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