Example #1
0
        // Template for testing: {"subMenu_Id":"583cb590422ce433e4abec81","items":[{"Id":"ABCD"},{"Id":"EFGH"},{"Id":"IJKL"}]}
        public string updateSubMenuDishList(string json)
        {
            // Create a temoplate of Json i get from Lior
            dynamic jsonObjectItems = new JObject();
            dynamic jsonObjectID1   = new JObject();

            jsonObjectID1.Id = "aa";
            dynamic jsonObjectID2 = new JObject();

            jsonObjectID2.Id = "bb";
            dynamic jsonObjectID3 = new JObject();

            jsonObjectID3.Id = "cc";

            JArray idList = new JArray();

            idList.Add(jsonObjectID1);
            idList.Add(jsonObjectID2);
            idList.Add(jsonObjectID3);
            //jsonObjectItems.subMenu_Id = subMenu_Id;
            jsonObjectItems.items = idList;

            // Beginning of actual method

            string        updateDishesJsonString = JsonConvert.SerializeObject(jsonObjectItems);
            dynamic       updateDishesJsonObj    = JsonConvert.DeserializeObject(json);
            List <string> dishesList             = new List <string>(); // A list to hold all new updated dishes

            foreach (var obj in updateDishesJsonObj.items)              // for every dish in under items key in updateDishesJsonObj
            {
                dishesList.Add((string)obj.Id);                         // add the id to the dishesList
            }

            string[] dishesArray = dishesList.ToArray();
            //return dishesArray[2];

            JsonToWebServer finalJsonObj = new JsonToWebServer();
            string          subMenu_Id   = (string)updateDishesJsonObj.subMenu_Id; // extract the subMenu_Id from the json string argument

            try
            {
                subMenuCollection.UpdateOneAsync(
                    Builders <SubMenu> .Filter.Eq("_id", new ObjectId(subMenu_Id)),
                    Builders <SubMenu> .Update.Set("dishArray", dishesArray));

                SubMenu newEditedSubMenu = subMenuCollection.AsQueryable().First(sm => sm._id == new ObjectId(subMenu_Id));
                finalJsonObj.items = newEditedSubMenu;
            }
            catch (Exception ex)
            {
                finalJsonObj.status = "Fail";
                finalJsonObj.reason = ex.Message;
            }
            string finalJson = JsonConvert.SerializeObject(finalJsonObj);

            return(finalJson);
        }
        // This method get a SubMenu Id and  SubMenu name
        // and update the corrsponding entry of that subMenuId in the SubMenu collection in DB
        // All fields will be updated (except the subMenuId and menuId)
        public string editSubMenuBySubMenuId(string subMenu_Id, string name)
        {
            JsonToWebServer finalJsonObj = new JsonToWebServer();

            try
            {
                subMenuCollection.UpdateOneAsync(
                    Builders <SubMenu> .Filter.Eq("_id", new ObjectId(subMenu_Id)),
                    Builders <SubMenu> .Update.Set("name", name));

                SubMenu newEditedSubMenu = subMenuCollection.AsQueryable().First(sm => sm._id == new ObjectId(subMenu_Id));
                finalJsonObj.items = newEditedSubMenu;
            }
            catch (Exception ex)
            {
                finalJsonObj.status = "Fail";
                finalJsonObj.reason = ex.Message;
            }
            string finalJson = JsonConvert.SerializeObject(finalJsonObj);

            return(finalJson);
        }