Thursday 25 February 2016

Example of kendo gride

@model Nop.Admin.Models.OutgoingCalls.Summary
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;
}
@using (Html.BeginForm())
{

    <div class="section-header accounthead">
        <div class="title">
            Outgoing Calls
        </div>
        <div class="assmeblyKitinfo form-group marginTop10 marginBot pull-left">
            <ul class="list-inline">
                <li>
                    <label>From </label>
                    @Html.TextBoxFor(model => model.From, new { @class = "form-control", @id = "txtFrom" })

                </li>
                <li>
                    <label>To </label>
                    @Html.TextBoxFor(model => model.To, new { @class = "form-control", @id = "txtTo" })

                </li>
            </ul>
        </div>
        <div class="searchWrap marginTop15">
            <div class="search-input"><input type="text" id="txtSearch" name="txtSearch" /></div>
            <div class="search-btn">
                <input type="button"  id="btnSearch" class="k-button" value="" />
            </div>
        </div>
        <div class="clearer"></div>
    </div>
}
<div class="tableWrap">
    <table class="admincontent table-responsive" style="width:100%;">

        <tr>
            <td>
                <div id="OutgoingSummary-grid"></div>
            </td>
        </tr>

    </table>
    <script type="text/javascript">
        $(function () {

            $('#txtFrom').kendoDatePicker();
            $('#txtTo').kendoDatePicker();
            $('#txtTo').change(function(){
                var grid = $('#OutgoingSummary-grid').data('kendoGrid');
                grid.dataSource.page(1);
                return false;
            });
            $('#txtFrom').change(function(){
                var grid = $('#OutgoingSummary-grid').data('kendoGrid');
                grid.dataSource.page(1);
                return false;
            });
            $("#OutgoingSummary-grid").kendoGrid({
                dataSource: {
                    type: "json",
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("GetData", "OutgoingCalls"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        }


                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        this.cancelChanges();
                    },
                    pageSize: @(defaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: false,
                    serverSorting: false,
                },
                filterable: {
                    extra:false,
                    operators: {
                        string:{ contains: "Contains"}
                    }
                },
                sortable: {
                    mode: "single",
                    allowUnsort: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(gridPageSizes)]
                },

                scrollable: false,
                selectable: false,


                columns:
                [
                {

                    title: "Id",
                    template: "#=Id#",
                    hidden: true,



                },

                {
                    field: " Date",
                    title: " Date",
                    template: "#= kendo.toString(kendo.parseDate(Date, 'yyyy-MM-dd'), 'MM/dd/yyyy') #",


                },
                {
                    field: "AccountNo",
                    title: "Account No.",



                },
                 {
                     field: "AccountName",
                     title: "Account Name",


                 },


                {
                    field: "ContactPerson",
                    title: "Contact Person",


                },

                {
                    field: "ContactNo",
                    title: "Contact No.",


                },
                {
                    field: "Email",
                    title: "Email",


                },
                {
                    field: "LastBuyDate",
                    title: "Last Buy Date",
                    template: "#= (LastBuyDate == null) ? '' :kendo.toString(kendo.parseDate(LastBuyDate, 'yyyy-MM-dd'), 'MM/dd/yyyy') #",

                 

                },

                {
                 
                    field: "OnHold",
                    title: "On Hold",
                    template: '#if(OnHold){#<input  style="float:left"  type="checkbox" checked="checked" value="#=OnHold#"   name="cb" disabled="disabled"  />#} else{#<input style="float:left"  type="checkbox"  value="#=OnHold#"   name="cb" disabled="disabled"  />#} #',

                },
                 {
                     field: "Time",
                     title: "Time",

                 },
                 {
                     field: "Result",
                     title: "Result",


                 },

                   {
                       title: "Action",
                       width:"100px",
                       command:[
                           {
                               name: "edit", text: "", imageClass: "k-icon k-edit", click: Update

                           },
                           //{
                           //    name: "edit",
                           //    text: "",
                           //    template: "<div class='k-grid-view'><a onclick='Update1(this)' > <img  src='/Administration/Content/images/notes-icon.png' /></a></div>",
                             
                           //    click: Update1

                           //}

                       ]
                   },
                   {
                       title: "Notes",
                       field: "Notes",
                       template: "<div class='k-grid-view'><a onclick='openpopup(#=Id#)' > <img  src='/Administration/Content/images/notes-icon.png' /></a></div>",
                     
                   },
                ],
                dataBound: function ()
                {
                    if ($('#OutgoingSummary-grid').find("tr").length==1) {
                        $('#OutgoingSummary-grid').find('tbody')
                                .append('<tr class="kendo-data-row"><td colspan="12" style="text-align:center"><b>No Results Found!</b></td></tr>');
                    }
                }

            } );
            function additionalData() {
                return {
                    StartDate:$('#txtFrom').val(),
                    EndDate:$('#txtTo').val(),
                    Search: $('#txtSearch').val(),

                };
            }

        });
       
        function Update(e)
        {
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            $.ajax({
                cache: false,

                type: "POST",
                url: '../OutgoingCalls/FillPopup',
                data:{Id:dataItem.Id,mark:1},
                success: function (data)
                {
                    $('#Edit').empty().append(data);
                    var window = $('#Edit');
                    if (!window.data('kendoWindow')) {

                        window.kendoWindow({
                            modal: true,
                            title: 'Result',
                            actions: ['Close'],
                            width: 600,
                            draggable: false,
                            resizable: false,
                            visible: false,
                            height: 300
                        });
                    }

                    window.data('kendoWindow').center().open();
                    return false;

                }
            });
        }
        function openpopup(s)
        {
            $.ajax({
                cache: false,

                type: "POST",

                url: '../OutgoingCalls/Openpopup',
                data:{Id:s,mark:1},
                success: function (data)
                {
                    $('#Note').empty().append(data);
                    var window = $('#Note');
                    if (!window.data('kendoWindow')) {

                        window.kendoWindow({
                            modal: true,
                            title: 'Add Note',
                            actions: ['Close'],
                            width: 600,
                            draggable: false,
                            resizable: false,
                            visible: false,
                            height: 400
                        });
                    }

                    window.data('kendoWindow').center().open();
                    return false;

                }
            });
        }
        $('#btnSearch').click(function () {
            var grid = $('#OutgoingSummary-grid').data('kendoGrid');
            grid.dataSource.page(1);
            return false;
        });
        $('#txtSearch').keydown(function (event) {
            if (event.keyCode == 13) {
                $("#btnSearch").click();
                return false;
            }

        });
        $('#txtSearch').val('Search');
        $('#txtSearch').focus(function(){
            $(this).val('');
        });
        $('#txtSearch').blur(function(){
            if($(this).val().length==0)
                $('#txtSearch').val('Search');
        });
       
    </script>
