Commit ba522393 authored by cyanide4all's avatar cyanide4all

Working as intended, finally

parent 1a64cd50
......@@ -20,30 +20,21 @@ import es.uvigo.esei.daa.entities.Pet;
*/
public class PetDAO extends DAO {
private final static Logger LOG = Logger.getLogger(PetDAO.class.getName());
//TODO está puesto a 1 para testeos
private int ownerID = 1;
public int getOwnerID(){
return ownerID;
}
public void setOwnerID(int ownerID) {this.ownerID=ownerID;}
public PetDAO(int ownerID) {
this.ownerID = ownerID;
}
public PetDAO(){}
/**
* Returns a pet stored persisted in the system.
*
*
* @param ownerID necessary for the rowToEntity method
* @param id identifier of the pet.
* @return a pet with the provided identifier.
* @throws DAOException if an error happens while retrieving the pet.
* @throws IllegalArgumentException if the provided id does not corresponds
* with any persisted pet.
*/
public Pet get(int id)
public Pet get(int ownerID, int id)
throws DAOException, IllegalArgumentException {
try (final Connection conn = this.getConnection()) {
final String query = "SELECT * FROM pet WHERE id=?";
......@@ -53,7 +44,7 @@ public class PetDAO extends DAO {
try (final ResultSet result = statement.executeQuery()) {
if (result.next()) {
return rowToEntity(result);
return rowToEntity(ownerID ,result);
} else {
throw new IllegalArgumentException("Invalid id");
}
......@@ -67,11 +58,11 @@ public class PetDAO extends DAO {
/**
* Returns a list with all the pets persisted in the system.
*
* @param ownerID id of the owner of the pets to list
* @return a list with all the pet persisted in the system.
* @throws DAOException if an error happens while retrieving the pets.
*/
public List<Pet> list() throws DAOException {
public List<Pet> list(int ownerID) throws DAOException {
try (final Connection conn = this.getConnection()) {
final String query = "SELECT * FROM pet WHERE ownerID=?";
......@@ -82,7 +73,7 @@ public class PetDAO extends DAO {
final List<Pet> petList = new LinkedList<>();
while (result.next()) {
petList.add(rowToEntity(result));
petList.add(rowToEntity(ownerID, result));
}
return petList;
......@@ -98,12 +89,13 @@ public class PetDAO extends DAO {
* Persists a new pet in the system. An identifier will be assigned
* automatically to the new pet.
*
* @param ownerID id of the owner of the pet.
* @param name name of the new pet. Can't be {@code null}.
* @return a {@link Pet} entity representing the persisted pet.
* @throws DAOException if an error happens while persisting the new pet.
* @throws IllegalArgumentException if the name is {@code null}.
*/
public Pet add(String name)
public Pet add(int ownerID, String name)
throws DAOException, IllegalArgumentException {
if (name == null) {
throw new IllegalArgumentException("name can't be null");
......@@ -194,7 +186,7 @@ public class PetDAO extends DAO {
}
}
private Pet rowToEntity(ResultSet row) throws SQLException {
private Pet rowToEntity(int ownerID, ResultSet row) throws SQLException {
return new Pet(
row.getInt("id"),
row.getString("name"),
......
......@@ -12,6 +12,11 @@ var PetDAO = (function() {
};
function PetDAO() {
//This method serves as initialization for the resourcePath var fot the current webpage
this.addToResourcePath = function(ownerID) {
resourcePath = resourcePath + ownerID + "/";
}
this.listPets = function(done, fail, always) {
requestByAjax({
url: resourcePath,
......@@ -19,14 +24,6 @@ var PetDAO = (function() {
}, done, fail, always);
};
this.setOwnerIDPetDAO = function(ownerID, done, fail, always) {
requestByAjax({
url: resourcePath + ownerID,
type: 'POST',
data: ownerID
}, done, fail, always);
};
this.addPet = function(pet, done, fail, always) {
requestByAjax({
url: resourcePath,
......
......@@ -21,8 +21,9 @@ var PetView = (function() {
dao = petDao;
self = this;
dao.setOwnerIDPetDAO(getQueryParameter('id'));
//This has to be executed prior to everything for the correct execution of every other method in dao
dao.addToResourcePath(getQueryParameter("id"));
insertPetForm($('#' + formContainerId));
insertPetList($('#' + listContainerId));
......
......@@ -6,7 +6,7 @@
</head>
<body>
<div id="pet-container">
<h1>People</h1>
<h1>Pets</h1>
<a id="#logout" href="logout">Logout</a> <a id="#atras" href="main.html">Back</a>
</div>
......
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