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;
import es.uvigo.esei.daa.entities.Person;
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 {
try (final Connection conn = this.getConnection()) {
try (Statement statement = conn.createStatement()) {
......
......@@ -5,6 +5,9 @@ public class Person {
private String name;
private String surname;
public Person() {
}
public Person(int id, String name, String surname) {
this.id = id;
this.name = name;
......
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.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.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
......@@ -20,7 +24,6 @@ public class People {
}
@GET
@Path("/list")
public Response list() {
try {
return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build();
......@@ -29,11 +32,24 @@ public class People {
return Response.serverError().entity(e.getMessage()).build();
}
}
@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(
@QueryParam("id") int id
@PathParam("id") int id
) {
try {
this.dao.delete(id);
......@@ -45,12 +61,12 @@ public class People {
}
}
@GET
@Path("/modify")
@PUT
@Path("/{id}")
public Response modify(
@QueryParam("id") int id,
@QueryParam("name") String name,
@QueryParam("surname") String surname
@PathParam("id") int id,
@FormParam("name") String name,
@FormParam("surname") String surname
) {
try {
return Response.ok(this.dao.modify(id, name, surname)).build();
......@@ -60,11 +76,10 @@ public class People {
}
}
@GET
@Path("/add")
@POST
public Response add(
@QueryParam("name") String name,
@QueryParam("surname") String surname
@FormParam("name") String name,
@FormParam("surname") String surname
) {
try {
return Response.ok(this.dao.add(name, surname)).build();
......
<?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">
<display-name>DAAExampleTMP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DAA Example DB Connection</description>
<res-ref-name>jdbc/daaexample</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<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">
<display-name>DAAExampleTMP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DAA Example DB Connection</description>
<res-ref-name>jdbc/daaexample</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<init-param>
<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>
\ No newline at end of file
......@@ -3,10 +3,13 @@ function listPeople(done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/list')
.done(done)
.fail(fail)
.always(always);
$.ajax({
url: 'rest/people',
type: 'GET'
})
.done(done)
.fail(fail)
.always(always);
}
function addPerson(person, done, fail, always) {
......@@ -14,10 +17,14 @@ function addPerson(person, done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/add', person)
.done(done)
.fail(fail)
.always(always);
$.ajax({
url: 'rest/people',
type: 'POST',
data: person
})
.done(done)
.fail(fail)
.always(always);
}
function modifyPerson(person, done, fail, always) {
......@@ -25,10 +32,14 @@ function modifyPerson(person, done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/modify', person)
.done(done)
.fail(fail)
.always(always);
$.ajax({
url: 'rest/people/' + person.id,
type: 'PUT',
data: person
})
.done(done)
.fail(fail)
.always(always);
}
function deletePerson(id, done, fail, always) {
......@@ -36,8 +47,11 @@ function deletePerson(id, done, fail, always) {
fail = typeof fail !== 'undefined' ? fail : function() {};
always = typeof always !== 'undefined' ? always : function() {};
$.getJSON('rest/people/delete', { 'id': id })
.done(done)
.fail(fail)
.always(always);
$.ajax({
url: 'rest/people/' + id,
type: 'DELETE',
})
.done(done)
.fail(fail)
.always(always);
}
\ No newline at end of file
......@@ -79,21 +79,22 @@ function enableForm() {
$(peopleFormQuery + ' input').prop('disabled', false);
}
function showErrorMessage(jqxhr, textStatus, error) {
alert(textStatus + ": " + error);
}
function resetForm() {
$(peopleFormQuery)[0].reset();
$(peopleFormQuery + ' input[name="id"]').val('');
$('#btnSubmit').val('Crear');
}
function showErrorMessage(jqxhr, textStatus, error) {
alert(textStatus + ": " + error);
}
function addRowListeners(person) {
$('#person-' + person.id + ' a.edit').click(function() {
personToForm(rowToPerson(person.id));
$('input#btnSubmit').val('Modificar');
});
$('#person-' + person.id + ' a.delete').click(function() {
if (confirm('Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?')) {
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