</div>
<div id="Note" style="display:none;">

</div>
<div id="Edit" style="display:none;">

</div>

Wednesday 24 February 2016

how to call a web api in console app

web api code

using Nop.Admin.Models.PurchaseOrder;
using Nop.Core.Domain.PurchaseOrder;
using Nop.Core.Domain.Vendors;
using Nop.Services.Catalog;
using Nop.Services.PurchaseOrder;
using Nop.Services.Vendors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Nop.Admin.Extensions;
using Nop.Services.SalesOrder;
using System.Web.Mvc;
using Nop.Core.Configuration;
using Nop.Services.Localization;
using Nop.Core;
using Nop.Services.Security;
using Nop.Services.Account;
using Nop.Services.Helpers;
using Nop.Services.Customers;
using Nop.Core.Infrastructure;
using Nop.Services.Common;
using Nop.Services.Invoice;
using Nop.Core.Domain.Invoice;
using System.Data.Entity;
using Nop.Core.Domain.OutgoingCalls;
using Nop.Services.OutgoingCalls;
using Nop.Services.SchedulePlanner;

namespace Nop.Admin.Controllers
{
    public class ForecastController : ApiController
    {
     
        private readonly IOutgoingCallConfigurationService _OutgoingCallConfigurationService;
        private readonly IDeliveryScheduleService _DeliveryScheduleService;
        private readonly IMonthlyDeliveryScheduleService _MonthlyDeliveryScheduleService;
        public ForecastController()
            : this(
            EngineContext.Current.Resolve<IOutgoingCallConfigurationService>(),
            EngineContext.Current.Resolve<IDeliveryScheduleService>(),
            EngineContext.Current.Resolve<IMonthlyDeliveryScheduleService>()
            )
        {
        }
        public ForecastController
            (
         
            IOutgoingCallConfigurationService OutgoingCallConfigurationService,
            IDeliveryScheduleService DeliveryScheduleService,
            IMonthlyDeliveryScheduleService MonthlyDeliveryScheduleService
            )
        {
           
            this._OutgoingCallConfigurationService = OutgoingCallConfigurationService;
            this._DeliveryScheduleService = DeliveryScheduleService;
            this._MonthlyDeliveryScheduleService = MonthlyDeliveryScheduleService;
        }


