Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST multiple products to .../api/products not wieling? #226

Open
oeloe100 opened this issue Jul 17, 2020 · 6 comments
Open

POST multiple products to .../api/products not wieling? #226

oeloe100 opened this issue Jul 17, 2020 · 6 comments

Comments

@oeloe100
Copy link

Hi There,

Im not sure of this is the right way or place To adress my problem. Ive also poster this question on nopcommerce forum without success.

POSTING product data from eighteir my application or Postman to the Web API endpoint only Works for one single product.

My question is, how to map multiple products at once?

@oeloe100
Copy link
Author

Fixed it using multiple Tasks (for every POST single task)

@champcbg
Copy link

champcbg commented Aug 25, 2020

@oeloe100 can you expand on ur answer that solved the problem. I am having the same issue when posting multi items to an order.

@oeloe100
Copy link
Author

Hi there @champcbg ,

you could create a simple task like:

public async Task UpdateOrder(... data)
{
await (httpclient).PostAsync(requestUri, data);
}

@champcbg
Copy link

Ahhhh. ok. you were referring to the client application.

Thats doesnt help me. Thanks for the feedback though

@oeloe100
Copy link
Author

So what problem do you have? meby i can help?

@champcbg
Copy link

The problem i am having is when trying to post an order with multiple items on the order.

What happens is and order with X number of order items is posted to the order API, the order only gets created with 1 order item. Its the last order item in the list of items.

Debugging the issue, I know it is a issue in the model binding. The order resource gets the order the correct way with X number of order items. when it comes out of the model Binding to build the Delta there is only 1 order item.

I think this is the problem method


        private void AddOrUpdateComplexItemInCollection(Dictionary<string, object> newProperties, IList collection, Type collectionElementsType,
            Dictionary<object, object> objectPropertyNameValuePairs, bool handleComplexTypeCollections)
        {
            if (newProperties.ContainsKey("Id"))
            {
                // Every element in collection, that is not System type should have an id.
                var id = int.Parse(newProperties["Id"].ToString());

                object itemToBeUpdated = null;

                // Check if there is already an item with this id in the collection.
                foreach (var item in collection)
                {
                    if (int.Parse(item.GetType()
                        .GetProperty("Id", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
                        .GetValue(item)
                        .ToString()) == id)
                    {
                        itemToBeUpdated = item;
                        break;
                    }
                }

                if (itemToBeUpdated == null)
                {
                    // We should create a new item and put it in the collection.
                    AddNewItemInCollection(newProperties, collection, collectionElementsType, objectPropertyNameValuePairs, handleComplexTypeCollections);
                }
                else
                {
                    // We should update the existing element.
                    SetValues(newProperties, itemToBeUpdated, collectionElementsType, objectPropertyNameValuePairs, handleComplexTypeCollections);
                }
            }
            // It is a new item.
            else
            {
                AddNewItemInCollection(newProperties, collection, collectionElementsType, objectPropertyNameValuePairs, handleComplexTypeCollections);
            }
        }

I am just getting familiar with the code, but I think what is happening is after the 1st item gets added to the collection, every item that comes through after that just updates that item, a new item is not being added to the collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants