diff --git a/ng1/app/contacts.srv.js b/ng1/app/contacts.srv.js new file mode 100644 index 0000000..75cbde7 --- /dev/null +++ b/ng1/app/contacts.srv.js @@ -0,0 +1,63 @@ +(function () { + + angular.module('app').service('contactsService', Srv); + + Srv.$inject = ['$http']; + + function Srv($http) { + var srv = this; + srv.getContacts = getContacts; + srv.getContact = getContact; + srv.addContact = addContact; + srv.editContact = editContact; + srv.deleteContact = deleteContact; + + + function getContacts() { + return $http.get("/api/contacts") + .then(function(resp){ + return resp.data; + }).catch(function(e) { + console.error("Failed to get contacts", e); + }) + + }; + + function getContact(id) { + return $http.get("/api/contacts/"+id) + .then(function(resp) { + return resp.data; + }).catch(function(e) { + console.error("Failed to get contact "+id, e); + }); + } + + function addContact(contact) { + return $http.post("/api/contacts", contact) + .then(function(resp){ + return resp.data; + }).catch(function(e) { + console.error("Failed to add contact", e); + }); + } + + function editContact(id, contact) { + return $http.put("/api/contacts/"+id, contact) + .then(function(resp){ + return resp.data; + }).catch(function(e) { + console.error("Failed to edit contacts "+id, e); + }); + } + + function deleteContact(id) { + return $http.delete("/api/contacts/"+id) + .then(function(resp) { + return resp.data; + }).catch(function(e) { + console.error("Failed to delete contact "+id, e); + }); + } + + } +})() diff --git a/ng1/app/edit.ctrl.js b/ng1/app/edit.ctrl.js index 0f76beb..f4bce97 100644 --- a/ng1/app/edit.ctrl.js +++ b/ng1/app/edit.ctrl.js @@ -2,31 +2,33 @@ 'use strict'; angular.module("app").controller("EditCtrl", Ctrl); - Ctrl.$inject = ['$http', '$routeParams', '$location'] + Ctrl.$inject = ['$http', '$routeParams', '$location', 'contactsService'] - function Ctrl($http, $routeParams, $location) { + function Ctrl($http, $routeParams, $location, contactsService) { var vm = this; vm.contact = {}; vm.submitForm = submitForm; - getContact($routeParams.id) + var contactId = $routeParams.id; + + getContact(contactId) function getContact(id) { if(!id) return - $http.get("/api/contacts/"+id).then(function(resp) { - vm.contact = resp.data; - }); + contactsService.getContact(id).then(function(contact) { + vm.contact = contact; + }) }; function submitForm() { console.log("submitting contact", vm.contact); - if(vm.contact._id) { - $http.put("/api/contacts/"+vm.contact._id, vm.contact).then(function(resp){ + if(contactId) { + contactsService.editContact(contactId, vm.contact).then(function(resp){ $location.path("/search"); }); } else { - $http.post("/api/contacts", vm.contact).then(function(resp){ + contactsService.addContact(vm.contact).then(function(resp){ $location.path("/search"); }); } diff --git a/ng1/app/search.ctrl.js b/ng1/app/search.ctrl.js index 6d34b39..924e4fc 100644 --- a/ng1/app/search.ctrl.js +++ b/ng1/app/search.ctrl.js @@ -2,9 +2,9 @@ 'use strict'; angular.module("app").controller("SearchCtrl", Ctrl); - Ctrl.$inject = ['$http', 'modalService'] + Ctrl.$inject = ['$http', 'modalService', 'contactsService'] - function Ctrl($http, modalService) { + function Ctrl($http, modalService, contactsService) { var vm = this; vm.contacts = []; vm.searchString = ""; @@ -14,18 +14,16 @@ getContacts(); - function getContacts() { - $http.get("/api/contacts").then(function(resp){ - vm.contacts = resp.data; - }).catch(function(e) { - console.error("Failed to get contacts", e); - }) - - }; + function getContacts(){ + contactsService.getContacts() + .then(function(contacts) { + vm.contacts = contacts + }) + } function deleteContact(id) { modalService.confirm().then(function(resp) { - $http.delete("/api/contacts/"+id).then(function(resp) { + contactsService.deleteContact(id).then(function() { console.log("Deleted contact "+id) getContacts(); }) diff --git a/ng1/package.json b/ng1/package.json index 7639f19..9791f79 100644 --- a/ng1/package.json +++ b/ng1/package.json @@ -7,6 +7,7 @@ "devDependencies": { "body-parser": "^1.17.1", "express": "^4.15.2", + "morgan": "^1.8.2", "nodemon": "^1.11.0" } }