diff --git a/src/main/java/es/uvigo/esei/daa/entities/Pet.java b/src/main/java/es/uvigo/esei/daa/entities/Pet.java
index e3f73321d87b0587fbbc28e827d925658185b5a8..b42ef4467c050b0056c0e140733aa908b5801378 100644
--- a/src/main/java/es/uvigo/esei/daa/entities/Pet.java
+++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java
@@ -73,7 +73,4 @@ public class Pet {
public void setOwner(Person owner) {
this.owner = requireNonNull(owner, "Owner can't be a null reference");
}
-
-
- //TODO -------- ESTABA HACIENDO ESTO MIRAR PRACTICA 2 PARA ENUNCIADO
}
diff --git a/src/main/webapp/js/dao/pet.js b/src/main/webapp/js/dao/pet.js
new file mode 100644
index 0000000000000000000000000000000000000000..08ae3dd02924b833dbc9e705474475b6233a4e39
--- /dev/null
+++ b/src/main/webapp/js/dao/pet.js
@@ -0,0 +1,47 @@
+var PetDAO = (function() {
+ var resourcePath = "rest/pet/";
+ 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 PetDAO() {
+ 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.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 PetDAO;
+})();
\ No newline at end of file
diff --git a/src/main/webapp/js/view/people.js b/src/main/webapp/js/view/people.js
index 1f7bc5ec03d301065334b27dd3cbee15c881a8ad..b8727b969805832a7527ec944ce8f58eec2adf09 100644
--- a/src/main/webapp/js/view/people.js
+++ b/src/main/webapp/js/view/people.js
@@ -142,6 +142,9 @@ var PeopleView = (function() {
return '
\
' + person.name + ' | \
' + person.surname + ' | \
+ \
+ Pets\
+ | \
\
Edit\
| \
@@ -171,6 +174,10 @@ var PeopleView = (function() {
);
}
});
+
+ $('#person-' + person.id + ' a.pet').click(function() {
+ window.location.href = "pet.html?id="+person.id;
+ });
}
var appendToTable = function(person) {
diff --git a/src/main/webapp/js/view/pet.js b/src/main/webapp/js/view/pet.js
new file mode 100644
index 0000000000000000000000000000000000000000..0171c8d854ece9deeb7c267dcaba782b388b3cd9
--- /dev/null
+++ b/src/main/webapp/js/view/pet.js
@@ -0,0 +1,176 @@
+//TODO esto en principio está, pero mantener un ojo en ello
+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(petDao, formContainerId, listContainerId) {
+ dao = petDao;
+ self = this;
+
+ insertPetForm($('#' + formContainerId));
+ insertPetList($('#' + listContainerId));
+
+ this.init = function() {
+ dao.listPets(function(pet) {
+ $.each(pets, 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.id + ' td.name').text(pet.name);
+ 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(),
+ };
+ };
+
+ this.getPetInRow = function(id) {
+ var row = $('#pet-' + id);
+
+ if (row !== undefined) {
+ return {
+ 'id': id,
+ 'name': row.find('td.name').text(),
+ };
+ } else {
+ return undefined;
+ }
+ };
+
+ this.editPet = function(id) {
+ var row = $('#pet-' + id);
+
+ console.log(row);
+ if (row !== undefined) {
+ var form = $(formQuery);
+ console.log(form);
+ console.log(row.find('td.name').text());
+
+ form.find('input[name="id"]').val(id);
+ form.find('input[name="name"]').val(row.find('td.name').text());
+ }
+ }
+
+ 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 insertPetList = function(parent) {
+ parent.append(
+ '\
+ \
+ Nombre | \
+ | \
+ | \
+
\
+
'
+ );
+ }
+
+ var insertPetForm = function(parent) {
+ parent.append(
+ ''
+ );
+ }
+
+ var createPetRow = function(pet) {
+ return '
\
+ ' + pet.name + ' | \
+ \
+ Edit\
+ | \
+ \
+ Delete\
+ | \
+
';
+ }
+
+ var showErrorMessage = function(jqxhr, textStatus, error) {
+ alert(textStatus + ": " + error);
+ }
+
+ var addRowListeners = function(pet) {
+ $('#pet-' + pet.id + ' a.edit').click(function() {
+ self.editPet(pet.id);
+ $('input#btnSubmit').val('Modificar');
+ });
+
+ $('#pet-' + pet.id + ' a.delete').click(function() {
+ if (confirm('Está a punto de eliminar una mascota. ¿Está seguro de que desea continuar?')) {
+ dao.deletePet(pet.id,
+ function() {
+ $('tr#pet-' + pet.id).remove();
+ },
+ showErrorMessage
+ );
+ }
+ });
+ }
+
+ var appendToTable = function(pet) {
+ $(listQuery + ' > tbody:last')
+ .append(createPetRow(pet));
+ addRowListeners(pet);
+ }
+
+ return PetView;
+})();
diff --git a/src/main/webapp/main.html b/src/main/webapp/main.html
index 461f6e52e8f92bfc313bca30ac8b347d9122387f..dd0afcc1e86f4bc6b6d3c4922d095515ad361d56 100644
--- a/src/main/webapp/main.html
+++ b/src/main/webapp/main.html
@@ -1,8 +1,8 @@
-
-DAA Example
+
+ DAA Example
diff --git a/src/main/webapp/pet.html b/src/main/webapp/pet.html
new file mode 100644
index 0000000000000000000000000000000000000000..ac81ccdc7e8345fe2bc618bbc078aea5a0f9f9c3
--- /dev/null
+++ b/src/main/webapp/pet.html
@@ -0,0 +1,24 @@
+
+
+
+
+
DAA Example
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file