        public HttpResponseMessage DemandForecastPurchaseOrderDailyScheduler()
        {
            AddDatainOutgoingCall();
         
            HttpResponseMessage response = new HttpResponseMessage();
            return response;
        }

 #region Outgoingcallbychinmaya
        private void SaveOutgoingCall(int AccountId, int OutgoingConfigurationId)
        {
            OutgoingSummary outgoingSummary = new OutgoingSummary();
            outgoingSummary.Date = DateTime.Today;
            outgoingSummary.AccountId = AccountId;
            outgoingSummary.OnHold = false;
            outgoingSummary.Time = "00:00:00";
            outgoingSummary.ResultId = 0;
            outgoingSummary.OutgoingConfigurationId = OutgoingConfigurationId;
            outgoingSummary.Isactive = true;
            outgoingSummary.CreatedDate = DateTime.UtcNow;
            _OutgoingCallConfigurationService.PostOutgoingSummary(outgoingSummary);
        }
        private string GetDeliverDay(int Days, string CurrentDay)
        {
            string DeliverDay = string.Empty;
            switch (Days)
            {
                case 1:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Mon";
                        break;
                    }
                case 2:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Tue";
                        break;
                    }
                case 3:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Wed";
                        break;
                    }
                case 4:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Fri";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Thu";
                        break;
                    }
                case 5:
                    {
                        if (CurrentDay.Trim() == "Mon".Trim())
                            DeliverDay = "Mon";
                        else if (CurrentDay.Trim() == "Tue".Trim())
                            DeliverDay = "Tue";
                        else if (CurrentDay.Trim() == "Wed".Trim())
                            DeliverDay = "Wed";
                        else if (CurrentDay.Trim() == "Thu".Trim())
                            DeliverDay = "Thu";
                        else if (CurrentDay.Trim() == "Fri".Trim())
                            DeliverDay = "Fri";
                        break;
                    }
                default:
                    DeliverDay = null;
                    break;
            }
            return DeliverDay;
        }
        private int GetweekNumber(DateTime PassDate)
        {
            int WeekNumber = 0;
            if (PassDate.Day <= 7)
            {
                WeekNumber = 1;
            }
            else if (PassDate.Day > 7 && PassDate.Day <= 14)
            {
                WeekNumber = 2;
            }
            else if (PassDate.Day > 14 && PassDate.Day <= 21)
            {
                WeekNumber = 3;
            }
            else if (PassDate.Day > 21 && PassDate.Day <= 28)
            {
                WeekNumber = 4;
            }
            else if (PassDate.Day > 28)
            {
                WeekNumber = 5;
            }
            return WeekNumber;
        }
        private void AddDatainOutgoingCall()
        {
            #region Variable
            string CurrentDay = string.Empty;
            string DeliverDay = string.Empty;
            string WeekNumber = string.Empty;
            DateTime CurrentDate = DateTime.Now;
            int i, j;
            #endregion Variable
            try
            {
                CurrentDay = DateTime.UtcNow.DayOfWeek.ToString().Substring(0, 3);
                if (CurrentDay.Trim() == "Sat" || CurrentDay.Trim() == "Sun")
                    return;
                var lstConfiguration = _OutgoingCallConfigurationService.Gets().OrderByDescending(m => m.Id).Take(1).ToList();
                DeliverDay = GetDeliverDay(lstConfiguration[0].OutGoingCallsPeriod, CurrentDay);
                if (DeliverDay == null)
                    return;
                var lstDeliverySchedule = _DeliveryScheduleService.GetAllDeliverySchedules().Where(m => m.IsLastUpdated == true).ToList();
                for (i = 0; i < lstDeliverySchedule.Count(); i++)
                {
                    if (lstDeliverySchedule[i].DeliveryTypeID == 1)
                    {

                        //save data in outgionsummary table
                        SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                    }
                    if (lstDeliverySchedule[i].DeliveryTypeID == 2)
                    {

                        if (!string.IsNullOrEmpty(lstDeliverySchedule[i].PreferredDays))
                        {
                            string[] s = lstDeliverySchedule[i].PreferredDays.Split(',');

                            for (j = 0; j < s.GetLength(0); j++)
                                if (s[j].Trim() == DeliverDay.Trim())
                                {
                                    //save data in outgionsummary table
                                    SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);
                                    break;
                                }

                        }




                    }
                    if (lstDeliverySchedule[i].DeliveryTypeID == 3)
                    {


                        WeekNumber = "Week " + GetweekNumber(DateTime.UtcNow).ToString();
                        var MonthlyDeliverySchedules = _MonthlyDeliveryScheduleService.GetAllMonthlyDeliverySchedules(0, int.MaxValue, lstDeliverySchedule[i].Id).FirstOrDefault(m => m.WeekNumber == WeekNumber);
                        if (MonthlyDeliverySchedules.Monday)
                            if (DeliverDay.Trim() == "Mon".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Tuesday)
                            if (DeliverDay.Trim() == "Tue".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Wednesday)
                            if (DeliverDay.Trim() == "Wed".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Thursday)
                            if (DeliverDay.Trim() == "Thu".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }

                        if (MonthlyDeliverySchedules.Friday)
                            if (DeliverDay.Trim() == "Fri".Trim())
                            {
                                //save data in outgionsummary table
                                SaveOutgoingCall(lstDeliverySchedule[i].AccountID, lstConfiguration[0].Id);

                            }


                    }
                }

            }
            catch (Exception ex)
            {
            }
        }
        #endregion
}

