Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
daaexample
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Miguel Reboiro Jato
daaexample
Commits
7e143670
Commit
7e143670
authored
Feb 10, 2014
by
michada
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RESTful API correction.
RESTful API modified to use PUT, DELETE, POST and GET methods.
parent
7de0b1a9
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
128 additions
and
57 deletions
+128
-57
PeopleDAO.java
src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java
+24
-0
Person.java
src/main/java/es/uvigo/esei/daa/entities/Person.java
+3
-0
People.java
src/main/java/es/uvigo/esei/daa/rest/People.java
+29
-14
web.xml
src/main/webapp/WEB-INF/web.xml
+37
-23
people.js
src/main/webapp/js/dao/people.js
+30
-16
people.js
src/main/webapp/js/view/people.js
+5
-4
No files found.
src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java
View file @
7e143670
...
...
@@ -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
())
{
...
...
src/main/java/es/uvigo/esei/daa/entities/Person.java
View file @
7e143670
...
...
@@ -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
;
...
...
src/main/java/es/uvigo/esei/daa/rest/People.java
View file @
7e143670
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
();
...
...
@@ -31,9 +34,22 @@ public class People {
}
@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
(
@
Query
Param
(
"id"
)
int
id
@
Path
Param
(
"id"
)
int
id
)
{
try
{
this
.
dao
.
delete
(
id
);
...
...
@@ -45,12 +61,12 @@ public class People {
}
}
@
GE
T
@Path
(
"/
modify
"
)
@
PU
T
@Path
(
"/
{id}
"
)
public
Response
modify
(
@
Query
Param
(
"id"
)
int
id
,
@
Query
Param
(
"name"
)
String
name
,
@
Query
Param
(
"surname"
)
String
surname
@
Path
Param
(
"id"
)
int
id
,
@
Form
Param
(
"name"
)
String
name
,
@
Form
Param
(
"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
(
@
Query
Param
(
"name"
)
String
name
,
@
Query
Param
(
"surname"
)
String
surname
@
Form
Param
(
"name"
)
String
name
,
@
Form
Param
(
"surname"
)
String
surname
)
{
try
{
return
Response
.
ok
(
this
.
dao
.
add
(
name
,
surname
)).
build
();
...
...
src/main/webapp/WEB-INF/web.xml
View file @
7e143670
<?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"
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>
...
...
@@ -17,9 +20,20 @@
</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
src/main/webapp/js/dao/people.js
View file @
7e143670
...
...
@@ -3,7 +3,10 @@ function listPeople(done, fail, always) {
fail
=
typeof
fail
!==
'undefined'
?
fail
:
function
()
{};
always
=
typeof
always
!==
'undefined'
?
always
:
function
()
{};
$
.
getJSON
(
'rest/people/list'
)
$
.
ajax
({
url
:
'rest/people'
,
type
:
'GET'
})
.
done
(
done
)
.
fail
(
fail
)
.
always
(
always
);
...
...
@@ -14,7 +17,11 @@ function addPerson(person, done, fail, always) {
fail
=
typeof
fail
!==
'undefined'
?
fail
:
function
()
{};
always
=
typeof
always
!==
'undefined'
?
always
:
function
()
{};
$
.
getJSON
(
'rest/people/add'
,
person
)
$
.
ajax
({
url
:
'rest/people'
,
type
:
'POST'
,
data
:
person
})
.
done
(
done
)
.
fail
(
fail
)
.
always
(
always
);
...
...
@@ -25,7 +32,11 @@ function modifyPerson(person, done, fail, always) {
fail
=
typeof
fail
!==
'undefined'
?
fail
:
function
()
{};
always
=
typeof
always
!==
'undefined'
?
always
:
function
()
{};
$
.
getJSON
(
'rest/people/modify'
,
person
)
$
.
ajax
({
url
:
'rest/people/'
+
person
.
id
,
type
:
'PUT'
,
data
:
person
})
.
done
(
done
)
.
fail
(
fail
)
.
always
(
always
);
...
...
@@ -36,7 +47,10 @@ function deletePerson(id, done, fail, always) {
fail
=
typeof
fail
!==
'undefined'
?
fail
:
function
()
{};
always
=
typeof
always
!==
'undefined'
?
always
:
function
()
{};
$
.
getJSON
(
'rest/people/delete'
,
{
'id'
:
id
})
$
.
ajax
({
url
:
'rest/people/'
+
id
,
type
:
'DELETE'
,
})
.
done
(
done
)
.
fail
(
fail
)
.
always
(
always
);
...
...
src/main/webapp/js/view/people.js
View file @
7e143670
...
...
@@ -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
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment