Example #1
0
        private void SyncCatStckTypes(Dictionary <int, string> catStckTypes)
        {
            //1. Get existings specification attributes
            //2. Check existences
            //2b (opt) Check if names has changed
            //3. Add or udpates
            //4. Delete if necessary

            JToken[] specAtts = ParserJSon.ParseResultToJTokenList(WebService.Get(ENTITY));

            List <int> toKeep = new List <int>();

            foreach (KeyValuePair <int, string> s in catStckTypes)
            {
                JToken j = null;
                if (specAtts.Length > 0)
                {
                    j = specAtts[s.Key - 1];
                }
                if (j == null)
                {
                    //ADD
                    CAT_STCK cs = new CAT_STCK();
                    cs.NOM  = s.Value;
                    cs.TYPE = s.Key;
                    string result = WebService.Post(ENTITY, ParserJSon.ParseScatToSpecificationAttribute(cs).ToString());
                    //JToken[] resultJ = ParserJSon.ParseResultToJTokenList(result);
                    JObject jsonResult = JObject.Parse(result);
                    toKeep.Add((int)jsonResult["Id"]);

                    string gaResult = WebService.Post(WebApiEntities.GENERIC_ATTRIBUTE, ParserJSon.GetGenericAttributeJSon((int)jsonResult["Id"], ENTITY, KEY_TYPE, cs.TYPE.ToString()).ToString());

                    Console.WriteLine("Cat Stock:" + cs.NOM + " inserted");
                }
                else
                {
                    if (j["Name"].ToString() != s.Value)
                    {
                        //Update
                        JObject jo = new JObject();
                        jo.Add("Name", s.Value);
                        //jo.Add("Type", s.Key);
                        WebService.Patch(urlBuilder.Id((int)j["Id"]).BuildQuery(), jo.ToString());
                    }
                    toKeep.Add((int)j["Id"]);
                }
            }

            IEnumerable <int> idsExisting = specAtts.Select(x => (int)x["Id"]);
            IEnumerable <int> toDelete    = idsExisting.Except(toKeep);

            toDelete.ToList().ForEach(x => WebService.Delete(urlBuilder.Id(x).BuildQuery()));
        }