Moscara
Moscara

Reputation: 1

Nopcommerce Data Access Layer

In my ~/Presentation/Nop.Web/Controllers/ProductController.cs I created a method to do some filtering with googleMapsApi like this :

// GET: /Product/StoreLocatorResults
    public ActionResult StoreLocatorResults(string address)
    {
        // Make sure we have an address - if not, send user back to /Product/StoreLocator
        if (string.IsNullOrEmpty(address))
            return RedirectToAction("StoreLocator");

        // Get the lat/long info about the address
        var results = GoogleMapsAPIHelpersCS.GetGeocodingSearchResults(address);

        // Determine the lat & long parameters
        var lat = Convert.ToDecimal(results.Element("result").Element("geometry").Element("location").Element("lat").Value, NumberFormatInfo.InvariantInfo);
        var lng = Convert.ToDecimal(results.Element("result").Element("geometry").Element("location").Element("lng").Value, NumberFormatInfo.InvariantInfo);



       // var vendors = _vendorService.GetAllVendors();
        // Get those locations near the store
       TyrePromosContext context = new TyrePromosContext();
        var nearbyStores = from store in context.contextVendors
                           where Math.Abs(decimal.Parse(store.Latitude) - lat) < 0.25M &&
                                 Math.Abs(decimal.Parse(store.Longitude) - lng) < 0.25M
                           select new NearbyStoreLocation()
                           {
                               Id = store.Id,
                               VendorAddress = store.VendorAddress,
                               City = store.City,
                               PostalCode = store.PostalCode,
                               Latitude = store.Latitude,
                               Longitude = store.Longitude,
                               AddressLatitude = lat,
                               AddressLongitude = lng
                           };

        // Order the results from nearest to farthest
        var nearbySortedStores = nearbyStores.ToList().OrderBy(s => s.DistanceFromAddress).ToList();

        return View(nearbySortedStores);
    }

I created a context called TyrePromosContext which seems to connect well as I don't get any build errors and the web projects runs fine, but when an address string is parsed into StoreLocatorResults method above I get this error message

ERROR MESSAGE: An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL

Now my question is how can I use service layer to get all my vendors this way:

var vendors = _vendorService.GetAllVendors(model.SearchName, command.Page - 1, command.PageSize, true); 

like in ~Presentation/Nop.Web/Administration/Controllers/VendorController.cs ?

Upvotes: 0

Views: 355

Answers (1)

Omar Ramo
Omar Ramo

Reputation: 21

To use a service in nopCommerce you have to add an attribute for _vendor service and add it to your constructor to respect dependancy register just like this :

IVendorService _vendor;
public ProductController(IVendorService vendor)
{
    _vendor = vendor ;
}

And you can use this service in the Action that you want with _vendor attribute that you will add.

I hope that it help you. Best Regards.

Upvotes: 1

Related Questions