- Login:
+
+
\ No newline at end of file
diff --git a/src/main/webapp/js/dao/pets.js b/src/main/webapp/js/dao/pets.js
new file mode 100644
index 0000000000000000000000000000000000000000..0dd1d72ce7f783b68e6f90a7e6d7ca38c3dc2d3b
--- /dev/null
+++ b/src/main/webapp/js/dao/pets.js
@@ -0,0 +1,50 @@
+/**
+ * Created by Alex on 15/02/2017.
+ */
+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.listPets = 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 PetsDAO;
+})();
\ 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..0d7309baaa42ac5eca3064e65181ba1c4253ab74 100644
--- a/src/main/webapp/js/view/people.js
+++ b/src/main/webapp/js/view/people.js
@@ -118,6 +118,9 @@ var PeopleView = (function() {
'
\
\
Nombre | \
+ | \
+ | \
+ | \
Apellido | \
| \
| \
@@ -145,7 +148,7 @@ var PeopleView = (function() {
\
Edit\
| \
- \
+ | \
Delete\
| \
';
diff --git a/src/main/webapp/js/view/pets.js b/src/main/webapp/js/view/pets.js
new file mode 100644
index 0000000000000000000000000000000000000000..8c5a09b72f8f5e58380bd5a2af0c5d78e6fdb0ed
--- /dev/null
+++ b/src/main/webapp/js/view/pets.js
@@ -0,0 +1,193 @@
+/**
+ * Created by Alex on 15/02/2017.
+ */
+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(petsDao, formContainerId, listContainerId) {
+ dao = petsDao;
+ self = this;
+
+ insertPetsForm($('#' + formContainerId));
+ insertPetsList($('#' + listContainerId));
+
+ this.init = function() {
+ dao.listPets(function(pets) {
+ $.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);
+ $('#pet-' + pet.id + ' td.breed').text(pet.breed);
+ $('#pet-' + pet.id + ' 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 {
+ 'id': form.find('input[name="id"]').val(),
+ 'name': form.find('input[name="name"]').val(),
+ 'breed': form.find('input[name="breed"]').val(),
+ 'idOwner': form.find('input[name="idOwner"]').val()
+ };
+ };
+
+ this.getPetInRow = function(id) {
+ var row = $('#pet-' + id);
+
+ if (row !== undefined) {
+ return {
+ 'id': id,
+ 'name': row.find('td.name').text(),
+ 'breed': row.find('td.breed').text(),
+ 'idOwner': row.find('td.idOwner').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());
+ console.log(row.find('td.breed').text());
+ console.log(row.find('td.idOwner').text());
+
+ form.find('input[name="id"]').val(id);
+ form.find('input[name="name"]').val(row.find('td.name').text());
+ form.find('input[name="breed"]').val(row.find('td.breed').text());
+ form.find('input[name="idOwner"]').val(row.find('td.idOwner').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 insertPetsList = function(parent) {
+ parent.append(
+ '\
+ \
+ Nombre | \
+ Raza | \
+ Owner | \
+ | \
+
\
+
'
+ );
+ }
+
+ var insertPetsForm = function(parent) {
+ parent.append(
+ ''
+ );
+ }
+
+ var createPetRow = function(pet) {
+ return '\
+ ' + pet.name + ' | \
+ ' + pet.breed + ' | \
+ ' + pet.idOwner + ' | \
+ \
+ 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 a una peta. ¿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 PetsView;
+})();
\ No newline at end of file
diff --git a/src/main/webapp/main.html b/src/main/webapp/main.html
index 461f6e52e8f92bfc313bca30ac8b347d9122387f..42f1fa575877378fdd5418fd3040c64a21a9246f 100644
--- a/src/main/webapp/main.html
+++ b/src/main/webapp/main.html
@@ -1,5 +1,8 @@
+
+
+
DAA Example
@@ -7,16 +10,23 @@
+
+
diff --git a/src/main/webapp/main_pet.html b/src/main/webapp/main_pet.html
new file mode 100644
index 0000000000000000000000000000000000000000..401643584db81dcbbb07c876284e8f8febb4efdc
--- /dev/null
+++ b/src/main/webapp/main_pet.html
@@ -0,0 +1,36 @@
+
+
+
+
+
+
+
+ DAA Example
+
+
+
+
Pets
+
+
+
Back
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/test.iml b/src/test/test.iml
new file mode 100644
index 0000000000000000000000000000000000000000..5ebc6f48ecbe7e78271fcbb44057f6682d802638
--- /dev/null
+++ b/src/test/test.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file