in console app code

appconfic code

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="ConsoleApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.serviceModel>
    <bindings />
    <client />
  </system.serviceModel>
  <!--<applicationSettings>
    <ConsoleApp.Properties.Settings>
      --><!--<setting name="ConsoleApp_com_qssi_ws_PHWebServices" serializeAs="String">
      </setting>--><!--
    </ConsoleApp.Properties.Settings>
  </applicationSettings>-->
  <appSettings>
    <add key="SchedulerUrl" value="http://localhost:15536/api/Forecast/DemandForecastPurchaseOrderDailyScheduler" />
    <add key="Domain" value="http://localhost" />
    <add key="UserName" value="charan_rathi@fulcrumww.com" />
    <add key="Password" value="admin" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

console code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {


        static void Main(string[] args)
        {
            RunDailyScheduler();
        }

        private static void RunDailyScheduler()
        {
            try
            {
                string Domain = ConfigurationManager.AppSettings["Domain"];
                string UserName = ConfigurationManager.AppSettings["UserName"];
                string Password = ConfigurationManager.AppSettings["Password"];

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["SchedulerUrl"]);
                webRequest.Method = "POST";
                webRequest.ContentLength = 0;
                webRequest.PreAuthenticate = true;
                webRequest.Credentials = new NetworkCredential(UserName, Password, Domain);
                var response = (HttpWebResponse)webRequest.GetResponse();

                //HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create(ConfigurationSettings.AppSettings["ScheduleUrl"]);
                //webRequest1.Method = "POST";
                //webRequest1.ContentLength = 0;
                //webRequest1.Credentials = new NetworkCredential(UserName, Password, Domain);
                //var res = (HttpWebResponse)webRequest1.GetResponse();
            }

            catch (Exception ex)
            {
                //log the exception here
            }
        }
    }
}


Tuesday 23 February 2016

CURD opration and Search,Sort and Pagination using anguraljs,MVC,WEB API & MVVM with all control and cascading dropdown



