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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Manuel Barciela Martín
DAAExample
Commits
f3514d5a
Commit
f3514d5a
authored
Mar 15, 2018
by
Manuel Barciela Martín
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add end point to list all pets from one owner
parent
aadbd925
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
400 additions
and
47 deletions
+400
-47
mysql-with-inserts.sql
db/mysql-with-inserts.sql
+7
-6
mysql.sql
db/mysql.sql
+2
-1
PeopleDAO.java
src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java
+6
-0
PetsDAO.java
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
+50
-29
Pet.java
src/main/java/es/uvigo/esei/daa/entities/Pet.java
+15
-7
PeopleResource.java
src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java
+19
-0
PetResource.java
src/main/java/es/uvigo/esei/daa/rest/PetResource.java
+53
-1
Pet.js
src/main/webapp/js/dao/Pet.js
+47
-0
pet.js
src/main/webapp/js/view/pet.js
+193
-0
PetUnitTest.java
src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java
+3
-3
PetResourceTest.java
src/test/java/es/uvigo/esei/daa/rest/PetResourceTest.java
+5
-0
No files found.
db/mysql-with-inserts.sql
View file @
f3514d5a
DROP
DATABASE
IF
EXISTS
`daaexample`
;
CREATE
DATABASE
`daaexample`
;
CREATE
DATABASE
`daaexample`
;
CREATE
TABLE
`daaexample`
.
`people`
(
CREATE
TABLE
`daaexample`
.
`people`
(
...
@@ -18,7 +19,7 @@ CREATE TABLE `daaexample`.`pets` (
...
@@ -18,7 +19,7 @@ CREATE TABLE `daaexample`.`pets` (
`name`
varchar
(
50
)
NOT
NULL
,
`name`
varchar
(
50
)
NOT
NULL
,
`idOwner`
int
NOT
NULL
,
`idOwner`
int
NOT
NULL
,
PRIMARY
KEY
(
`idPet`
),
PRIMARY
KEY
(
`idPet`
),
FOREI
NG
KEY
(
`idOwner`
)
REFERENCES
people
(
id
),
FOREI
GN
KEY
(
`idOwner`
)
REFERENCES
people
(
id
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
GRANT
ALL
ON
`daaexample`
.
*
TO
'daa'
@
'localhost'
IDENTIFIED
BY
'daa'
;
GRANT
ALL
ON
`daaexample`
.
*
TO
'daa'
@
'localhost'
IDENTIFIED
BY
'daa'
;
...
@@ -35,11 +36,11 @@ INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Alba','Fern
...
@@ -35,11 +36,11 @@ INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Alba','Fern
INSERT
INTO
`daaexample`
.
`people`
(
`id`
,
`name`
,
`surname`
)
VALUES
(
0
,
'Asunción'
,
'Jiménez'
);
INSERT
INTO
`daaexample`
.
`people`
(
`id`
,
`name`
,
`surname`
)
VALUES
(
0
,
'Asunción'
,
'Jiménez'
);
-- INSERTS IN PETS --
-- INSERTS IN PETS --
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id
`
)
VALUES
(
0
,
'Perro'
,
0
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id
Owner`
)
VALUES
(
0
,
'Perro'
,
4
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id`
)
VALUES
(
0
,
'Gato'
,
1
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id
Owner
`
)
VALUES
(
0
,
'Gato'
,
1
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id`
)
VALUES
(
0
,
'Loro'
,
2
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id
Owner
`
)
VALUES
(
0
,
'Loro'
,
2
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id`
)
VALUES
(
0
,
'Serpiente'
,
3
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id
Owner
`
)
VALUES
(
0
,
'Serpiente'
,
3
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id
`
)
VALUES
(
0
,
'Rata'
,
0
);
INSERT
INTO
`daaexample`
.
`pets`
(
`idPet`
,
`name`
,
`id
Owner`
)
VALUES
(
0
,
'Rata'
,
1
);
-- The password for each user is its login suffixed with "pass". For example, user "admin" has the password "adminpass".
-- The password for each user is its login suffixed with "pass". For example, user "admin" has the password "adminpass".
...
...
db/mysql.sql
View file @
f3514d5a
DROP
DATABASE
IF
EXISTS
`daaexample`
;
CREATE
DATABASE
`daaexample`
;
CREATE
DATABASE
`daaexample`
;
CREATE
TABLE
`daaexample`
.
`people`
(
CREATE
TABLE
`daaexample`
.
`people`
(
...
@@ -18,7 +19,7 @@ CREATE TABLE `daaexample`.`pets` (
...
@@ -18,7 +19,7 @@ CREATE TABLE `daaexample`.`pets` (
`name`
varchar
(
50
)
NOT
NULL
,
`name`
varchar
(
50
)
NOT
NULL
,
`idOwner`
int
NOT
NULL
,
`idOwner`
int
NOT
NULL
,
PRIMARY
KEY
(
`idPet`
),
PRIMARY
KEY
(
`idPet`
),
FOREI
NG
KEY
(
`idOwner`
)
REFERENCES
people
(
id
),
FOREI
GN
KEY
(
`idOwner`
)
REFERENCES
`daaexample`
.
`people`
(
id
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
GRANT
ALL
ON
`daaexample`
.
*
TO
'daa'
@
'localhost'
IDENTIFIED
BY
'daa'
;
GRANT
ALL
ON
`daaexample`
.
*
TO
'daa'
@
'localhost'
IDENTIFIED
BY
'daa'
;
src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java
View file @
f3514d5a
...
@@ -188,4 +188,10 @@ public class PeopleDAO extends DAO {
...
@@ -188,4 +188,10 @@ public class PeopleDAO extends DAO {
row
.
getString
(
"surname"
)
row
.
getString
(
"surname"
)
);
);
}
}
}
}
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
View file @
f3514d5a
...
@@ -17,13 +17,13 @@ public class PetsDAO extends DAO{
...
@@ -17,13 +17,13 @@ public class PetsDAO extends DAO{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetsDAO
.
class
.
getName
());
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetsDAO
.
class
.
getName
());
public
Pet
get
(
int
id
)
public
Pet
get
(
int
id
Pet
)
throws
DAOException
,
IllegalArgumentException
{
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pets WHERE idPet=?"
;
final
String
query
=
"SELECT * FROM pets WHERE idPet=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
statement
.
setInt
(
1
,
id
Pet
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
if
(
result
.
next
())
{
if
(
result
.
next
())
{
...
@@ -57,30 +57,30 @@ public class PetsDAO extends DAO{
...
@@ -57,30 +57,30 @@ public class PetsDAO extends DAO{
}
}
}
}
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pe
ople
"
,
e
);
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pe
t
"
,
e
);
throw
new
DAOException
(
e
);
throw
new
DAOException
(
e
);
}
}
}
}
public
Pe
rson
add
(
String
name
,
String
surname
)
public
Pe
t
add
(
String
name
,
int
idOwner
)
throws
DAOException
,
IllegalArgumentException
{
throws
DAOException
,
IllegalArgumentException
{
if
(
name
==
null
||
surname
==
null
)
{
if
(
name
==
null
)
{
throw
new
IllegalArgumentException
(
"name
and surname
can't be null"
);
throw
new
IllegalArgumentException
(
"name can't be null"
);
}
}
try
(
Connection
conn
=
this
.
getConnection
())
{
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"INSERT INTO pe
ople
VALUES(null, ?, ?)"
;
final
String
query
=
"INSERT INTO pe
ts
VALUES(null, ?, ?)"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
,
Statement
.
RETURN_GENERATED_KEYS
))
{
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
,
Statement
.
RETURN_GENERATED_KEYS
))
{
statement
.
setString
(
1
,
name
);
statement
.
setString
(
1
,
name
);
statement
.
set
String
(
2
,
surname
);
statement
.
set
Int
(
2
,
idOwner
);
if
(
statement
.
executeUpdate
()
==
1
)
{
if
(
statement
.
executeUpdate
()
==
1
)
{
try
(
ResultSet
resultKeys
=
statement
.
getGeneratedKeys
())
{
try
(
ResultSet
resultKeys
=
statement
.
getGeneratedKeys
())
{
if
(
resultKeys
.
next
())
{
if
(
resultKeys
.
next
())
{
return
new
Pe
rson
(
resultKeys
.
getInt
(
1
),
name
,
surname
);
return
new
Pe
t
(
resultKeys
.
getInt
(
1
),
name
,
idOwner
);
}
else
{
}
else
{
LOG
.
log
(
Level
.
SEVERE
,
"Error retrieving inserted id"
);
LOG
.
log
(
Level
.
SEVERE
,
"Error retrieving inserted id"
);
throw
new
SQLException
(
"Error retrieving inserted id"
);
throw
new
SQLException
(
"Error retrieving inserted id"
);
...
@@ -92,32 +92,32 @@ public class PetsDAO extends DAO{
...
@@ -92,32 +92,32 @@ public class PetsDAO extends DAO{
}
}
}
}
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pe
rson
"
,
e
);
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pe
t
"
,
e
);
throw
new
DAOException
(
e
);
throw
new
DAOException
(
e
);
}
}
}
}
public
void
modify
(
Pe
rson
person
)
public
void
modify
(
Pe
t
pet
)
throws
DAOException
,
IllegalArgumentException
{
throws
DAOException
,
IllegalArgumentException
{
if
(
pe
rson
==
null
)
{
if
(
pe
t
==
null
)
{
throw
new
IllegalArgumentException
(
"person can't be null"
);
throw
new
IllegalArgumentException
(
"person can't be null"
);
}
}
try
(
Connection
conn
=
this
.
getConnection
())
{
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"UPDATE pe
ople SET name=?, surname=? WHERE id
=?"
;
final
String
query
=
"UPDATE pe
ts SET name=?, idOwner=? WHERE idPet
=?"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setString
(
1
,
pe
rson
.
getName
());
statement
.
setString
(
1
,
pe
t
.
getName
());
statement
.
set
String
(
2
,
person
.
getSurname
());
statement
.
set
Int
(
2
,
pet
.
getIdOwner
());
statement
.
setInt
(
3
,
pe
rson
.
getId
());
statement
.
setInt
(
3
,
pe
t
.
getIdPet
());
if
(
statement
.
executeUpdate
()
!=
1
)
{
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"name
and surname
can't be null"
);
throw
new
IllegalArgumentException
(
"name can't be null"
);
}
}
}
}
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pe
rson
"
,
e
);
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pe
t
"
,
e
);
throw
new
DAOException
();
throw
new
DAOException
();
}
}
}
}
...
@@ -127,7 +127,7 @@ public class PetsDAO extends DAO{
...
@@ -127,7 +127,7 @@ public class PetsDAO extends DAO{
public
void
delete
(
int
id
)
public
void
delete
(
int
id
)
throws
DAOException
,
IllegalArgumentException
{
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"DELETE FROM pe
ople WHERE id
=?"
;
final
String
query
=
"DELETE FROM pe
ts WHERE idPet
=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
statement
.
setInt
(
1
,
id
);
...
@@ -137,28 +137,49 @@ public class PetsDAO extends DAO{
...
@@ -137,28 +137,49 @@ public class PetsDAO extends DAO{
}
}
}
}
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pe
rson
"
,
e
);
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pe
t
"
,
e
);
throw
new
DAOException
(
e
);
throw
new
DAOException
(
e
);
}
}
}
}
private
Pet
rowToEntity
(
ResultSet
row
)
throws
SQLException
{
private
Pet
rowToEntity
(
ResultSet
row
)
throws
SQLException
{
return
new
Pet
(
return
new
Pet
(
row
.
getInt
(
"id"
),
row
.
getInt
(
"id
Pet
"
),
row
.
getString
(
"name"
),
row
.
getString
(
"name"
),
row
.
getInt
(
"idOwner"
)
row
.
getInt
(
"idOwner"
)
);
);
}
}
public
List
<
Pet
>
listPetsOwner
(
int
idOwner
)
throws
DAOException
{
try
(
final
Connection
connection
=
this
.
getConnection
()){
final
String
query
=
"SELECT * FROM pets WHERE idOwner=?"
;
try
(
final
PreparedStatement
statement
=
connection
.
prepareStatement
(
query
)){
statement
.
setInt
(
1
,
idOwner
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
()){
final
List
<
Pet
>
petsOwner
=
new
LinkedList
<>();
while
(
result
.
next
())
{
petsOwner
.
add
(
rowToEntity
(
result
));
}
return
petsOwner
;
}
}
}
catch
(
SQLException
exc
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Fallo al listar mascotas"
,
exc
);
throw
new
DAOException
(
exc
);
}
}
}
}
src/main/java/es/uvigo/esei/daa/entities/Pet.java
View file @
f3514d5a
...
@@ -3,21 +3,21 @@ package es.uvigo.esei.daa.entities;
...
@@ -3,21 +3,21 @@ package es.uvigo.esei.daa.entities;
import
static
java
.
util
.
Objects
.
requireNonNull
;
import
static
java
.
util
.
Objects
.
requireNonNull
;
public
class
Pet
{
public
class
Pet
{
private
int
id
;
private
int
id
Pet
;
private
String
name
;
private
String
name
;
private
int
idOwner
;
private
int
idOwner
;
Pet
()
{}
Pet
()
{}
public
Pet
(
int
id
,
String
name
,
int
idOwner
)
{
public
Pet
(
int
id
Pet
,
String
name
,
int
idOwner
)
{
this
.
id
=
id
;
this
.
id
Pet
=
idPet
;
this
.
setName
(
name
);
this
.
setName
(
name
);
this
.
setOwner
(
idOwner
);
this
.
setOwner
(
idOwner
);
}
}
public
int
getId
()
{
public
int
getId
Pet
()
{
return
id
;
return
id
Pet
;
}
}
public
String
getName
()
{
public
String
getName
()
{
...
@@ -29,13 +29,21 @@ public class Pet {
...
@@ -29,13 +29,21 @@ public class Pet {
}
}
public
void
setName
(
String
name
)
{
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
this
.
name
=
requireNonNull
(
name
,
"Name can't be null"
)
;
}
}
public
void
setOwner
(
int
idOwner
)
{
public
void
setOwner
(
int
idOwner
)
{
this
.
idOwner
=
idOwner
;
this
.
idOwner
=
idOwner
;
}
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
idPet
;
return
result
;
}
@Override
@Override
public
boolean
equals
(
Object
obj
)
{
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
if
(
this
==
obj
)
...
@@ -45,7 +53,7 @@ public class Pet {
...
@@ -45,7 +53,7 @@ public class Pet {
if
(!(
obj
instanceof
Pet
))
if
(!(
obj
instanceof
Pet
))
return
false
;
return
false
;
Pet
other
=
(
Pet
)
obj
;
Pet
other
=
(
Pet
)
obj
;
if
(
id
!=
other
.
id
)
if
(
id
Pet
!=
other
.
idPet
)
return
false
;
return
false
;
return
true
;
return
true
;
}
}
...
...
src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java
View file @
f3514d5a
...
@@ -16,6 +16,7 @@ import javax.ws.rs.core.Response;
...
@@ -16,6 +16,7 @@ import javax.ws.rs.core.Response;
import
es.uvigo.esei.daa.dao.DAOException
;
import
es.uvigo.esei.daa.dao.DAOException
;
import
es.uvigo.esei.daa.dao.PeopleDAO
;
import
es.uvigo.esei.daa.dao.PeopleDAO
;
import
es.uvigo.esei.daa.dao.PetsDAO
;
import
es.uvigo.esei.daa.entities.Person
;
import
es.uvigo.esei.daa.entities.Person
;
/**
/**
...
@@ -75,6 +76,24 @@ public class PeopleResource {
...
@@ -75,6 +76,24 @@ public class PeopleResource {
.
build
();
.
build
();
}
}
}
}
@GET
@Path
(
"/{id}/pets"
)
public
Response
getListPets
(
@PathParam
(
"id"
)
int
idOwner
)
{
try
{
return
Response
.
ok
((
new
PetsDAO
()).
listPetsOwner
(
idOwner
)).
build
();
}
catch
(
DAOException
exc
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error al mostrar las mascotas de la persona"
,
exc
);
return
Response
.
serverError
().
entity
(
exc
.
getMessage
()).
build
();
}
}
/**
/**
* Returns the complete list of people stored in the system.
* Returns the complete list of people stored in the system.
...
...
src/main/java/es/uvigo/esei/daa/rest/PetResource.java
View file @
f3514d5a
package
es
.
uvigo
.
esei
.
daa
.
rest
;
package
es
.
uvigo
.
esei
.
daa
.
rest
;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
javax.ws.rs.FormParam
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.Produces
;
...
@@ -10,17 +13,31 @@ import javax.ws.rs.core.MediaType;
...
@@ -10,17 +13,31 @@ import javax.ws.rs.core.MediaType;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response
;
import
es.uvigo.esei.daa.dao.DAOException
;
import
es.uvigo.esei.daa.dao.DAOException
;
import
es.uvigo.esei.daa.dao.PeopleDAO
;
import
es.uvigo.esei.daa.dao.PetsDAO
;
import
es.uvigo.esei.daa.entities.Person
;
import
es.uvigo.esei.daa.entities.Pet
;
import
es.uvigo.esei.daa.entities.Pet
;
@Path
(
"/pets"
)
@Path
(
"/pets"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
PetResource
{
public
class
PetResource
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetResource
.
class
.
getName
());
private
final
PetsDAO
dao
;
public
PetResource
()
{
this
(
new
PetsDAO
());
}
PetResource
(
PetsDAO
dao
)
{
this
.
dao
=
dao
;
}
@GET
@GET
@Path
(
"/{idPet}"
)
@Path
(
"/{idPet}"
)
public
Response
get
(
@PathParam
(
"idPet"
)
int
idPet
)
{
public
Response
get
(
@PathParam
(
"idPet"
)
int
idPet
)
{
try
{
try
{
final
Pet
pet
=
this
.
dao
.
get
(
id
);
final
Pet
pet
=
this
.
dao
.
get
(
id
Pet
);
return
Response
.
ok
(
pet
).
build
();
return
Response
.
ok
(
pet
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
}
catch
(
IllegalArgumentException
iae
)
{
...
@@ -37,5 +54,40 @@ public class PetResource {
...
@@ -37,5 +54,40 @@ public class PetResource {
.
build
();
.
build
();
}
}
}
}
@GET
public
Response
list
()
{
try
{
return
Response
.
ok
(
this
.
dao
.
list
()).
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pet"
,
e
);
return
Response
.
serverError
().
entity
(
e
.
getMessage
()).
build
();
}
}
@POST
public
Response
add
(
@FormParam
(
"name"
)
String
name
,
@FormParam
(
"idOwner"
)
int
idOwner
)
{
try
{
final
Pet
newPet
=
this
.
dao
.
add
(
name
,
idOwner
);
return
Response
.
ok
(
newPet
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid pet id in add method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pet"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
}
}
src/main/webapp/js/dao/Pet.js
0 → 100644
View file @
f3514d5a
var
PetsDAO
=
(
function
()
{
var
resourcePath
=
"rest/pets/"
;
var
requestByAjax
=
function
(
data
,
done
,
fail
,
always
)
{
done
=
typeof
done
!==
'undefined'
?
done
:
function
()
{};
fail
=
typeof
fail
!==
'undefined'
?
fail
:
function
()
{};
always
=
typeof
always
!==
'undefined'
?
always
:
function
()
{};
$
.
ajax
(
data
)
.
done
(
done
)
.
fail
(
fail
)
.
always
(
always
);
};
function
PetsDAO
()
{
this
.
listPet
=
function
(
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
,
type
:
'GET'
},
done
,
fail
,
always
);
};
this
.
addPet
=
function
(
pet
,
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
,
type
:
'POST'
,
data
:
pet
},
done
,
fail
,
always
);
};
this
.
modifyPet
=
function
(
pet
,
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
+
pet
.
idPet
,
type
:
'PUT'
,
data
:
pet
},
done
,
fail
,
always
);
};
this
.
deletePet
=
function
(
idPet
,
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
+
idPet
,
type
:
'DELETE'
,
},
done
,
fail
,
always
);
};
}
return
PetsDAO
;
})();
\ No newline at end of file
src/main/webapp/js/view/pet.js
0 → 100644
View file @
f3514d5a
var
PetView
=
(
function
()
{
var
dao
;
// Referencia a this que permite acceder a las funciones públicas desde las funciones de jQuery.
var
self
;
var
formId
=
'pet-form'
;
var
listId
=
'pet-list'
;
var
formQuery
=
'#'
+
formId
;
var
listQuery
=
'#'
+
listId
;
function
PetView
(
petsDao
,
formContainerId
,
listContainerId
)
{
dao
=
petsDao
;
self
=
this
;
insertPetForm
(
$
(
'#'
+
formContainerId
));
insertPetList
(
$
(
'#'
+
listContainerId
));
this
.
init
=
function
()
{
dao
.
listPet
(
function
(
pet
)
{
$
.
each
(
pet
,
function
(
key
,
pet
)
{
appendToTable
(
pet
);
});
});
// La acción por defecto de enviar formulario (submit) se sobreescribe
// para que el envío sea a través de AJAX
$
(
formQuery
).
submit
(
function
(
event
)
{
var
pet
=
self
.
getPetInForm
();
if
(
self
.
isEditing
())
{
dao
.
modifyPet
(
pet
,
function
(
pet
)
{
$
(
'#pet-'
+
pet
.
idPet
+
' td.name'
).
text
(
pet
.
name
);
$
(
'#pet-'
+
pet
.
idPet
+
' td.idOwner'
).
text
(
pet
.
idOwner
);
self
.
resetForm
();
},
showErrorMessage
,
self
.
enableForm
);
}
else
{
dao
.
addPet
(
pet
,
function
(
pet
)
{
appendToTable
(
pet
);
self
.
resetForm
();
},
showErrorMessage
,
self
.
enableForm
);
}
return
false
;
});
$
(
'#btnClear'
).
click
(
this
.
resetForm
);
};
this
.
getPetInForm
=
function
()
{
var
form
=
$
(
formQuery
);
return
{
'idPet'
:
form
.
find
(
'input[name="idPet"]'
).
val
(),
'name'
:
form
.
find
(
'input[name="name"]'
).
val
(),
'idOwner'
:
form
.
find
(
'input[name="idOwner"]'
).
val
()
};
};
this
.
getPetInRow
=
function
(
idPet
)
{
var
row
=
$
(
'#pet-'
+
idPet
);
if
(
row
!==
undefined
)
{
return
{
'idPet'
:
idPet
,
'name'
:
row
.
find
(
'td.name'
).
text
(),
'idOwner'
:
row
.
find
(
'td.idOwner'
).
text
()
};
}
else
{
return
undefined
;
}
};
this
.
editPet
=
function
(
idPet
)
{
var
row
=
$
(
'#pet-'
+
idPet
);
if
(
row
!==
undefined
)
{
var
form
=
$
(
formQuery
);
form
.
find
(
'input[name="idPet"]'
).
val
(
idPet
);
form
.
find
(
'input[name="name"]'
).
val
(
row
.
find
(
'td.name'
).
text
());
form
.
find
(
'input[name="idOwner"]'
).
val
(
row
.
find
(
'td.idOwner'
).
text
());
$
(
'input#btnSubmit'
).
val
(
'Modificar'
);
}
};
this
.
deletePet
=
function
(
idPet
)
{
if
(
confirm
(
'Está a punto de eliminar a una mascota. ¿Está seguro de que desea continuar?'
))
{
dao
.
deletePet
(
id
,
function
()
{
$
(
'tr#pet-'
+
idPet
).
remove
();
},
showErrorMessage
);
}
};
this
.
isEditing
=
function
()
{
return
$
(
formQuery
+
' input[name="idPet"]'
).
val
()
!=
""
;
};
this
.
disableForm
=
function
()
{
$
(
formQuery
+
' input'
).
prop
(
'disabled'
,
true
);
};
this
.
enableForm
=
function
()
{
$
(
formQuery
+
' input'
).
prop
(
'disabled'
,
false
);
};
this
.
resetForm
=
function
()
{
$
(
formQuery
)[
0
].
reset
();
$
(
formQuery
+
' input[name="idPet"]'
).
val
(
''
);
$
(
'#btnSubmit'
).
val
(
'Crear'
);
};
};
var
insertPetList
=
function
(
parent
)
{
parent
.
append
(
'<table idPet="'
+
listIdPet
+
'" class="table">
\
<thead>
\
<tr class="row">
\
<th class="col-sm-4">Nombre</th>
\
<th class="col-sm-5">Dueño</th>
\
<th class="col-sm-3"> </th>
\
</tr>
\
</thead>
\
<tbody>
\
</tbody>
\
</table>'
);
};
var
insertPetForm
=
function
(
parent
)
{
parent
.
append
(
'<form idPet="'
+
formIdPet
+
'" class="mb-5 mb-10">
\
<input name="idPet" type="hidden" value=""/>
\
<div class="row">
\
<div class="col-sm-4">
\
<input name="name" type="text" value="" placeholder="Nombre" class="form-control" required/>
\
</div>
\
<div class="col-sm-5">
\
<input name="idOwner" type="text" value="" placeholder="Dueño" class="form-control" required/>
\
</div>
\
<div class="col-sm-3">
\
<input id="btnSubmit" type="submit" value="Crear" class="btn btn-primary" />
\
<input id="btnClear" type="reset" value="Limpiar" class="btn" />
\
</div>
\
</div>
\
</form>'
);
};
var
createPetRow
=
function
(
pet
)
{
return
'<tr id="pet-'
+
pet
.
idPet
+
'" class="row">
\
<td class="name col-sm-4">'
+
pet
.
name
+
'</td>
\
<td class="idOwner col-sm-5">'
+
pet
.
idOwner
+
'</td>
\
<td class="col-sm-3">
\
<a class="edit btn btn-primary" href="#">Editar</a>
\
<a class="delete btn btn-warning" href="#">Eliminar</a>
\
</td>
\
</tr>'
;
};
var
showErrorMessage
=
function
(
jqxhr
,
textStatus
,
error
)
{
alert
(
textStatus
+
": "
+
error
);
};
var
addRowListeners
=
function
(
pet
)
{
$
(
'#pet-'
+
pet
.
idPet
+
' a.edit'
).
click
(
function
()
{
self
.
editPet
(
pet
.
idPet
);
});
$
(
'#pet-'
+
pet
.
idPet
+
' a.delete'
).
click
(
function
()
{
self
.
deletePet
(
pet
.
idPet
);
});
};
var
appendToTable
=
function
(
pet
)
{
$
(
listQuery
+
' > tbody:last'
)
.
append
(
createPetRow
(
pet
));
addRowListeners
(
pet
);
};
return
PetView
;
})();
src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java
View file @
f3514d5a
...
@@ -21,7 +21,7 @@ public class PetUnitTest {
...
@@ -21,7 +21,7 @@ public class PetUnitTest {
final
Pet
pet
=
new
Pet
(
id
,
name
,
idOwner
);
final
Pet
pet
=
new
Pet
(
id
,
name
,
idOwner
);
assertThat
(
pet
.
getId
(),
equalTo
(
id
));
assertThat
(
pet
.
getId
Pet
(),
equalTo
(
id
));
assertThat
(
pet
.
getName
(),
equalTo
(
name
));
assertThat
(
pet
.
getName
(),
equalTo
(
name
));
assertThat
(
pet
.
getIdOwner
(),
equalTo
(
idOwner
));
assertThat
(
pet
.
getIdOwner
(),
equalTo
(
idOwner
));
...
@@ -41,7 +41,7 @@ public class PetUnitTest {
...
@@ -41,7 +41,7 @@ public class PetUnitTest {
final
Pet
pet
=
new
Pet
(
id
,
"gato"
,
idOwner
);
final
Pet
pet
=
new
Pet
(
id
,
"gato"
,
idOwner
);
pet
.
setName
(
"manoplas"
);
pet
.
setName
(
"manoplas"
);
assertThat
(
pet
.
getId
(),
is
(
equalTo
(
id
)));
assertThat
(
pet
.
getId
Pet
(),
is
(
equalTo
(
id
)));
assertThat
(
pet
.
getName
(),
is
(
equalTo
(
"manoplas"
)));
assertThat
(
pet
.
getName
(),
is
(
equalTo
(
"manoplas"
)));
assertThat
(
pet
.
getIdOwner
(),
is
(
equalTo
(
idOwner
)));
assertThat
(
pet
.
getIdOwner
(),
is
(
equalTo
(
idOwner
)));
}
}
...
@@ -61,7 +61,7 @@ public class PetUnitTest {
...
@@ -61,7 +61,7 @@ public class PetUnitTest {
final
Pet
pet
=
new
Pet
(
id
,
name
,
2
);
final
Pet
pet
=
new
Pet
(
id
,
name
,
2
);
pet
.
setOwner
(
4
);
pet
.
setOwner
(
4
);
assertThat
(
pet
.
getId
(),
is
(
equalTo
(
id
)));
assertThat
(
pet
.
getId
Pet
(),
is
(
equalTo
(
id
)));
assertThat
(
pet
.
getName
(),
is
(
equalTo
(
name
)));
assertThat
(
pet
.
getName
(),
is
(
equalTo
(
name
)));
assertThat
(
pet
.
getIdOwner
(),
is
(
equalTo
(
4
)));
assertThat
(
pet
.
getIdOwner
(),
is
(
equalTo
(
4
)));
}
}
...
...
src/test/java/es/uvigo/esei/daa/rest/PetResourceTest.java
0 → 100644
View file @
f3514d5a
package
es
.
uvigo
.
esei
.
daa
.
rest
;
public
class
PetResourceTest
{
}
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