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
Santiago Gómez Vilar
DAAExample
Commits
d2ccccea
Commit
d2ccccea
authored
Mar 08, 2019
by
Santi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Completes list of pets by owner
parent
2e5babc8
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
655 additions
and
393 deletions
+655
-393
PetsDAO.java
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
+3
-3
PeopleResource.java
src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java
+189
-197
PetsResource.java
src/main/java/es/uvigo/esei/daa/rest/PetsResource.java
+8
-13
people.js
src/main/webapp/js/dao/people.js
+3
-3
pets.js
src/main/webapp/js/dao/pets.js
+54
-0
people.js
src/main/webapp/js/view/people.js
+187
-168
pets.js
src/main/webapp/js/view/pets.js
+201
-0
PeopleDataset.java
src/test/java/es/uvigo/esei/daa/dataset/PeopleDataset.java
+4
-4
PetsResourceUnitTest.java
...est/java/es/uvigo/esei/daa/rest/PetsResourceUnitTest.java
+6
-5
No files found.
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
View file @
d2ccccea
...
...
@@ -57,14 +57,14 @@ public class PetsDAO extends DAO {
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
public
List
<
Pet
>
list
()
throws
DAOException
{
public
List
<
Pet
>
list
(
int
owner
)
throws
DAOException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pet"
;
final
String
query
=
"SELECT * FROM pet
WHERE owner = ?
"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
owner
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
final
List
<
Pet
>
pets
=
new
LinkedList
<>();
...
...
src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java
View file @
d2ccccea
package
es
.
uvigo
.
esei
.
daa
.
rest
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
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.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
es.uvigo.esei.daa.dao.DAOException
;
import
es.uvigo.esei.daa.dao.PeopleDAO
;
import
es.uvigo.esei.daa.entities.Person
;
import
javax.ws.rs.*
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
/**
* REST resource for managing people.
*
*
* @author Miguel Reboiro Jato.
*/
@Path
(
"/people"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
PeopleResource
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PeopleResource
.
class
.
getName
());
private
final
PeopleDAO
dao
;
/**
* Constructs a new instance of {@link PeopleResource}.
*/
public
PeopleResource
()
{
this
(
new
PeopleDAO
());
}
// Needed for testing purposes
PeopleResource
(
PeopleDAO
dao
)
{
this
.
dao
=
dao
;
}
/**
* Returns a person with the provided identifier.
*
* @param id the identifier of the person to retrieve.
* @return a 200 OK response with a person that has the provided identifier.
* If the identifier does not corresponds with any user, a 400 Bad Request
* response with an error message will be returned. If an error happens
* while retrieving the list, a 500 Internal Server Error response with an
* error message will be returned.
*/
@GET
@Path
(
"/{id}"
)
public
Response
get
(
@PathParam
(
"id"
)
int
id
)
{
try
{
final
Person
person
=
this
.
dao
.
get
(
id
);
return
Response
.
ok
(
person
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person id in get method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error getting a person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Returns the complete list of people stored in the system.
*
* @return a 200 OK response with the complete list of people stored in the
* system. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@GET
public
Response
list
()
{
try
{
return
Response
.
ok
(
this
.
dao
.
list
()).
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing people"
,
e
);
return
Response
.
serverError
().
entity
(
e
.
getMessage
()).
build
();
}
}
/**
* Creates a new person in the system.
*
* @param name
the name of the new person.
* @param surname the surname of the new person.
* @return a 200 OK response with a person that has been created. If the
* name or the surname are not provided, a 400 Bad Request response with an
* error message will be returned. If an error happens while retrieving the
* list, a 500 Internal Server Error response with an error message will be
* returned.
*/
@POST
public
Response
add
(
@FormParam
(
"name"
)
String
name
,
@FormParam
(
"surname"
)
String
surname
)
{
try
{
final
Person
newPerson
=
this
.
dao
.
add
(
name
,
surname
);
return
Response
.
ok
(
newPerson
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person 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 person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Modifies the data of a person.
*
* @param id
identifier of the person to modify.
* @param name
the new name of the person.
* @param surname the new surname of the person.
* @return a 200 OK response with a person that has been modified. If the
* identifier does not corresponds with any user or the name or surname are
* not provided, a 400 Bad Request response with an error message will be
* returned. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@PUT
@Path
(
"/{id}"
)
public
Response
modify
(
@PathParam
(
"id"
)
int
id
,
@FormParam
(
"name"
)
String
name
,
@FormParam
(
"surname"
)
String
surname
)
{
try
{
final
Person
modifiedPerson
=
new
Person
(
id
,
name
,
surname
);
this
.
dao
.
modify
(
modifiedPerson
);
return
Response
.
ok
(
modifiedPerson
).
build
();
}
catch
(
NullPointerException
npe
)
{
final
String
message
=
String
.
format
(
"Invalid data for person (name: %s, surname: %s)"
,
name
,
surname
);
LOG
.
log
(
Level
.
FINE
,
message
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
message
)
.
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person id in modify method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Deletes a person from the system.
*
* @param id the identifier of the person to be deleted.
* @return a 200 OK response with the identifier of the person that has
* been deleted. If the identifier does not corresponds with any user, a 400
* Bad Request response with an error message will be returned. If an error
* happens while retrieving the list, a 500 Internal Server Error response
* with an error message will be returned.
*/
@DELETE
@Path
(
"/{id}"
)
public
Response
delete
(
@PathParam
(
"id"
)
int
id
)
{
try
{
this
.
dao
.
delete
(
id
);
return
Response
.
ok
(
id
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person id in delete method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PeopleResource
.
class
.
getName
());
private
final
PeopleDAO
dao
;
/**
* Constructs a new instance of {@link PeopleResource}.
*/
public
PeopleResource
()
{
this
(
new
PeopleDAO
());
}
// Needed for testing purposes
PeopleResource
(
PeopleDAO
dao
)
{
this
.
dao
=
dao
;
}
/**
* Returns a person with the provided identifier.
*
* @param id the identifier of the person to retrieve.
* @return a 200 OK response with a person that has the provided identifier.
* If the identifier does not corresponds with any user, a 400 Bad Request
* response with an error message will be returned. If an error happens
* while retrieving the list, a 500 Internal Server Error response with an
* error message will be returned.
*/
@GET
@Path
(
"/{id}"
)
public
Response
get
(
@PathParam
(
"id"
)
int
id
)
{
try
{
final
Person
person
=
this
.
dao
.
get
(
id
);
return
Response
.
ok
(
person
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person id in get method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error getting a person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Returns the complete list of people stored in the system.
*
* @return a 200 OK response with the complete list of people stored in the
* system. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@GET
public
Response
list
()
{
try
{
return
Response
.
ok
(
this
.
dao
.
list
()).
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing people"
,
e
);
return
Response
.
serverError
().
entity
(
e
.
getMessage
()).
build
();
}
}
/**
* Creates a new person in the system.
*
* @param name
the name of the new person.
* @param surname the surname of the new person.
* @return a 200 OK response with a person that has been created. If the
* name or the surname are not provided, a 400 Bad Request response with an
* error message will be returned. If an error happens while retrieving the
* list, a 500 Internal Server Error response with an error message will be
* returned.
*/
@POST
public
Response
add
(
@FormParam
(
"name"
)
String
name
,
@FormParam
(
"surname"
)
String
surname
)
{
try
{
final
Person
newPerson
=
this
.
dao
.
add
(
name
,
surname
);
return
Response
.
ok
(
newPerson
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person 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 person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Modifies the data of a person.
*
* @param id
identifier of the person to modify.
* @param name
the new name of the person.
* @param surname the new surname of the person.
* @return a 200 OK response with a person that has been modified. If the
* identifier does not corresponds with any user or the name or surname are
* not provided, a 400 Bad Request response with an error message will be
* returned. If an error happens while retrieving the list, a 500 Internal
* Server Error response with an error message will be returned.
*/
@PUT
@Path
(
"/{id}"
)
public
Response
modify
(
@PathParam
(
"id"
)
int
id
,
@FormParam
(
"name"
)
String
name
,
@FormParam
(
"surname"
)
String
surname
)
{
try
{
final
Person
modifiedPerson
=
new
Person
(
id
,
name
,
surname
);
this
.
dao
.
modify
(
modifiedPerson
);
return
Response
.
ok
(
modifiedPerson
).
build
();
}
catch
(
NullPointerException
npe
)
{
final
String
message
=
String
.
format
(
"Invalid data for person (name: %s, surname: %s)"
,
name
,
surname
);
LOG
.
log
(
Level
.
FINE
,
message
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
message
)
.
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person id in modify method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
/**
* Deletes a person from the system.
*
* @param id the identifier of the person to be deleted.
* @return a 200 OK response with the identifier of the person that has
* been deleted. If the identifier does not corresponds with any user, a 400
* Bad Request response with an error message will be returned. If an error
* happens while retrieving the list, a 500 Internal Server Error response
* with an error message will be returned.
*/
@DELETE
@Path
(
"/{id}"
)
public
Response
delete
(
@PathParam
(
"id"
)
int
id
)
{
try
{
this
.
dao
.
delete
(
id
);
return
Response
.
ok
(
id
).
build
();
}
catch
(
IllegalArgumentException
iae
)
{
LOG
.
log
(
Level
.
FINE
,
"Invalid person id in delete method"
,
iae
);
return
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
iae
.
getMessage
())
.
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a person"
,
e
);
return
Response
.
serverError
()
.
entity
(
e
.
getMessage
())
.
build
();
}
}
}
src/main/java/es/uvigo/esei/daa/rest/PetsResource.java
View file @
d2ccccea
...
...
@@ -22,21 +22,15 @@ public class PetsResource {
private
final
PetsDAO
dao
;
public
PetsResource
()
{
this
(
new
PetsDAO
());
}
PetsResource
(
PetsDAO
dao
)
{
this
.
dao
=
dao
;
}
/**
* Returns a person with the provided identifier.
*
* @param id the identifier of the person to retrieve.
* @return a 200 OK response with a person that has the provided identifier.
* If the identifier does not corresponds with any user, a 400 Bad Request
* response with an error message will be returned. If an error happens
* while retrieving the list, a 500 Internal Server Error response with an
* error message will be returned.
*/
@GET
@Path
(
"/{id}"
)
public
Response
get
(
...
...
@@ -88,9 +82,10 @@ public class PetsResource {
@GET
public
Response
list
()
{
@Path
(
"/owner/{owner}"
)
public
Response
list
(
@PathParam
(
"owner"
)
int
owner
)
{
try
{
return
Response
.
ok
(
this
.
dao
.
list
()).
build
();
return
Response
.
ok
(
this
.
dao
.
list
(
owner
)).
build
();
}
catch
(
DAOException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pets"
,
e
);
return
Response
.
serverError
().
entity
(
e
.
getMessage
()).
build
();
...
...
src/main/webapp/js/dao/people.js
View file @
d2ccccea
...
...
@@ -5,7 +5,7 @@ var PeopleDAO = (function() {
fail
=
typeof
fail
!==
'undefined'
?
fail
:
function
()
{};
always
=
typeof
always
!==
'undefined'
?
always
:
function
()
{};
let
authToken
=
localStorage
.
getItem
(
'authorization-token'
);
var
authToken
=
localStorage
.
getItem
(
'authorization-token'
);
if
(
authToken
!==
null
)
{
data
.
beforeSend
=
function
(
xhr
)
{
xhr
.
setRequestHeader
(
'Authorization'
,
'Basic '
+
authToken
);
...
...
@@ -23,7 +23,7 @@ var PeopleDAO = (function() {
},
done
,
fail
,
always
);
};
this
.
addPerson
=
function
(
person
,
done
,
fail
,
always
)
{
this
.
addPet
=
function
(
person
,
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
,
type
:
'POST'
,
...
...
@@ -31,7 +31,7 @@ var PeopleDAO = (function() {
},
done
,
fail
,
always
);
};
this
.
modifyPerson
=
function
(
person
,
done
,
fail
,
always
)
{
this
.
modifyPet
=
function
(
person
,
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
+
person
.
id
,
type
:
'PUT'
,
...
...
src/main/webapp/js/dao/pets.js
0 → 100644
View file @
d2ccccea
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
()
{
};
var
authToken
=
localStorage
.
getItem
(
'authorization-token'
);
if
(
authToken
!==
null
)
{
data
.
beforeSend
=
function
(
xhr
)
{
xhr
.
setRequestHeader
(
'Authorization'
,
'Basic '
+
authToken
);
};
}
$
.
ajax
(
data
).
done
(
done
).
fail
(
fail
).
always
(
always
);
};
function
PetsDAO
()
{
this
.
listPets
=
function
(
owner
,
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
+
"owner/"
+
owner
,
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
.
id
,
type
:
'PUT'
,
data
:
pet
},
done
,
fail
,
always
);
};
this
.
deletePet
=
function
(
id
,
done
,
fail
,
always
)
{
requestByAjax
({
url
:
resourcePath
+
id
,
type
:
'DELETE'
,
},
done
,
fail
,
always
);
};
}
return
PetsDAO
;
})();
\ No newline at end of file
src/main/webapp/js/view/people.js
View file @
d2ccccea
var
PeopleView
=
(
function
()
{
var
dao
;
// Referencia a this que permite acceder a las funciones públicas desde las funciones de jQuery.
var
self
;
var
formId
=
'people-form'
;
var
listId
=
'people-list'
;
var
formQuery
=
'#'
+
formId
;
var
listQuery
=
'#'
+
listId
;
function
PeopleView
(
peopleDao
,
formContainerId
,
listContainerId
)
{
dao
=
peopleDao
;
self
=
this
;
insertPeopleForm
(
$
(
'#'
+
formContainerId
));
insertPeopleList
(
$
(
'#'
+
listContainerId
));
this
.
init
=
function
()
{
dao
.
listPeople
(
function
(
people
)
{
$
.
each
(
people
,
function
(
key
,
person
)
{
appendToTable
(
person
);
});
},
function
()
{
alert
(
'No has sido posible acceder al listado de personas.'
);
});
// 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
person
=
self
.
getPersonInForm
();
if
(
self
.
isEditing
())
{
dao
.
modifyPerson
(
person
,
function
(
person
)
{
$
(
'#person-'
+
person
.
id
+
' td.name'
).
text
(
person
.
name
);
$
(
'#person-'
+
person
.
id
+
' td.surname'
).
text
(
person
.
surname
);
self
.
resetForm
();
},
showErrorMessage
,
self
.
enableForm
);
}
else
{
dao
.
addPerson
(
person
,
function
(
person
)
{
appendToTable
(
person
);
self
.
resetForm
();
},
showErrorMessage
,
self
.
enableForm
);
}
return
false
;
});
$
(
'#btnClear'
).
click
(
this
.
resetForm
);
};
this
.
getPersonInForm
=
function
()
{
var
form
=
$
(
formQuery
);
return
{
'id'
:
form
.
find
(
'input[name="id"]'
).
val
(),
'name'
:
form
.
find
(
'input[name="name"]'
).
val
(),
'surname'
:
form
.
find
(
'input[name="surname"]'
).
val
()
};
};
this
.
getPersonInRow
=
function
(
id
)
{
var
row
=
$
(
'#person-'
+
id
);
if
(
row
!==
undefined
)
{
return
{
'id'
:
id
,
'name'
:
row
.
find
(
'td.name'
).
text
(),
'surname'
:
row
.
find
(
'td.surname'
).
text
()
};
}
else
{
return
undefined
;
}
};
this
.
editPerson
=
function
(
id
)
{
var
row
=
$
(
'#person-'
+
id
);
if
(
row
!==
undefined
)
{
var
form
=
$
(
formQuery
);
form
.
find
(
'input[name="id"]'
).
val
(
id
);
form
.
find
(
'input[name="name"]'
).
val
(
row
.
find
(
'td.name'
).
text
());
form
.
find
(
'input[name="surname"]'
).
val
(
row
.
find
(
'td.surname'
).
text
());
$
(
'input#btnSubmit'
).
val
(
'Modificar'
);
}
};
this
.
deletePerson
=
function
(
id
)
{
if
(
confirm
(
'Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?'
))
{
dao
.
deletePerson
(
id
,
function
()
{
$
(
'tr#person-'
+
id
).
remove
();
},
showErrorMessage
);
}
};
this
.
isEditing
=
function
()
{
return
$
(
formQuery
+
' input[name="id"]'
).
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="id"]'
).
val
(
''
);
$
(
'#btnSubmit'
).
val
(
'Crear'
);
};
};
var
insertPeopleList
=
function
(
parent
)
{
parent
.
append
(
'<table id="'
+
listId
+
'" class="table">
\
var
PeopleView
=
(
function
()
{
var
dao
;
// Referencia a this que permite acceder a las funciones públicas desde las funciones de jQuery.
var
self
;
var
formId
=
'people-form'
;
var
listId
=
'people-list'
;
var
formQuery
=
'#'
+
formId
;
var
listQuery
=
'#'
+
listId
;
function
PeopleView
(
peopleDao
,
formContainerId
,
listContainerId
)
{
dao
=
peopleDao
;
self
=
this
;
insertPeopleForm
(
$
(
'#'
+
formContainerId
));
insertPeopleList
(
$
(
'#'
+
listContainerId
));
this
.
init
=
function
()
{
dao
.
listPeople
(
function
(
people
)
{
$
.
each
(
people
,
function
(
key
,
person
)
{
appendToTable
(
person
);
});
},
function
()
{
alert
(
'No has sido posible acceder al listado de personas.'
);
});
// 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
person
=
self
.
getPersonInForm
();
if
(
self
.
isEditing
())
{
dao
.
modifyPet
(
person
,
function
(
person
)
{
$
(
'#person-'
+
person
.
id
+
' td.name'
).
text
(
person
.
name
);
$
(
'#person-'
+
person
.
id
+
' td.surname'
).
text
(
person
.
surname
);
self
.
resetForm
();
},
showErrorMessage
,
self
.
enableForm
);
}
else
{
dao
.
addPet
(
person
,
function
(
person
)
{
appendToTable
(
person
);
self
.
resetForm
();
},
showErrorMessage
,
self
.
enableForm
);
}
return
false
;
});
$
(
'#btnClear'
).
click
(
this
.
resetForm
);
};
this
.
getPersonInForm
=
function
()
{
var
form
=
$
(
formQuery
);
return
{
'id'
:
form
.
find
(
'input[name="id"]'
).
val
(),
'name'
:
form
.
find
(
'input[name="name"]'
).
val
(),
'surname'
:
form
.
find
(
'input[name="surname"]'
).
val
()
};
};
this
.
getPersonInRow
=
function
(
id
)
{
var
row
=
$
(
'#person-'
+
id
);
if
(
row
!==
undefined
)
{
return
{
'id'
:
id
,
'name'
:
row
.
find
(
'td.name'
).
text
(),
'surname'
:
row
.
find
(
'td.surname'
).
text
()
};
}
else
{
return
undefined
;
}
};
this
.
editPerson
=
function
(
id
)
{
var
row
=
$
(
'#person-'
+
id
);
if
(
row
!==
undefined
)
{
var
form
=
$
(
formQuery
);
form
.
find
(
'input[name="id"]'
).
val
(
id
);
form
.
find
(
'input[name="name"]'
).
val
(
row
.
find
(
'td.name'
).
text
());
form
.
find
(
'input[name="surname"]'
).
val
(
row
.
find
(
'td.surname'
).
text
());
$
(
'input#btnSubmit'
).
val
(
'Modificar'
);
}
};
this
.
deletePerson
=
function
(
id
)
{
if
(
confirm
(
'Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?'
))
{
dao
.
deletePet
(
id
,
function
()
{
$
(
'tr#person-'
+
id
).
remove
();
},
showErrorMessage
);
}
};
this
.
showPets
=
function
(
id
)
{
document
.
getElementById
(
'people-container'
).
style
.
display
=
'none'
;
document
.
getElementById
(
'pet-container'
).
style
.
display
=
'block'
;
var
view
=
new
PetsView
(
id
,
new
PetsDAO
(),
'pet-container'
,
'pet-container'
);
view
.
init
();
}
this
.
isEditing
=
function
()
{
return
$
(
formQuery
+
' input[name="id"]'
).
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="id"]'
).
val
(
''
);
$
(
'#btnSubmit'
).
val
(
'Crear'
);
};
};
var
insertPeopleList
=
function
(
parent
)
{
parent
.
append
(
'<table id="'
+
listId
+
'" class="table">
\
<thead>
\
<tr class="row">
\
<th class="col-sm-4">Nombre</th>
\
...
...
@@ -138,12 +150,12 @@ var PeopleView = (function() {
<tbody>
\
</tbody>
\
</table>'
);
};
);
};
var
insertPeopleForm
=
function
(
parent
)
{
parent
.
append
(
'<form id="'
+
formId
+
'" class="mb-5 mb-10">
\
var
insertPeopleForm
=
function
(
parent
)
{
parent
.
append
(
'<form id="'
+
formId
+
'" class="mb-5 mb-10">
\
<input name="id" type="hidden" value=""/>
\
<div class="row">
\
<div class="col-sm-4">
\
...
...
@@ -158,39 +170,46 @@ var PeopleView = (function() {
</div>
\
</div>
\
</form>'
);
};
var
createPersonRow
=
function
(
person
)
{
return
'<tr id="person-'
+
person
.
id
+
'" class="row">
\
<td class="name col-sm-4">'
+
person
.
name
+
'</td>
\
<td class="surname col-sm-5">'
+
person
.
surname
+
'</td>
\
<td class="col-sm-3">
\
<a class="edit btn btn-primary" href="#">Editar</a>
\
<a class="delete btn btn-warning" href="#">Eliminar</a>
\
);
};
var
createPersonRow
=
function
(
person
)
{
return
'<tr id="person-'
+
person
.
id
+
'" class="row">
\
<td class="name col-sm-6 col-md-3">'
+
person
.
name
+
'</td>
\
<td class="surname col-sm-6 col-md-4">'
+
person
.
surname
+
'</td>
\
<td class="col-sm-12 col-md-5">
\
<a class="show-pets btn btn-primary" href="#">Mascotas</a>
\
<a class="edit btn btn-warning" href="#">Editar</a>
\
<a class="delete btn btn-danger" href="#">Eliminar</a>
\
</td>
\
</tr>'
;
};
var
showErrorMessage
=
function
(
jqxhr
,
textStatus
,
error
)
{
alert
(
textStatus
+
": "
+
error
);
};
var
addRowListeners
=
function
(
person
)
{
$
(
'#person-'
+
person
.
id
+
' a.edit'
).
click
(
function
()
{
self
.
editPerson
(
person
.
id
);
});
$
(
'#person-'
+
person
.
id
+
' a.delete'
).
click
(
function
()
{
self
.
deletePerson
(
person
.
id
);
});
};
var
appendToTable
=
function
(
person
)
{
$
(
listQuery
+
' > tbody:last'
)
.
append
(
createPersonRow
(
person
));
addRowListeners
(
person
);
};
return
PeopleView
;
};
var
showErrorMessage
=
function
(
jqxhr
,
textStatus
,
error
)
{
alert
(
textStatus
+
": "
+
error
);
};
var
addRowListeners
=
function
(
person
)
{
$
(
'#person-'
+
person
.
id
+
' a.edit'
).
click
(
function
()
{
self
.
editPerson
(
person
.
id
);
});
$
(
'#person-'
+
person
.
id
+
' a.delete'
).
click
(
function
()
{
self
.
deletePerson
(
person
.
id
);
});
$
(
'#person-'
+
person
.
id
+
' a.show-pets'
).
click
(
function
()
{
self
.
showPets
(
person
.
id
);
});
};
var
appendToTable
=
function
(
person
)
{
$
(
listQuery
+
' > tbody:last'
)
.
append
(
createPersonRow
(
person
));
addRowListeners
(
person
);
};
return
PeopleView
;
})();
src/main/webapp/js/view/pets.js
0 → 100644
View file @
d2ccccea
var
PetsView
=
(
function
()
{
var
dao
;
// Referencia a this que permite acceder a las funciones públicas desde las funciones de jQuery.
var
self
;
var
formId
=
'pets-form'
;
var
listId
=
'pets-list'
;
var
formQuery
=
'#'
+
formId
;
var
listQuery
=
'#'
+
listId
;
function
PetsView
(
owner
,
petsDao
,
formContainerId
,
listContainerId
)
{
dao
=
petsDao
;
self
=
this
;
insertPetsForm
(
$
(
'#'
+
formContainerId
));
insertPetsList
(
$
(
'#'
+
listContainerId
));
this
.
init
=
function
()
{
dao
.
listPets
(
owner
,
function
(
pets
)
{
$
.
each
(
pets
,
function
(
key
,
pet
)
{
appendToTable
(
pet
);
});
},
function
()
{
alert
(
'No has sido posible acceder al listado de mascotas.'
);
});
// 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
.
id
+
' td.name'
).
text
(
pet
.
name
);
$
(
'#pet-'
+
pet
.
id
+
' td.owner'
).
text
(
pet
.
owner
);
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
{
'id'
:
form
.
find
(
'input[name="id"]'
).
val
(),
'name'
:
form
.
find
(
'input[name="name"]'
).
val
(),
'owner'
:
form
.
find
(
'input[name="owner"]'
).
val
()
};
};
this
.
getPetInRow
=
function
(
id
)
{
var
row
=
$
(
'#pet-'
+
id
);
if
(
row
!==
undefined
)
{
return
{
'id'
:
id
,
'name'
:
row
.
find
(
'td.name'
).
text
(),
'owner'
:
row
.
find
(
'td.owner'
).
text
()
};
}
else
{
return
undefined
;
}
};
this
.
editPet
=
function
(
id
)
{
var
row
=
$
(
'#pet-'
+
id
);
if
(
row
!==
undefined
)
{
var
form
=
$
(
formQuery
);
form
.
find
(
'input[name="id"]'
).
val
(
id
);
form
.
find
(
'input[name="name"]'
).
val
(
row
.
find
(
'td.name'
).
text
());
form
.
find
(
'input[name="owner"]'
).
val
(
row
.
find
(
'td.owner'
).
text
());
$
(
'input#btnSubmit'
).
val
(
'Modificar'
);
}
};
this
.
deletePet
=
function
(
id
)
{
if
(
confirm
(
'Está a punto de eliminar a una mascota. ¿Está seguro de que desea continuar?'
))
{
dao
.
deletePet
(
id
,
function
()
{
$
(
'tr#pet-'
+
id
).
remove
();
},
showErrorMessage
);
}
};
this
.
showPets
=
function
(
id
)
{
}
this
.
isEditing
=
function
()
{
return
$
(
formQuery
+
' input[name="id"]'
).
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="id"]'
).
val
(
''
);
$
(
'#btnSubmit'
).
val
(
'Crear'
);
};
};
var
insertPetsList
=
function
(
parent
)
{
parent
.
append
(
'<table id="'
+
listId
+
'" class="table">
\
<thead>
\
<tr class="row">
\
<th class="col-sm-4">Nombre</th>
\
<th class="col-sm-5">Propietario</th>
\
<th class="col-sm-3"> </th>
\
</tr>
\
</thead>
\
<tbody>
\
</tbody>
\
</table>'
);
};
var
insertPetsForm
=
function
(
parent
)
{
parent
.
append
(
'<form id="'
+
formId
+
'" class="mb-5 mb-10">
\
<input name="id" 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="owner" 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
.
id
+
'" class="row">
\
<td class="name col-sm-6 col-md-3">'
+
pet
.
name
+
'</td>
\
<td class="owner col-sm-6 col-md-4">'
+
pet
.
owner
+
'</td>
\
<td class="col-sm-12 col-md-5">
\
<a class="edit btn btn-warning" href="#">Editar</a>
\
<a class="delete btn btn-danger" href="#">Eliminar</a>
\
</td>
\
</tr>'
;
};
var
showErrorMessage
=
function
(
jqxhr
,
textStatus
,
error
)
{
alert
(
textStatus
+
": "
+
error
);
};
var
addRowListeners
=
function
(
pet
)
{
$
(
'#pet-'
+
pet
.
id
+
' a.edit'
).
click
(
function
()
{
self
.
editPet
(
pet
.
id
);
});
$
(
'#pet-'
+
pet
.
id
+
' a.delete'
).
click
(
function
()
{
self
.
deletePet
(
pet
.
id
);
});
};
var
appendToTable
=
function
(
pet
)
{
$
(
listQuery
+
' > tbody:last'
)
.
append
(
createPetRow
(
pet
));
addRowListeners
(
pet
);
};
return
PetsView
;
})();
src/test/java/es/uvigo/esei/daa/dataset/PeopleDataset.java
View file @
d2ccccea
package
es
.
uvigo
.
esei
.
daa
.
dataset
;
import
static
java
.
util
.
Arrays
.
binarySearch
;
import
static
java
.
util
.
Arrays
.
stream
;
import
es.uvigo.esei.daa.entities.Person
;
import
java.util.Arrays
;
import
java.util.function.Predicate
;
import
es.uvigo.esei.daa.entities.Person
;
import
static
java
.
util
.
Arrays
.
binarySearch
;
import
static
java
.
util
.
Arrays
.
stream
;
public
final
class
PeopleDataset
{
private
PeopleDataset
()
{}
...
...
@@ -45,7 +45,7 @@ public final class PeopleDataset {
}
public
static
int
existentId
()
{
return
5
;
return
1
;
}
public
static
int
nonExistentId
()
{
...
...
src/test/java/es/uvigo/esei/daa/rest/PetsResourceUnitTest.java
View file @
d2ccccea
...
...
@@ -11,6 +11,7 @@ import org.junit.Test;
import
javax.ws.rs.core.Response
;
import
java.util.List
;
import
static
es
.
uvigo
.
esei
.
daa
.
dataset
.
PeopleDataset
.
existentPerson
;
import
static
es
.
uvigo
.
esei
.
daa
.
dataset
.
PeopleDataset
.
newName
;
import
static
es
.
uvigo
.
esei
.
daa
.
dataset
.
PetsDataset
.*;
import
static
es
.
uvigo
.
esei
.
daa
.
matchers
.
HasHttpStatus
.*;
...
...
@@ -61,13 +62,13 @@ public class PetsResourceUnitTest {
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
testList
()
throws
Exception
{
final
List
<
Pet
>
pe
ople
=
asList
(
pets
());
final
List
<
Pet
>
pe
ts
=
asList
(
pets
());
expect
(
daoMock
.
list
(
)).
andReturn
(
people
);
expect
(
daoMock
.
list
(
existentPerson
().
getId
())).
andReturn
(
pets
);
replay
(
daoMock
);
final
Response
response
=
resource
.
list
();
final
Response
response
=
resource
.
list
(
existentPet
().
getId
()
);
assertThat
(
response
,
hasOkStatus
());
assertThat
((
List
<
Pet
>)
response
.
getEntity
(),
containsPetsInAnyOrder
(
pets
()));
...
...
@@ -76,11 +77,11 @@ public class PetsResourceUnitTest {
@Test
public
void
testListDAOException
()
throws
Exception
{
expect
(
daoMock
.
list
()).
andThrow
(
new
DAOException
());
expect
(
daoMock
.
list
(
existentPerson
().
getId
()
)).
andThrow
(
new
DAOException
());
replay
(
daoMock
);
final
Response
response
=
resource
.
list
();
final
Response
response
=
resource
.
list
(
existentPerson
().
getId
()
);
assertThat
(
response
,
hasInternalServerErrorStatus
());
}
...
...
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