From 0fec1cc5f847182be6c268f81b00bf619cab9261 Mon Sep 17 00:00:00 2001 From: hacklego Date: Sun, 26 Feb 2017 20:51:38 +0100 Subject: [PATCH] Added frontend PetDAO --- src/main/webapp/js/dao/pets.js | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/webapp/js/dao/pets.js diff --git a/src/main/webapp/js/dao/pets.js b/src/main/webapp/js/dao/pets.js new file mode 100644 index 0000000..72c3d2b --- /dev/null +++ b/src/main/webapp/js/dao/pets.js @@ -0,0 +1,54 @@ +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.listPeoplePets = function(id, done, fail, always) { + requestByAjax({ + url: "rest/people/" + id + "/pets/", + 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 -- 2.18.1