homework/ng1/app/contacts.srv.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

(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);
});
}
}
})()