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';
|
'use strict';
|
||||||
|
|
||||||
angular.module("app").controller("EditCtrl", Ctrl);
|
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;
|
var vm = this;
|
||||||
|
|
||||||
vm.contact = {};
|
vm.contact = {};
|
||||||
vm.submitForm = submitForm;
|
vm.submitForm = submitForm;
|
||||||
|
|
||||||
getContact($routeParams.id)
|
var contactId = $routeParams.id;
|
||||||
|
|
||||||
|
getContact(contactId)
|
||||||
function getContact(id) {
|
function getContact(id) {
|
||||||
if(!id) return
|
if(!id) return
|
||||||
|
|
||||||
$http.get("/api/contacts/"+id).then(function(resp) {
|
contactsService.getContact(id).then(function(contact) {
|
||||||
vm.contact = resp.data;
|
vm.contact = contact;
|
||||||
});
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
function submitForm() {
|
function submitForm() {
|
||||||
console.log("submitting contact", vm.contact);
|
console.log("submitting contact", vm.contact);
|
||||||
if(vm.contact._id) {
|
if(contactId) {
|
||||||
$http.put("/api/contacts/"+vm.contact._id, vm.contact).then(function(resp){
|
contactsService.editContact(contactId, vm.contact).then(function(resp){
|
||||||
$location.path("/search");
|
$location.path("/search");
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
$http.post("/api/contacts", vm.contact).then(function(resp){
|
contactsService.addContact(vm.contact).then(function(resp){
|
||||||
$location.path("/search");
|
$location.path("/search");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module("app").controller("SearchCtrl", Ctrl);
|
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;
|
var vm = this;
|
||||||
vm.contacts = [];
|
vm.contacts = [];
|
||||||
vm.searchString = "";
|
vm.searchString = "";
|
||||||
@ -15,17 +15,15 @@
|
|||||||
|
|
||||||
getContacts();
|
getContacts();
|
||||||
function getContacts(){
|
function getContacts(){
|
||||||
$http.get("/api/contacts").then(function(resp){
|
contactsService.getContacts()
|
||||||
vm.contacts = resp.data;
|
.then(function(contacts) {
|
||||||
}).catch(function(e) {
|
vm.contacts = contacts
|
||||||
console.error("Failed to get contacts", e);
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
};
|
|
||||||
|
|
||||||
function deleteContact(id) {
|
function deleteContact(id) {
|
||||||
modalService.confirm().then(function(resp) {
|
modalService.confirm().then(function(resp) {
|
||||||
$http.delete("/api/contacts/"+id).then(function(resp) {
|
contactsService.deleteContact(id).then(function() {
|
||||||
console.log("Deleted contact "+id)
|
console.log("Deleted contact "+id)
|
||||||
getContacts();
|
getContacts();
|
||||||
})
|
})
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"body-parser": "^1.17.1",
|
"body-parser": "^1.17.1",
|
||||||
"express": "^4.15.2",
|
"express": "^4.15.2",
|
||||||
|
"morgan": "^1.8.2",
|
||||||
"nodemon": "^1.11.0"
|
"nodemon": "^1.11.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user