In last post, we learnt how to create Single Page Application route navigation system using AngularJS route module.
Pre-requisite: It is important to read Single Page Application Web API creation post before this post.
After that we will learn how to create an AngularJS $http service that will help us to perform CRUD (create, read, update and delete) operations in the database table.
In order to create service, we will use Factory method. Below is the code snippet for the serviecs we will be using in this Single Page Application demonstration.
Code snippet for 'PDService' that contains CRUD functions.
// ===================================================
// Angular Factory to create service to peform CRUD
// ===================================================
app.factory("PDService", function ($http) {
var thisPDService = {};
// get all data from database
thisPDService.GetAll = function () {
var promise = $http({
method: 'GET',
url: '/api/PersonalDetails'
})
.then(function (response) {
return response.data;
},
function (response) {
return response.data;
});
return promise;
};
// get single record from database
thisPDService.GetSingle = function (id) {
var promise = $http({
method: 'GET',
url: '/api/PersonalDetails/'+ id
})
.then(function (response) {
return response.data;
},
function (response) {
return response.data;
});
return promise;
};
// post the data from database
thisPDService.Insert = function (firstName, lastName, age, active) {
var personalDetail = {
FirstName: firstName,
LastName: lastName,
Age: age,
Active: active,
};
var promise = $http({
method: 'POST',
url: '/api/PersonalDetails',
data: personalDetail
})
.then(function (response) {
return response.statusText;
},
function (response) {
return response.statusText;
});
return promise;
};
// put the data from database
thisPDService.Update = function (autoId, firstName, lastName, age, active) {
var personalDetail = {
AutoId : autoId,
FirstName: firstName,
LastName: lastName,
Age: age,
Active: active,
};
var promise = $http({
method: 'PUT',
url: '/api/PersonalDetails/'+ autoId,
data: personalDetail
})
.then(function (response) {
return "Updated";
// return response.statusText + ' ' + response.status + ' ' + response.data;
},
function (response) {
return response.statusText + ' ' + response.status + ' ' + response.data;
});
return promise;
};
// delete the data from database
thisPDService.Remove = function (autoId) {
var promise = $http({
method: 'DELETE',
url: '/api/PersonalDetails/' + autoId
})
.then(function (response) {
// return "Deleted";
return response.statusText + ' ' + response.status + ' ' + response.data;
},
function (response) {
return response.statusText + ' ' + response.status + ' ' + response.data;
});
return promise;
};
return thisPDService;
});
In above code snippet, we have created following functions inside "PDService".
Overall, all above functions sends different ajax request to the server based on action it has to perform and receives the response from the server.
All the above functions are grouped inside "PDService" that we will use in different controllers to call respective methods in different views.
Views: 24481 | Post Order: 73