Ejemplo n.º 1
0
        /*
         * Constructing new list
         */
        public ListObj(string listname, string desc, string categoryname)
        {
            string name = listname;
            Dictionary <string, string> body;
            HttpResponseMessage         res;

            try
            {
                // name and category name are required
                if (name == null || categoryname == null)
                {
                    throw new Exception("Name or Category cannot be null");
                }

                // if list name exists, use a different name
                int iter = 1;
                while (true)
                {
                    if (Presence.GetList(name) != null)
                    {
                        name  = name + " (" + iter.ToString() + ")";
                        iter += 1;
                    }
                    else
                    {
                        break;
                    }
                }

                // if category doesn't already exist, create one
                string category_id = Presence.GetCategory(categoryname);
                if (category_id == null)
                {
                    body = new Dictionary <string, string> {
                        { "name", categoryname }
                    };
                    res = HttpHandler.Request(HttpMethod.Post, "/list-categories/", JsonConvert.SerializeObject(body)).Result;
                    JObject cat = JObject.Parse(res.Content.ReadAsStringAsync().Result);
                    category_id = (string)cat["_id"];
                    Presence.AddCategory(categoryname, category_id);
                }

                // create new list
                body = new Dictionary <string, string> {
                    { "name", name }, { "category", category_id }
                };
                if (desc != null)
                {
                    body.Add("description", desc);
                }
                res = HttpHandler.Request(HttpMethod.Post, "/lists/", JsonConvert.SerializeObject(body)).Result;
                ListResponse lst = JsonConvert.DeserializeObject <ListResponse>(res.Content.ReadAsStringAsync().Result);
                this.Name      = name;
                this.Id        = lst._id;
                this.PrimaryId = lst.primary_column;
                this.InUseId   = lst.inuse_column;
                foreach (var entry in lst.columns)
                {
                    if (entry.Key != this.PrimaryId && entry.Key != this.InUseId)
                    {
                        this.DescId = entry.Key;
                    }
                }
                this.FirstRow = lst.rows.Keys.First();
                Presence.AddList(name, Id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }