Saturday 6 August 2016

DML operation in angular js using Service

service :

A service is a constructor function which creates the object using new keyword. You can add properties and functions to a service object by using this keyword. Unlike factory, it doesn’t return anything.
Example

web api code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication38.Models;

namespace WebApplication38.Controllers
{

    public class ValuesController : ApiController
    {
        [HttpGet]
        public List<EMP> Gets()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
        }
        [HttpGet]
        public EMP Get(int EID)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.SingleOrDefault(m => m.EID == EID);
            } 
        }
        [HttpPost]
        public string Post(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                obj.EMPs.Add(emp);
                obj.SaveChanges();
                return "Data Saved.";
            }
        }
        [HttpPut]
        public string Put(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp1 = obj.EMPs.SingleOrDefault(m => m.EID == emp.EID);
                emp1.NAME = emp.NAME;
                obj.SaveChanges();
                return "Data Updated.";
            }
        }
        [HttpDelete]
        public string Mdelete([FromUri]List<int> lst)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                obj.EMPs.RemoveRange(obj.EMPs.Where(m => lst.Contains(m.EID)));
                obj.SaveChanges();
                return "Data Deleted.";
            }
        }
        [HttpGet]
        public string Delete(int EID,int i)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp1 = obj.EMPs.SingleOrDefault(m => m.EID == EID);
                obj.EMPs.Remove(emp1);
                obj.SaveChanges();
                return "Data Deleted.";
            }
        }


    }
}
View Code :

@{
    ViewBag.Title = "Index";
}
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/Service.js"></script>
<h2>Jay Jagannath...</h2>
<div class="container" ng-app="app" ng-controller="ctr">
    <form class="form-horizontal">
        <div class="form-group">
            <label class="col-lg-4 control-label">EID</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EMP.EID" id="tb" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">NAME</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EMP.NAME" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label"></label>
            <div class="col-lg-4">
                <input type="button" value="Delete All" style="width:80px" class="btn btn-primary" ng-click="da()" />
                <input type="button" value="Save" style="width:80px" class="btn btn-primary" ng-click="save()" />
                <input type="button" value="Update" style="width:80px" class="btn btn-primary" ng-click="update()" />
                <input type="button" value="Reset" style="width:80px" class="btn btn-primary" ng-click="reset()" />
            </div>
        </div>
        <div class="row">
            <table class="table table-bordered table-condensed table-hover table-responsive table-striped">
                <thead class="bg-primary">
                    <tr>
                        <th><input type="checkbox" id="hcb" /></th>
                        <th>EID</th>
                        <th>NAME</th>
                        <th>UPDATE</th>
                        <th>DELETE</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="c in list" class="row1">
                        <td><input type="checkbox" class="cb" value="{{c.EID}}" /></td>
                        <td>{{c.EID}}</td>
                        <td>{{c.NAME}}</td>
                        <td><a ng-click="edit(c)">Edit</a></td>
                        <td><a ng-click="del(c)">Delete</a></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
</div>
Angular Js Code :(service.js)
/// <reference path="jquery-1.9.1.js" />
/// <reference path="angular.js" />


angular.module("app", [])
.controller("ctr", function ($scope, EmpService) {

    function clr() {
        $scope.EMP = new empvm();
        $('#tb').focus();
    } clr();
    function fill()
    {
        EmpService.GetAll().then(function (d) {
            $scope.list = d.data;
        });
    } fill();
    $scope.reset = function ()
    {
        clr();
    }
    $scope.da = function ()
    {
        if (confirm('Do you want to delete it ?'))
        {
            var n = '';
            angular.forEach($(".cb:checked"), function (i, j) {
                n = n + 'lst=' + $(i).val() + '&';
            });
            n = n.substring(0, n.lastIndexOf('&'));
            EmpService.Mdelete(n).then(function (d) {
                alert(d.data);
                fill();
            });
        }
    }
    $scope.save = function ()
    {
        EmpService.Save($scope.EMP).then(function (d) {
            alert(d.data);
            clr();
            fill();
        });
    }
    $scope.edit = function (s)
    {
        EmpService.Get(s.EID).then(function (d) {
            $scope.EMP = d.data;
        });
    }
    $scope.update = function () {
        EmpService.Update($scope.EMP).then(function (d) {
            alert(d.data);
            clr();
            fill();
        });
    }
    $scope.del = function (s)
    {
        if (confirm('Do you want to delete it ?'))
        {
            EmpService.Delete(s.EID).then(function (d) {
                alert(d.data);
                fill();
            });
        }
    }
    $('#hcb').click(function () {

        if ($('#hcb').is(":checked"))
            $('.cb').prop("checked", true);
        else
            $('.cb').prop("checked", false);
    });
    $('body').on("click", '.cb', function () {
        if ($('.cb').length == $('.cb:checked').length)
            $('#hcb').prop("checked", true);
        else
            $('#hcb').prop("checked", false);
    });
})
.service("EmpService", function ($http) {
    this.GetAll = function () {
        return $http.get('http://localhost:63386/api/Values');
    }
    this.Get = function (EID) {
        return $http.get('http://localhost:63386/api/Values/Get?EID=' + EID);
    }
    this.Save = function (EMP) {
        return $http.post('http://localhost:63386/api/Values/Post', EMP);
    }
    this.Update = function (EMP) {
        return $http.put('http://localhost:63386/api/Values/Put', EMP);
    }
    this.Delete = function (EID) {
        return $http.get('http://localhost:63386/api/Values/Delete?EID=' + EID+"&i="+1);
    }
    this.Mdelete = function (lst) {
        return $http.delete('http://localhost:63386/api/Values/Mdelete?' + lst);
    }
}
);
function empvm()
{
    return {
        EID: null,
        NAME:null
    }
}

When to use

It is a singleton object. Use it when you need to share a single object across the application. For example, authenticated user details.

No comments:

Post a Comment