allBlogsList

Extending Insite using Web Services

Insite 4 was revamped to display data using AngularJS. The data is pulled from Insite using webservices. To meet the business needs the webservices need to be extended. In the following example I am going to show how to extend the dealer webservice. The webservice will return what product is available for each dealer.

Create a class that inherits the Insite.Dealers.WebApi.V1.Mappers.GetDealMapperer class. The GetDealer class is used by the Insite Dealer webservices to return the dealer information.

using Insite.Core.Common.Interfaces;
using Insite.Core.WebApi.Interfaces;
using Insite.Dealers.Services;
using Insite.Dealers.Services.Results;
using Insite.Dealers.WebApi.V1.ApiModels;
using Insite.Dealers.WebApi.V1.Mappers;
using InSite.Model;
using InSite.Model.Core;
using InSite.Model.Interfaces;
using System;
using System.Linq;
using System.Net.Http;

namespace InSiteCommerce.Web.BL
{
    public class CustomGetDealerMappernamespace : GetDealerMapper
    {
        protected IUnitOfWork UnitOfWork { get; set; }

        public CustomGetDealerMappernamespace(IObjectToObjectMapper objectToObjectMapper, IUrlHelper urlHelper, IUnitOfWork unitOfWork)
            : base(objectToObjectMapper, urlHelper)
        {
            this.UnitOfWork = unitOfWork;
        }
    }
}

We are going to override the MapResult function for GetDealerMapper. This will allow us to add additional properties to the dealer web service request.

        public override DealerModel MapResult(GetDealerResult serviceResult, HttpRequestMessage request, CurrentContext currentContext)
        {
        }

The first step in the MapResult will be calling the base function. This will allow us to still use the default Insite functionality. In addition this approach will minimize any issues when upgrading Insite to a newer version.

        public override DealerModel MapResult(GetDealerResult serviceResult, HttpRequestMessage request, CurrentContext currentContext)
        {
            DealerModel model = base.MapResult(serviceResult, request, currentContext);

            return model;
        }

We need to lookup what products are sold by the dealer. To find the products for each dealer we will use UnitOfWork.GetRepository().GetTable(). This method will allow the database request to be cached and minimized multiple trips to the database. To return only products are linked to the current dealer we use the where clause and model.DealerID.

            List products = UnitOfWork.GetRepository().GetTable().Where(p => p.Dealers.Where(v => v.Id == model.DealerId).Count() > 0).ToList();

Once we have the list of products we concat the list into a single string using String.Join. Below is the completed function for MapResult.

        public override DealerModel MapResult(GetDealerResult serviceResult, HttpRequestMessage request, CurrentContext currentContext)
        {
            DealerModel model = base.MapResult(serviceResult, request, currentContext);
            List products = UnitOfWork.GetRepository().GetTable().Where(p => p.Dealers.Where(v => v.Id == model.DealerId).Count() > 0).ToList();

            string productNames = String.Join(", ", products.Select(p => p.Name).ToArray());

            model.Properties.Add("productNames", productNames);

            return model;
        }

The last step is to update the angular page to display the Products. To do this we need to modify \Themes\Responsive\Views\Directives\Dealers\DealerLocatorView.cshtml. At line 58 we will need to add the following code.

Product: {{dealer.properties.productNames}}						
						

Below is the code to display the dealer on the webpage.

{{dealer.address1}}

{{dealer.address2}}

{{dealer.city}}, {{dealer.state}} {{dealer.zip}}

{{dealer.phone1}}

					
Product: {{dealer.properties.productNames}}					

The above example shows how to extend the web services to return additional data. This will allow us to meet the business needs.