WEB API code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        public List<string> Getc()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.COUNTRies.Select(m=>m.CNAME).ToList();
            }
        }
        public List<string> Gets(string CNAME)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                int i = obj.COUNTRies.SingleOrDefault(m => m.CNAME == CNAME).CID;
                return obj.STATEs.Where(n => n.CID == i).Select(m=>m.SNAME).ToList();
            }
        }
        public List<EMP> GetAll(int i)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
        }
        public EMP GetById(int t, int Id)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.SingleOrDefault(m => m.EID == Id);
            }
        }
        [HttpPost]
        public string Save(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                obj.EMPs.Add(emp);
                obj.SaveChanges();
                return "Data Saved.";
            }
         
        }
        [HttpPut]
        public string Update(EMP emp1)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp = obj.EMPs.SingleOrDefault(m => m.EID == emp1.EID);
                emp.NAME = emp1.NAME;
                emp.ADDRESS= emp1.ADDRESS;
                emp.PASSWORD = emp1.PASSWORD;
                emp.GENDER = emp1.GENDER;
                emp.ISMARRIED = emp1.ISMARRIED;
                emp.CNAME = emp1.CNAME;
                emp.SNAME = emp1.SNAME;
                obj.SaveChanges();
                return "Data Updated.";
            }

        }
        [HttpPatch]
        public string Delete(EMP emp1)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp = obj.EMPs.SingleOrDefault(m => m.EID == emp1.EID);
                obj.EMPs.Remove(emp);
                obj.SaveChanges();
                return "Data Deleted.";
            }

        }
    }
   
}


HTML VIEW CODE :

@{
    ViewBag.Title = "Index";
}
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/Paging.js"></script>

<h2></h2>
<br /><br /><br />
<div class="container" ng-app="app" ng-controller="ctr">
    <form class="form-horizontal">
        <div class="form-group">
            <label class="control-label col-lg-4">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="control-label col-lg-4">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="control-label col-lg-4">ADDRESS</label>
            <div class="col-lg-4">
                <textarea class="form-control" ng-model="EMP.ADDRESS" ></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-4">PASSWORD</label>
            <div class="col-lg-4">
                <input type="password" class="form-control" ng-model="EMP.PASSWORD" />
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-4">GENDER</label>
            <div class="col-lg-4">
                <input type="radio"  ng-model="EMP.GENDER" value="Male" />Male
                <input type="radio" ng-model="EMP.GENDER" value="Female" />Female
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-4"></label>
            <div class="col-lg-4">
                <input type="checkbox" ng-model="EMP.ISMARRIED"  />ISMARRIED
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-4">COUNTRY</label>
            <div class="col-lg-4">
               <select class="form-control" id="ddl" ng-model="EMP.CNAME" ng-change="fill()" ng-options="c for c in listc"></select>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-4">STATE</label>
            <div class="col-lg-4">
                <select class="form-control" ng-model="EMP.SNAME" ng-options="c for c in lists"></select>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-4"></label>
            <div class="col-lg-4">
                <input type="button" value="Save" ng-click="save()" class="btn btn-primary" style="width:80px"/>
                <input type="button" value="Update" ng-click="update()" class="btn btn-primary" style="width:80px" />
                <input type="button" value="Reset" ng-click="reset()" class="btn btn-primary" style="width:80px" />
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-1">SEARCH</label>
            <div class="col-lg-2">
                <input type="text" class="form-control" ng-model="search" />
            </div>
        </div>
        <div class="row">
            <table class="table table-bordered table-condensed table-hover table-responsive table-striped">
                <thead class="bg-primary">
                    <tr>
                        <th ng-click="order('EID')">EID
                            <span class="glyphicon sort-icon" ng-show="sortKey=='EID'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
                        </th>
                        <th ng-click="order('NAME')">NAME
                            <span class="glyphicon sort-icon" ng-show="sortKey=='NAME'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
                        </th>
                        <th>UPDATE</th>
                        <th>DELETE</th>
                    </tr>
                </thead>
                <tbody>
                    <tr dir-paginate="c in list|filter:search|orderBy:predicate:reverse|itemsPerPage:2">
                        <td>{{c.EID}}</td>
                        <td>{{c.NAME}}</td>
                        <td><a ng-click="edit(c.EID)">Edit</a></td>
                        <td><a ng-click="del(c)">Delete</a></td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <dir-pagination-controls max-size="2"
                                                     direction-links="true"
                                                     boundary-links="true">
                            </dir-pagination-controls>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
