Wednesday, October 26, 2016

Developing Book My Seat Application In AngularJS And ASP.NET - WebAPI Methods - Part Two

Here, I’ve merely thought to write my thoughts about hands-on Angular. This is my first article which tells you how to get your hands dirty with AngularJS & ASP.NET WEBAPI and SQL Server BookMySeat Application Tutorials.

This is the technology stack for this BookMySeat library application, as shown below:

In the first article, I shared the technology and other brief information about components used. In this article, will look into WebApi creation and route to configure that.
This is an initial look at the WebAPI controller defined in Solution Explorer as depicted below:

If you open this file, you will have around 6 methods which have generic names to perform the operations and easy to understand.These WebAPI methods are given below:




The complete code for all APIs is given below.

  1. public class BookMySeatAPIController : ApiController 
  2.     { 
  3. //
  4. // GET: /BookMySeatAPI/
  5.         SqlConnection objConnection = new SqlConnection(); 
  6. public void SqlConnection() 
  7.         { 
  8.             objConnection.Dispose(); 
  9.             objConnection.ConnectionString = "server=.;database=BookMySeat;uid=sa;pwd=Tpg@1234;"; 
  10.             objConnection.Open(); 
  11.         } 
  12.         [HttpGet] 
  13. public int[] GetSeatCount([FromUri] int slot) 
  14.         { 
  15. try
  16.             { 
  17.                 SqlConnection(); 
  18.                 SqlCommand SqlCommand = new SqlCommand("sp_GetMySeat", objConnection); 
  19.                 SqlCommand.CommandType = CommandType.StoredProcedure; 
  20.                 SqlCommand.Parameters.AddWithValue("@timeslotid", slot); 
  21.                 SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
  22.                 da.SelectCommand = SqlCommand; 
  23.                 DataSet ds = new DataSet(); 
  24.                 da.Fill(ds); 
  25. int[] result = new int[ds.Tables[0].Rows.Count]; 
  26. for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
  27.                 { 
  28.                     result[i] = Convert.ToInt16(ds.Tables[0].Rows[i][0].ToString()); 
  29.                 } 
  30. return result; 
  31.             } 
  32. finally
  33.             { 
  34.                 objConnection.Close(); 
  35.                 objConnection.Dispose(); 
  36.             } 
  37.         } 
  38.         [HttpPost] 
  39.         [ActionName("SeatBook")] 
  40. public int[] PostBookSeat(BookSeat objBookSeat) 
  41.         { 
  42. try
  43.             { 
  44.                 SqlConnection(); 
  45.                 SqlCommand SqlCommand = new SqlCommand("sp_BookMySeat", objConnection); 
  46.                 SqlCommand.CommandType = CommandType.StoredProcedure; 
  47.                 SqlCommand.Parameters.AddWithValue("@UserName", objBookSeat.UserName); 
  48.                 SqlCommand.Parameters.AddWithValue("@TimeSlot", objBookSeat.TimeSlot); 
  49.                 SqlCommand.Parameters.AddWithValue("@SeatNo", objBookSeat.SeatNo); 
  50.                 SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
  51.                 da.SelectCommand = SqlCommand; 
  52.                 DataSet ds = new DataSet(); 
  53.                 da.Fill(ds); 
  54. int[] result = new int[ds.Tables[0].Rows.Count]; 
  55. for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
  56.                 { 
  57.                     result[i] = Convert.ToInt16(ds.Tables[0].Rows[i][0].ToString()); 
  58.                 } 
  59. return result; 
  60.             } 
  61. finally
  62.             { 
  63.                 objConnection.Close(); 
  64.                 objConnection.Dispose(); 
  65.             } 
  66.         } 
  67.         [HttpPost] 
  68.         [ActionName("ValidateUser")] 
  69. public string ValidateUser(ValidateUser objValidateUser) 
  70.         { 
  71. try
  72.             { 
  73.                 SqlConnection(); 
  74.                 SqlCommand SqlCommand = new SqlCommand("ValidateUser", objConnection); 
  75.                 SqlCommand.CommandType = CommandType.StoredProcedure; 
  76.                 SqlCommand.Parameters.AddWithValue("@UserName", objValidateUser.UserName); 
  77.                 SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
  78.                 da.SelectCommand = SqlCommand; 
  79.                 DataSet ds = new DataSet(); 
  80.                 da.Fill(ds); 
  81. return ds.Tables[0].Rows[0][0].ToString(); 
  82.             } 
  83. finally
  84.             { 
  85.                 objConnection.Close(); 
  86.                 objConnection.Dispose(); 
  87.             } 
  88.         } 
  89.         [HttpPost] 
  90.         [ActionName("SeatDetail")] 
  91. public string GetSeatDetail(GetSeatDetail objGetSeatDetail) 
  92.         { 
  93. try
  94.             { 
  95.                 SqlConnection(); 
  96.                 SqlCommand SqlCommand = new SqlCommand("sp_GetSeatDetail", objConnection); 
  97.                 SqlCommand.CommandType = CommandType.StoredProcedure; 
  98. // SqlCommand.Parameters.AddWithValue("@UserName", objBookSeat.UserName);
  99.                 SqlCommand.Parameters.AddWithValue("@TimeSlot", objGetSeatDetail.TimeSlot); 
  100.                 SqlCommand.Parameters.AddWithValue("@SeatNo", objGetSeatDetail.SeatNo); 
  101.                 SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
  102.                 da.SelectCommand = SqlCommand; 
  103.                 DataSet ds = new DataSet(); 
  104.                 da.Fill(ds); 
  105. return ds.Tables[0].Rows[0][0].ToString(); 
  106.             } 
  107. finally
  108.             { 
  109.                 objConnection.Close(); 
  110.                 objConnection.Dispose(); 
  111.             } 
  112.         } 
  113.         [HttpPost] 
  114.         [ActionName("DeleteSeat")] 
  115. public string DeleteSeat(DeleteSeat objDeleteSeat) 
  116.         { 
  117. try
  118.             { 
  119.                 SqlConnection(); 
  120.                 SqlCommand SqlCommand = new SqlCommand("sp_DeleteSeat", objConnection); 
  121.                 SqlCommand.CommandType = CommandType.StoredProcedure; 
  122. // SqlCommand.Parameters.AddWithValue("@UserName", objBookSeat.UserName);              
  123.                 SqlCommand.Parameters.AddWithValue("@SeatNo", objDeleteSeat.SeatNo); 
  124.                 SqlCommand.Parameters.AddWithValue("@SlotNo", objDeleteSeat.TimeSlot); 
  125.                 SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
  126.                 da.SelectCommand = SqlCommand; 
  127.                 DataSet ds = new DataSet(); 
  128.                 da.Fill(ds); 
  129. return ds.Tables[0].Rows[0][0].ToString(); 
  130.             } 
  131. finally
  132.             { 
  133.                 objConnection.Close(); 
  134.                 objConnection.Dispose(); 
  135.             } 
  136.         } 
  137.         [HttpGet] 
  138. public List<ShowBookSeat> ShowBookSeat() 
  139.         { 
  140. try
  141.             { 
  142.                 SqlConnection(); 
  143.                   List<ShowBookSeat> list = new List<ShowBookSeat>();  
  144.                 SqlCommand SqlCommand = new SqlCommand("sp_ShowBookDetail", objConnection); 
  145.                 SqlCommand.CommandType = CommandType.StoredProcedure; 
  146. // SqlCommand.Parameters.AddWithValue("@UserName", objBookSeat.UserName);              
  147. //SqlCommand.Parameters.AddWithValue("@UserName", objDeleteSeat.SeatNo);
  148. //SqlCommand.Parameters.AddWithValue("@SlotNo", objDeleteSeat.TimeSlot);
  149.                 SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
  150.                 da.SelectCommand = SqlCommand; 
  151.                 DataSet ds = new DataSet(); 
  152.                 da.Fill(ds); 
  153. for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
  154.                 { 
  155.                     ShowBookSeat ShowBookSeat = new ShowBookSeat(); 
  156.                     ShowBookSeat.UserName = (ds.Tables[0].Rows[i]["username"]).ToString(); 
  157.                     ShowBookSeat.Date =Convert.ToDateTime(ds.Tables[0].Rows[i]["currentday"]); 
  158.                     ShowBookSeat.TimeSlot = Convert.ToInt32(ds.Tables[0].Rows[i]["timeslot"]); 
  159.                     ShowBookSeat.SeatNo = Convert.ToInt32(ds.Tables[0].Rows[i]["seatno"]); 
  160.                     list.Add(ShowBookSeat); 
  161.                 } 
  162. return list;  
  163.             } 
  164. finally
  165.             { 
  166.                 objConnection.Close(); 
  167.                 objConnection.Dispose(); 
  168.             } 
  169.         } 
  170.     } 

