Nopcommerce Update entity issue
Asked Answered
H

1

7

Using NopCommerce 3.8, Visual Studio 2015 proff.

I have created a plugin that is responsible for making restful calls to my Web API that exposes a different DB to that of Nop.

The process is run via a nop Task, it successfully pulls the data back and i can step through and manipulate as i see fit, no issues so far.

Issue comes when i try to update a record on the product table, i perform the update... but nothing happens no change, no error.

I believe this is due to the Context having no idea about my newly instantiated product object, however I'm drawing a blank on what i need to do in relation to my particular example.

Similar questions usually reference a "model" object that is part of the parameter of the method call, "model" has the method ToEntity which seems to be the answer in similar question in stack.

However my example doesn't have the ToEntity class/method possibly because my parameter is actually a list of products. To Clarify here my code.

Method in RestClient.cs

public async Task<List<T>> GetAsync()
    {
        try
        {
            var httpClient = new HttpClient();

            var json = await httpClient.GetStringAsync(ApiControllerURL);

            var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

            return taskModels;
        }
        catch (Exception e)
        {
            return null;
        }
    }

Method in my Service Class

public async Task<List<MWProduct>> GetProductsAsync()
    {
        RestClient<MWProduct> restClient = new RestClient<MWProduct>(ApiConst.Products);
        var productsList = await restClient.GetAsync();

        InsertSyncProd(productsList.Select(x => x).ToList());
        return productsList;
    }
 private void InsertSyncProd(List<MWProduct> inserted)
    {
        var model = inserted.Select(x =>
        {
            switch (x.AD_Action)
            {
                case "I":
                    //_productService.InsertProduct(row);
                    break;
                case "U":
                    UpdateSyncProd(inserted);
                  .....

Then the method to bind and update

private void UpdateSyncProd(List<MWProduct> inserted)
    {
        var me = inserted.Select(x =>
        {
            var productEnt = _productRepos.Table.FirstOrDefault(ent => ent.Sku == x.Sku.ToString());
            if(productEnt != null)
            {
                productEnt.Sku = x.Sku.ToString();
                productEnt.ShortDescription = x.ShortDescription;
                productEnt.FullDescription = x.FullDescription;
                productEnt.Name = x.Name;
                productEnt.Height = x.Pd_height != null ? Convert.ToDecimal(x.Pd_height) : 0;
                productEnt.Width = x.Pd_width != null ? Convert.ToDecimal(x.Pd_width) : 0;
                productEnt.Length = x.Pd_depth != null ? Convert.ToDecimal(x.Pd_depth) : 0;
                productEnt.UpdatedOnUtc = DateTime.UtcNow;
            }
            //TODO: set to entity so context nows and can update
            _productService.UpdateProduct(productEnt);

            return productEnt;
        });

    }

So as you can see, I get the data and pass data through to certain method based on a result. From that list in the method I iterate over, and pull the the entity from the table, then update via the product service using that manipulated entity.

So what am I missing here, I'm sure its 1 step, and i think it may be either be because 1) The context still has no idea about the entity in question, or 2) Its Incorrect calls.

Summary Update is not updating, possibly due to context having no knowledge OR my methodology is wrong. (probably both).

UPDATE:

I added some logger.inertlog all around my service, it runs through fine, all to the point of the call of update. But again I check the product and nothing has changed in the admin section.

plugin

I have provided the full source as i think maybe this has something to do with the rest of the code setup possibly?

UPDATE:

Added the following for testin on my execute method.

var myprod = _productRepos.GetById(4852);
myprod.ShortDescription = "db test";
productRepos.Update(myprod);

This successfully updates the product description. I moved my methods from my service into the task class but still no luck. The more i look at it the more im thinking that my async is killing off the db context somehow.

Hans answered 12/1, 2017 at 9:16 Comment(8)
Hey, Did you check in database directly? changes displaying over there?Catlike
Yes, nio changes where updated so i dont think its a cache issue, although who knowsHans
Okay, code looks good, need to investigate more.Catlike
Ive provided the source code in my questionHans
still nothing, Cheers for looking into this DivHans
Okay, I just checking your plugin, please let me know the test scenario..And how to set ApiControllerURL ?Catlike
api controllerurl is the url that goes to my webapi, just setup the data inside the rest client for testing, the data will just be the product model.Hans
the URL is on our network internally hosted so you cant, instead you would have to create iether your own (toomuch work headache) or instead ignore rest api, and simply call a list of models from a different class from the service.Hans
H
0

Turned of async and bound the getbyid to a new product, also removed the lambda for the switch and changed it to a foreach loop. Seems to finally update the results.

Cannot confirm if async is the culprit, currently the web api seems to be returning the same result even though the data has changed (some wierd caching by deafult in .net core? ) so im creating a new question for that.

UPDATE: It appears that the issue stems from poor debugging of async. Each instance I am trying to iterate over an await call, simply put im trying to iterate over a collection that technically may or may not be completed yet. And probably due to poor debugging, I was not aware.

So answer await your collection Then iterate after.

Hans answered 16/1, 2017 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.