1 //The first step in creating this silverlight application is to create a webservice

    2 //In this demo I built on the same style of webservice that I used in the AJAX Control ToolKit so that

    3 //I might reuse the same functions with AJAX in later demos

    4 using System;

    5 using System.Collections;

    6 using System.ComponentModel;

    7 using System.Data;

    8 using System.Linq;

    9 using System.Web;

   10 using System.Web.Services;

   11 using System.Web.Services.Protocols;

   12 using System.Xml.Linq;

   13 using WEBSWAPP.Website.Data;

   14 using System.Collections.Generic;

   15 

   16 namespace Webswapp.Website.WebServices

   17     {

   18     /// <summary>

   19     /// Summary description for Locations

   20     /// </summary>

   21     [WebService(Namespace = "http://webswapp.com/")]

   22     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   23     [ToolboxItem(false)]

   24     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

   25     [System.Web.Script.Services.ScriptService]

   26     public class LocationService : System.Web.Services.WebService

   27         {

   28 

   29         /// <summary>

   30         /// Get Suggestions for the cities that match the first letters typed by the user

   31         /// </summary>

   32         /// <param name="prefixText">the first letters typed by the user</param>

   33         /// <param name="countryId">the country Id for which to select cities. It can be zero which means all countries.</param>

   34         /// <param name="count">The maximum number of records that matched the first letters passed in the prefixText</param>

   35         /// <returns>This method returns a generic list of type CityInfo</returns>

   36         [System.Web.Services.WebMethod]

   37         public List<CityInfo> GetSuggestions(string prefixText, int countryId, int count)

   38             {

   39             List<WEBSWAPP.Website.Data.CityInfo> ret = null;

   40             using (WEBSWAPP.Website.Data.SamplesDataContext db = new WEBSWAPP.Website.Data.SamplesDataContext())

   41                 {

   42                 ret = db.Locations

   43                     .Where(l => l.TypeId == 3 && l.Name.StartsWith(prefixText) &&

   44                         (countryId == 0 ? true :

   45                         l.Location1.Location1.Id == countryId))

   46                     .OrderBy(l => l.Name)

   47                     .Take(count)

   48                             .Select(l => new CityInfo()

   49                             {

   50                                 Id = l.Id,

   51                                 Name = l.Name,

   52                                 Province = l.Location1.Name,

   53                                 Country = l.Location1.Location1.Name

   54                             }).ToList();

   55                 }

   56             return ret;

   57 

   58             }

   59         /// <summary>

   60         /// Gets the city's statistical information by cityId

   61         /// </summary>

   62         /// <param name="cityId">an integer value that is a primary key for a record in the location datatable</param>

   63         /// <returns>returns a generic list of type LocationInfo</returns>

   64         [System.Web.Services.WebMethod]

   65         public List<LocationInfo> GetCityInfo(int cityId)

   66             {

   67             return WEBSWAPP.Website.Data.Location.CityInfo(cityId );

   68             }

   69 

   70 

   71         /// <summary>

   72         /// gets the province statistical information based on the city Id

   73         /// </summary>

   74         /// <param name="cityId">an integer value that is a primary key for a record in the location datatable</param>

   75         /// <returns>returns a generic list of type LocationInfo</returns>

   76         [System.Web.Services.WebMethod]

   77         public List<LocationInfo> GetProvinceInfo(int cityId)

   78             {

   79             return WEBSWAPP.Website.Data.Location.ProvinceInfo (cityId);

   80             }

   81 

   82         /// <summary>

   83         /// gets the city's statistical information based on the city Id

   84         /// </summary>

   85         /// <param name="cityId">an integer value that is a primary key for a record in the location datatable</param>

   86         /// <returns>returns a generic list of type LocationInfo</returns>

   87         [System.Web.Services.WebMethod]

   88         public List<LocationInfo> GetCountryInfo(int cityId)

   89             {

   90             return WEBSWAPP.Website.Data.Location.CountryInfo (cityId);

   91             }

   92 

   93         /// <summary>

   94         /// gets a list of all countries in WEBSWAPP website's database

   95         /// </summary>

   96         /// <returns>a generic list of type CountryInfo</returns>

   97         [System.Web.Services.WebMethod]

   98         public List<CountryInfo> GetCountries()

   99             {

  100             List<WEBSWAPP.Website.Data.CountryInfo> ret = null;

  101             using (WEBSWAPP.Website.Data.SamplesDataContext db = new WEBSWAPP.Website.Data.SamplesDataContext())

  102                 {

  103                 ret = db.Locations

  104                     .Where(l => l.TypeId == 1)

  105                     .OrderBy(l => l.Name)

  106                     .Select(l => new CountryInfo() { Id = l.Id, Name = l.Name })

  107                     .ToList();

  108                 }

  109             return ret;

  110             }

  111 

  112         /// <summary>

  113         /// gets a list of all cities that belong to a country identified by the countryId

  114         /// </summary>

  115         /// <param name="countryId">an integer that represents the primary key of a country record in the location datatable</param>

  116         /// <returns>a generic list of type CityInfo</returns>

  117         [System.Web.Services.WebMethod]

  118         public List<CityInfo> GetCities(int countryId)

  119             {

  120             List<WEBSWAPP.Website.Data.CityInfo> ret = null;

  121             using (WEBSWAPP.Website.Data.SamplesDataContext db = new WEBSWAPP.Website.Data.SamplesDataContext())

  122                 {

  123                 ret = db.Locations

  124                     .Where(l => l.TypeId == 3 && l.Location1.Location1.Id == countryId)

  125                     .OrderBy(l => l.Name)

  126                     .Select(l => new CityInfo()

  127                     {

  128                         Id = l.Id,

  129                         Name = l.Name,

  130                         Province = l.Location1.Name,

  131                         Country = l.Location1.Location1.Name

  132                     })

  133                     .ToList();

  134                 }

  135             return ret;

  136             }

  137 

  138         /// <summary>

  139         /// gets a list of all capital cities (provinicial and national) that belong to a country identified by the countryId

  140         /// </summary>

  141         /// <param name="countryId">an integer that represents the primary key of a country record in the location datatable.

  142         /// It can be zero to mean all countries</param>

  143         /// <param name="isCapital">a boolean flag to filter cities out if they were not capital cities</param>

  144         /// <returns>a generic list of type CityInfo</returns>

  145         [System.Web.Services.WebMethod]

  146         public List<CityInfo> GetCapitalCities(int countryId, bool isCapital)

  147             {

  148             List<WEBSWAPP.Website.Data.CityInfo> ret = null;

  149             using (WEBSWAPP.Website.Data.SamplesDataContext db = new WEBSWAPP.Website.Data.SamplesDataContext())

  150                 {

  151 

  152                 ret = db.Locations

  153                     .Where(l => l.TypeId == 3 &&

  154                         (countryId == 0 ? true : l.Location1.Location1.Id == countryId)

  155                         && (isCapital == (l.Location1.PropertyValues.Where(

  156                             p => p.PropertyDefintion.Name.Equals("Capital")).SingleOrDefault() != null ?

  157                             l.Location1.PropertyValues.Where(

  158                             p => p.PropertyDefintion.Name.Equals("Capital")).SingleOrDefault().Value == l.Id : false)

  159                             ||

  160                             (isCapital == (l.Location1.Location1.PropertyValues.Where(

  161                             p => p.PropertyDefintion.Name.Equals("Capital")).SingleOrDefault() != null ?

  162                             l.Location1.Location1.PropertyValues.Where(

  163                             p => p.PropertyDefintion.Name.Equals("Capital")).SingleOrDefault().Value == l.Id : false)))

  164                             )

  165                     .OrderBy(l => l.Name)

  166                     .Select(l => new CityInfo()

  167                     {

  168                         Id = l.Id,

  169                         Name = l.Name +  (l.Location1.Location1.PropertyValues.Where (

  170                             p=>p.PropertyDefintion.Name.Equals("Capital")).SingleOrDefault().Value == l.Id ?

  171                             " (Nation's Capital)":string.Empty),

  172                         Province = l.Location1.Name ,

  173                         Country = l.Location1.Location1.Name

  174 

  175                     })

  176                     .ToList();

  177 

  178                 }

  179             return ret;

  180             }

  181 

  182         }

  183     }