Code segment for WebApiConfig under App_start folder is given below.

  1. public static class WebApiConfig 
  2.     { 
  3. public static void Register(HttpConfiguration config) 
  4.         { 
  5.             config.Routes.MapHttpRoute( 
  6.                 name: "DefaultApi", 
  7.                 routeTemplate: "api/{controller}/{id}", 
  8.                 defaults: new { id = RouteParameter.Optional } 
  9.             ); 
  10.             config.Routes.MapHttpRoute( 
  11.                name: "seatbook", 
  12.                routeTemplate: "api/seat/{controller}/{action}/{id}", 
  13.                defaults: new { id = RouteParameter.Optional } 
  14.            ); 
  15.             config.Routes.MapHttpRoute( 
  16.                name: "seatDetail", 
  17.                routeTemplate: "api/seatdetail/{controller}/{action}/{id}", 
  18.                defaults: new { id = RouteParameter.Optional } 
  19.            ); 
  20.             config.Routes.MapHttpRoute( 
  21.                name: "ValidateUser", 
  22.                routeTemplate: "api/ValidateUser/{controller}/{action}/{id}", 
  23.                defaults: new { id = RouteParameter.Optional } 
  24.            ); 
  25.             config.Routes.MapHttpRoute( 
  26.                name: "DeleteSeat", 
  27.                routeTemplate: "api/DeleteSeat/{controller}/{action}/{id}", 
  28.                defaults: new { id = RouteParameter.Optional } 
  29.            ); 
  30.         } 
  31.     } 

