64 lines
1.7 KiB
JavaScript
64 lines
1.7 KiB
JavaScript
|
(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);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|
||
|
})()
|