</div>
<script type="text/javascript">
    angular.module("app", ['angularUtils.directives.dirPagination']).controller("ctr", function ($scope, $http) {
        var m;
        function clear()
        {
            $scope.EMP = new emp();
            $('#tb').focus();
        } clear();
        function fill()
        {
            m = $http({
                url: 'http://localhost:50293/api/Values/GetAll?i=1',
                method:'Get'
            });
            m.then(function (d) {
                $scope.list = d.data;
            });
        } fill();
        m = $http({
            url: 'http://localhost:50293/api/Values/Getc',
            method: 'Get'
        });
        m.then(function (d) {
            $scope.listc = d.data;
        });
        $scope.fill = function ()
        {
            fillddl($('#ddl :selected').text());
        }
        $scope.save = function () {
         
            m = $http({
                url: 'http://localhost:50293/api/Values/Save',
                method: 'Post',
                data: $scope.EMP
            });
            m.then(function (d) {
                alert(d.data);
                clear();
                fill();
            });
        }
        $scope.update = function () {

            m = $http({
                url: 'http://localhost:50293/api/Values/Update',
                method: 'Put',
                data: $scope.EMP
            });
            m.then(function (d) {
                alert(d.data);
                clear();
                fill();
            });
        }
        $scope.del = function (n)
        {
            if (confirm('Do you want to delete it ?'))
            {
                m = $http({
                    url: "http://localhost:50293/api/Values/Delete",
                    data:n,
                    method: 'Patch'
                });
                m.then(function (d) {
                    alert(d.data);
                    fill();
                });
            }
        }
        $scope.reset = function ()
        {
            clear();
        }
        $scope.edit = function (n)
        {
         
            m = $http({
                url: "http://localhost:50293/api/Values/GetById?t=" + n + "&Id="+n,
                method: 'Get'
            });
            m.then(function (d) {
                fillddl(d.data.CNAME);
                $scope.EMP = d.data;
            });
        }
        function fillddl(s)
        {
            m = $http({
                url: 'http://localhost:50293/api/Values/Gets?CNAME='+s,
                method: 'Get'
            });
            m.then(function (d) {
                $scope.lists= d.data;
            });
        }
        $scope.order = function (p) {
            $scope.reverse = ($scope.predicate === p) ? !$scope.reverse : false;
            $scope.predicate = p;
        }
    });
    function emp()
    {
        return {
            EID: null,
            NAME: null,
            ADDRESS: null,
            PASSWORD: null,
            GENDER: null,
            ISMARRIED: true,
            CNAME: "X",
            SNAME:null
        }
    }
</script>

Sunday 21 February 2016

Time calcularion

 string[] ss = outgoingCallSummaryDetails.Time.Split(':');
                        h = Convert.ToInt32(ss[0]) + DateTime.UtcNow.Subtract(outgoingCallSummaryDetails.UpdatedDate.Value).Hours;
                        mm = Convert.ToInt32(ss[1]) + DateTime.UtcNow.Subtract(outgoingCallSummaryDetails.UpdatedDate.Value).Minutes;
                        s = Convert.ToInt32(ss[2]) + DateTime.UtcNow.Subtract(outgoingCallSummaryDetails.UpdatedDate.Value).Seconds;
                        total = (h * 3600) + (mm * 60) + s;
                        h = total / 3600;
                        mm = (total % 3600) / 60;
                        s = (total % 3600) % 60;
                        outgoingSummary.Time = h.ToString() + ":" + mm.ToString() + ":" + s.ToString();

Tuesday 9 February 2016

CURD in webapi angularjs & MVC


web api code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MvcApplication5.Models;
using System.Data.Entity;