The application which we are about to build consists almost all of the above defined keywords and the initial look of the application is as shown below.

Hope it’ll help you some day. Enjoy Coding.

Wednesday, October 19, 2016

BOOKMYSEAT APPLICATION AngularJS  Asp.Net Webapi and Sql Server 2012

Here, I’ve thought to write my thoughts about hands-on Angular. This is the first article, which tells you  how to get your hands dirty with AngularJS, ASP.NET WEBAPI and SQL Server BookMySeat Application Tutorials.
This is the technology stack for BookMySeat library Application, as shown below:



I’ve designed a simple BookMySeat Library Application, where you will be familiarized with a few keywords & components, which you may be confronting in coming days. These keywords are shown below in the article-

Component of Angular Description

Module
Modules serve as containers to assist you to organize the code within your AngularJS Application. Modules can contain sub-modules.

$http
$http is an AngularJS Service for reading the data from the remote Servers.

Angular ui.bootstrap
<scriptdata-require="ui-bootstrap@*"data-semver="0.10.0"src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>

Services
Services are a point, where you can put common functionality to an AngularJS Application. For example,  if you would like to share the data with more than one controller then the best way is to promote the data to the Service and then make it available via the Service. Services extend the controllers and make them more globally accessible.

Routes Routing in Angular JS
Routes allow us to determine the ways to navigate to the specific states within our Application. It also allows us to define the configuration options for each specific route, such as which template and controller to use.

View
The view in AngularJS is what exists after AngularJS has compiled and rendered the DOM.

@scope
$scope is essentially the “glue” between the view and controller within an AngularJS Application. It supports two way binding within an Application.

Controller
The controller is to define the methods and properties that the view can bind to and interact with. Controllers should be lightweight and only focus on the view; they’re controlling.

Directive
A directive is an extension of a view in AngularJS, which allows us to create custom, reusable elements. You can also consider the directives as the decorators for your HTML. Directives are used to extend the views and to make these extensions available for use in more than one place.

Route Config
The config block of an AngularJS Application allows for the configuration to be applied before the Application actually runs. This is useful to set up routes, dynamically configuring Services and so on.

Dependency Injection
How can we inject dependency in Angular controller and module? For this sample Application, we have injected $Log,$RouteParams,$modal ,Custom Services and many more.

The Application which we are about to build consists of almost all the defined keywords, mentioned above and the initial look of the application is shown below-

There are a few points which we’ll cover in this Application. To make it complete, you can also reference how this Application will work from this BookMySeat Gif representation,

  • The screenshot, shown above, is a sample form for BookMySeat Application .You can book your seat for the specific slot. You can cancel your seat also from the given slot.
  • In the same way, we’ll try to integrate UI-bootstrap (modal Popup) to show which seat is booked by the user.
  • We will focus on Custom Directive and Custom Service.
  • Usage of $http to call to WebApi to populate the data on HTML page.
  • Usage of dependency injection at the various levels, Load partial templates to MVC view page.
  • There can be some various add ons, which you can do at your level after the completion of this Application like Email notification after booking the seat, cancel the seat, booking slot information and responsive Application .
  • The structure of the BookMySeat Application is given below-

    The basic life cycle of Angular app is given below-
Hope it’ll help you some day. Enjoy coding.