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