namespace MvcApplication5.Controllers
{
    public class ValuesController : ApiController
    {
       [HttpGet]
        public List<EMP> Gets()
        {
            using(Database1Entities obj=new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
           
        }

      [HttpGet]
        public EMP Get(int id)
        {
            using(Database1Entities obj=new Database1Entities())
            {
                return obj.EMPs.SingleOrDefault(m => m.EID == id);
            }
        }

       [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 emp1)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp = obj.EMPs.FirstOrDefault(m => m.EID == emp1.EID);
                emp.NAME = emp1.NAME;
                obj.SaveChanges();
                return "Data Updated.";
            }
        }

       
       [HttpPatch]
        public string Delete(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp1 = obj.EMPs.FirstOrDefault(m => m.EID == emp.EID);
                obj.EMPs.Remove(emp1);
                obj.SaveChanges();
                return "Data deleted.";
            }
        }
    }
}

view code :

@{
    ViewBag.Title = "Index";
}
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/bootstrap-theme.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/Paging.js"></script>

<br /><br /><br />
<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" id="tb" class="form-control" ng-model="EMP.EID" />
            </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" class="btn btn-primary" value="Save" ng-click="Save()" />
                <input type="button" class="btn btn-primary" value="Update" ng-click="Update()" />
                <input type="button" class="btn btn-primary" value="Reset" ng-click="Reset()" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-1 control-label">SEARCH</label>
            <div class="col-lg-2">
                <input type="text" class="form-control" ng-model="search" />
            </div>
        </div>
        <div class="row">
            <table class="table table-bordered table-condensed table-hover table-responsive table-striped">
                <thead class="bg-primary">
                    <tr>
                        <th ng-click="order('EID')">EID
                            <span class="glyphicon sort-icon" ng-show="sortKey=='EID'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
                        </th>
                        <th ng-click="order('NAME')">NAME
                            <span class="glyphicon sort-icon" ng-show="sortKey=='NAME'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
                        </th>
                        <th>UPDATE</th>
                        <th>DELETE</th>
                    </tr>
                </thead>
               <tbody>
                   <tr dir-paginate="c in list|filter:search|orderBy:predicate:reverse|itemsPerPage:2">
                       <td>{{c.EID}}</td>
                       <td>{{c.NAME}}</td>
                       <td><a ng-click="edit(c.EID)">Edit</a></td>
                       <td><a ng-click="del(c)">Delete</a></td>
                   </tr>
                   <tr>
                       <td colspan="4">
                           <dir-pagination-controls max-size="2"
                                                    direction-links="true"
                                                    boundary-links="true">
                           </dir-pagination-controls>
                       </td>
                   </tr>
               </tbody>
            </table>
        </div>
    </form>
</div>
<script type="text/javascript">
    function emp()
    {
        return {
            EID: null,
            NAME:null
        }
    }
    angular.module("app", ['angularUtils.directives.dirPagination']).controller("ctr", function ($scope, $http) {
        function fill()
        {
            var m = $http({
                url: 'http://localhost:61521/api/Values',
                method: 'Get',

            });
            m.then(function (d) {
                $scope.list = d.data;
            });
        } fill();
        function Clear()
        {
            $scope.EMP = new emp();
            $('#tb').focus();
        }
        $scope.Save = function ()
        {
            var m = $http({
                url: 'http://localhost:61521/api/Values/Post',
                method: 'Post',
                data: $scope.EMP,

            });
            m.then(function (d) {
                alert(d.data);
                fill();
                Clear();
            });

        }
        $scope.Update = function () {
            var m = $http({
                url: 'http://localhost:61521/api/Values/Put',
                method: 'Put',
                data: $scope.EMP,
            });
            m.then(function (d) {
                alert(d.data);
                fill();
                Clear();
            });

        }
        $scope.del = function (n) {
            if (confirm('Do you want to delete it ?'))
            {
                var m = $http({
                    url: 'http://localhost:61521/api/Values/Delete',
                    method: 'Patch',
                    data: n,
                });
                m.then(function (d) {
                    alert(d.data);
                    fill();
                    
                });

            }
        }
        $scope.edit = function (n) {
            
                var m = $http({
                    url: 'http://localhost:61521/api/Values/Get?id='+n,
                    method: 'Get'
                });
                m.then(function (d) {
                    $scope.EMP= d.data;
                });

           
        }
        $scope.Reset = function () {
            Clear();
        }
        $scope.order = function (p) {
            $scope.reverse = ($scope.predicate === p) ? !$scope.reverse : false;
            $scope.predicate = p;
        }
    });
</script>