Example #1
0
        public override ListModel partialUpdate(int foodId, ListModel data)
        {
            using (MyBuyListEntities entities = new MyBuyListEntities())
            {
                UserShoppingList list = entities.UserShoppingLists.FirstOrDefault(item => item.USER_ID == Globals.UserId.Value && item.FOOD_ID == foodId);

                List <PropertyInfo> listProperties = list.GetType().GetProperties().ToList();

                foreach (var dataProp in data.GetType().GetProperties())
                {
                    var dataPropValue = dataProp.GetValue(data);
                    if (dataPropValue != null && dataProp.Name != "version")
                    {
                        var listProp = listProperties.Find(item => item.Name == ListMapping[dataProp.Name]);
                        if (listProp != null)
                        {
                            listProp.SetValue(list, dataPropValue);
                        }
                    }
                }

                entities.SaveChanges();

                return(new ListModel
                {
                    ownerId = Globals.UserId.Value,
                    itemId = list.FOOD_ID,
                    itemName = list.FOOD_NAME,
                    itemUnit = list.MEASUREMENT_NAME,
                    value = list.QUANTITY.Value
                });
            }
        }