Migrate contacts rest api to a seperate service.
This commit is contained in:
parent
284320f618
commit
851743eb7e
63
ng1/app/contacts.srv.js
Normal file
63
ng1/app/contacts.srv.js
Normal file
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
})()
|
@ -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");
|
||||
});
|
||||
}
|
||||
|
@ -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();
|
||||
})
|
||||
|
@ -7,6 +7,7 @@
|
||||
"devDependencies": {
|
||||
"body-parser": "^1.17.1",
|
||||
"express": "^4.15.2",
|
||||
"morgan": "^1.8.2",
|
||||
"nodemon": "^1.11.0"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user