Now, we will create our Application. I hope, you will like this.

Background

Normally, we call Google Geolocation API, either from the Server side or from Client side, right? Today, we use the Server side. It is fully dynamic. We will use two tools, one is Postman and other one is Fiddler. Both are used to test Web API. This Google Geolocation API depends on latitude and longitude. Both will control our client. They will give the value.

Creating Database

The following query can be used to create a database in your SQL Server.

  1. CREATE DATABASE SalesTracking;
The following query can be used to create a table in your SQL Server.
  1. CREATE TABLE [dbo].[crm_DailySalesTask](
  2. [DailySalesTaskID] [bigint] NOT NULL,
  3. [UserID] [bigint] NULL,
  4. [BuyerName] [nvarchar](max) NULL,
  5. [Description] [nvarchar](max) NULL,
  6. [Achivement] [nvarchar](max) NULL,
  7. [Latitude] [nvarchar](max) NULL,
  8. [Longitude] [nvarchar](max) NULL,
  9. [Address] [nvarchar](max) NULL,
  10. [company_name] [nvarchar](max) NULL,
  11. [CreateDate] [datetime] NULL,
  12. CONSTRAINT [PK_crm_DailySalesTask] PRIMARY KEY CLUSTERED
  13. (
  14. [DailySalesTaskID] ASC
  15. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  16. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  17. GO
Creating web Application

Start Virtual Studio File>New>Project and click. New Project window will be shown on your monitor. Select Web and select ASP.NET Web Application (.NET Framework), give the name and click OK button. Again, a new Window will be shown on your monitor. From the following pop up, select Web API and click OK button once.

web Application

Figure: Web API Template with add folders and core references for MVC and Web API.

A project as MVC folder structure with core references will be created for you.

structure

Create Entity Data Model

Right click on your model folder and click new, select ADO.NET Entity Data Model. Follow the steps given. Once you have done the process, you can see the EDMX file and other files in your model folder. Here I gave SalesTrackingEntities for our Entity data model name. Now you can see a file with EDMX extension has been created.

Create ResponseModel Class

We will create ResponseModel class in Model folder. Actually, this class will be used for the return type. The code is given below:
  1. public class ResponseModel
  2. {
  3. public string Message { set; get; }
  4. public bool Status { set; get; }
  5. public object Data { set; get; }
  6. }
Create virtual class

We will create a virtual class in Model folder and the query is given bellow:
  1. public class vmTask
  2. {
  3. public Int64 UserID { get; set; }
  4. public string BuyerName { get; set; }
  5. public string Achivement { get; set; }
  6. public string Latitude { get; set; }
  7. public string Longitude { get; set; }
  8. public string Address { get; set; }
  9. public string Description { get; set; }
  10. }
Create API Controller

Right click on Controllers folder, add >controller, select Web API 2 Controller-Empty and give the name of the controller. Click OK once.
  1. public class TaskController : ApiController
  2. {
  3. SalesTrackingEntities _dbContext = null;
  4. // constructor
  5. public TaskController()
  6. {
  7. _dbContext = new SalesTrackingEntities();
  8. }
  9. // for Task save
  10. [HttpPost]
  11. public ResponseModel SaveTask(vmTask _vmTask)
  12. {
  13. ResponseModel _modelObj = new ResponseModel;
  14. int result = SaveTaskDetails(_vmTask);
  15. if (result == 1)
  16. {
  17. _modelObj.Status = true;
  18. _modelObj.Message = "Task Saved";
  19. }
  20. else
  21. {
  22. _modelObj.Status = false;
  23. _modelObj.Message = "Task Saved faill";
  24. }
  25. return _modelObj;
  26. }
  27. [HttpGet]
  28. // for get Task data by UserID
  29. public ResponseModel GetTaskList(Int64 userID)
  30. {
  31. ResponseModel _modelObj = new ResponseModel();
  32. List<vmTask> taskes = GetTaskDetails(userID);
  33. if (taskes.Count > 0)
  34. {
  35. _modelObj.Data = taskes;
  36. _modelObj.Status = true;
  37. _modelObj.Message = "Data get successfully.";
  38. }
  39. else
  40. {
  41. _modelObj.Data = taskes;
  42. _modelObj.Status = false;
  43. _modelObj.Message = "Data not found.";
  44. }
  45. return _modelObj;
  46. }
  47. private List<vmTask> GetTaskDetails(long userID)
  48. {
  49. List<vmTask> taskes = null;
  50. try
  51. {
  52. taskes = (from ds in _dbContext.crm_DailySalesTask
  53. where ds.UserID == userID
  54. select new vmTask
  55. {
  56. UserID = ds.UserID ?? 0,
  57. BuyerName = ds.BuyerName,
  58. Achivement = ds.Achivement,
  59. Description = ds.Description,
  60. Address = ds.Address
  61. }).ToList();
  62. return taskes;
  63. }
  64. catch
  65. {
  66. taskes = null;
  67. }
  68. return taskes;
  69. }
  70. private int SaveTaskDetails(vmTask _vmTask)
  71. {
  72. crm_DailySalesTask _objSalesTask = new crm_DailySalesTask();
  73. int result = 0;
  74. string Address = "";
  75. try
  76. {
  77. Int64 nextRow = _dbContext.crm_DailySalesTask.Count() + 1;
  78. _objSalesTask.DailySalesTaskID = nextRow;
  79. _objSalesTask.UserID = _vmTask.UserID;
  80. _objSalesTask.BuyerName = _vmTask.BuyerName;
  81. _objSalesTask.Description = _vmTask.Description;
  82. _objSalesTask.Achivement = _vmTask.Achivement;
  83. _objSalesTask.Latitude = _vmTask.Latitude;
  84. _objSalesTask.Longitude = _vmTask.Longitude;
  85. Address = GetAddress(_objSalesTask.Latitude, _objSalesTask.Longitude);
  86. _objSalesTask.Address = Address;
  87. _dbContext.crm_DailySalesTask.Add(_objSalesTask);
  88. _dbContext.SaveChanges();
  89. result = 1;
  90. }
  91. catch
  92. {
  93. result = 0;
  94. }
  95. return result;
  96. }
  97. // get Address
  98. private string GetAddress(string latitude, string longitude)
  99. {
  100. string locationName = "";
  101. string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude);
  102. XElement xml = XElement.Load(url);
  103. if (xml.Element("status").Value == "OK")
  104. {
  105. locationName = string.Format("{0}",
  106. xml.Element("result").Element("formatted_address").Value);
  107. }
  108. return locationName;
  109. }
  110. }
We are going to demonstrate our TaskController. First of all I want to say that in this project, I do not use any repository pattern or any multi layer. I have done all the coding in TaskController.
  1. SalesTrackingEntities _dbContext = null;
We have just pointed _dbContext instance.
  1. // constructor
  2. public TaskController()
  3. {
  4. _dbContext = new SalesTrackingEntities();
  5. }
We have created instance of SalesTarackingEntities in TaskController constructor because it will access all the tables and use linq.
  1. // for Task save
  2. [HttpPost]
  3. public ResponseModel SaveTask(vmTask _vmTask)
  4. {
  5. ResponseModel _modelObj = new ResponseModel();
  6. int result = SaveTaskDetails(_vmTask);
  7. if (result == 1)
  8. {
  9. _modelObj.Status = true;
  10. _modelObj.Message = "Task Saved";
  11. }
  12. else
  13. {
  14. _modelObj.Status = false;
  15. _modelObj.Message = "Task Saved faill";
  16. }
  17. return _modelObj;
  18. }
We use HttpPost for SaveTask method. We get vmTask object as a parameter from out world. When we will test this method, using Fiddler of Postman, we will see, how to pass vmTask object. We call SaveTaskDetails method for saving. This method exists in TaskController.
  1. private int SaveTaskDetails(vmTask _vmTask)
  2. {
  3. crm_DailySalesTask _objSalesTask = new crm_DailySalesTask();
  4. int result = 0;
  5. string Address = "";
  6. try
  7. {
  8. Int64 nextRow = _dbContext.crm_DailySalesTask.Count() + 1;
  9. _objSalesTask.DailySalesTaskID = nextRow;
  10. _objSalesTask.UserID = _vmTask.UserID;
  11. _objSalesTask.BuyerName = _vmTask.BuyerName;
  12. _objSalesTask.Description = _vmTask.Description;
  13. _objSalesTask.Achivement = _vmTask.Achivement;
  14. _objSalesTask.Latitude = _vmTask.Latitude;
  15. _objSalesTask.Longitude = _vmTask.Longitude;
  16. Address = GetAddress(_objSalesTask.Latitude, _objSalesTask.Longitude);
  17. _objSalesTask.Address = Address;
  18. _dbContext.crm_DailySalesTask.Add(_objSalesTask);
  19. _dbContext.SaveChanges();
  20. result = 1;
  21. }
  22. catch
  23. {
  24. result = 0;
  25. }
  26. return result;
  27. }
We can see the method, shown above, we just save in the database, using linq and we have called GetAddress Method with two parameters. The code is given below:
  1. private string GetAddress(string latitude, string longitude)
  2. {
  3. string locationName = "";
  4. string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude);
  5. XElement xml = XElement.Load(url);
  6. if (xml.Element("status").Value == "OK")
  7. {
  8. locationName = string.Format("{0}",
  9. xml.Element("result").Element("formatted_address").Value);
  10. }
  11. return locationName;
  12. }
In this method, we use Google API with the two parameters- latitude and longitude to get the address. We have used XElement class and Loaded static mehtod for geting API's response. After getting the location name, we have returned the location name.

Get task list by userID:
  1. private List<vmTask> GetTaskDetails(long userID)
  2. {
  3. List<vmTask> taskes = null;
  4. try
  5. {
  6. taskes = (from ds in _dbContext.crm_DailySalesTask
  7. where ds.UserID == userID
  8. select new vmTask
  9. {
  10. UserID = ds.UserID ?? 0,
  11. BuyerName = ds.BuyerName,
  12. Achivement = ds.Achivement,
  13. Description = ds.Description,
  14. Address = ds.Address
  15. }).ToList();
  16. return taskes;
  17. }
  18. catch
  19. {
  20. taskes = null;
  21. }
  22. return taskes;
  23. }
Our code is ready. Thus, we will check.

Check by Postman

For HttpPost:

HttpPost

Wow, its work is done. Let's go for HttpGet.

HttpGet

It will always work now.

Check by Fiddler

For HttpPost:

HttpPost

Output result is given below:

Output

Wow its work is done. Let's go for HttpGet.

HttpGet