Ejemplo n.º 1
0
        //http://127.0.0.1:8888?model=test&action=getbyid&_id=5744a604f1fa4b04a82cb94a
        public static string getbyid(Message m)
        {
            string json = "{}";

            try
            {
                var    jobject = JsonConvert.DeserializeObject <JObject>(m.input);
                string id      = jobject.getValue(_LITEDB_CONST.FIELD_ID);

                if (id.Length == 24)
                {
                    IDB db = dbi.Get(m.model);
                    {
                        var result = db.FindById(id);
                        json = @"{""ok"":true,""total"":" + db.Count().ToString() + @",""count"":" + (result == null ? "0" : "1") + @", ""items"":" + (result == null ? "null" : result.toJson) + @"}";
                    }
                }
                else
                {
                    json = JsonConvert.SerializeObject(new { ok = false, output = "The field _id should be 24 hex characters" });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new { ok = false, output = ex.Message });
            }
            return(json);
        }
Ejemplo n.º 2
0
        //http://127.0.0.1:8888/?model=test&action=fetch
        //http://127.0.0.1:8888/?model=test&action=fetch&skip=0&limit=10
        public static string fetch(Message m)
        {
            var    jobject = JsonConvert.DeserializeObject <JObject>(m.input);
            string json    = @"{""ok"":false,""total"":-1,""count"":-1,""msg"":""Can not find model [" + m.model + @"]""}";
            string skip    = jobject.getValue("skip");
            string limit   = jobject.getValue("limit");

            long _skip  = 0;
            long _limit = 0;

            long.TryParse(skip, out _skip);
            long.TryParse(limit, out _limit);

            if (_skip < 0)
            {
                _skip = _LITEDB_CONST._SKIP;
            }
            if (_limit <= 0)
            {
                _limit = _LITEDB_CONST._LIMIT;
            }

            IDB db = dbi.Get(m.model);

            if (db != null)
            {
                var result = db.Fetch(_skip, _limit).Select(x => x.toJson).ToArray();
                json = @"{""ok"":true,""total"":" + db.Count().ToString() + @",""count"":" + result.Length.ToString() + @",""items"":[" + string.Join(",", result) + @"]}";
            }
            return(json);
        }
Ejemplo n.º 3
0
        private static long _total(string model)
        {
            IDB db = Get(model);

            if (db != null)
            {
                return(db.Count());
            }
            return(0);
        }
Ejemplo n.º 4
0
        //http://127.0.0.1:8888/?model=test&action=count
        public static string count(Message m)
        {
            string json = @"{""ok"":false,""model"":""" + m.model + @""",""count"":-1}";
            IDB    db   = dbi.Get(m.model);

            if (db != null)
            {
                json = @"{""ok"":true,""model"":""" + m.model + @""",""count"":" + db.Count().ToString() + @"}";
            }
            return(json);
        }
Ejemplo n.º 5
0
        // [{ "model":"test", "action":"insert", "data":[{"key":"value1", "key2":"tiếng việt"}] }]
        public static string insert(Message m)
        {
            if (m.method != "POST")
            {
                return(JsonConvert.SerializeObject(new { ok = false, total = 0, count = 0, msg = "The method action [" + m.action + "] for model [" + m.model + "] must be POST" }));
            }

            string json = "{}";

            var ips = JsonConvert.DeserializeObject <JObject[]>(m.input);

            if (ips.Length > 0)
            {
                IDB db = dbi.Get(m.model);
                if (db != null)
                {
                    string[] IDs = db.InsertBulk(ips.convertBsonDocument(true));
                    json = @"{""ok"":true,""total"":" + db.Count().ToString() + @",""count"":" + IDs.Length.ToString() + @",""items"":" + JsonConvert.SerializeObject(IDs) + @"}";
                }
            }

            return(json);
        }
Ejemplo n.º 6
0
        //http://127.0.0.1:8888?model=test&action=getbyid&_id=5744a604f1fa4b04a82cb94a
        public static string removebyid(Message m)
        {
            if (m.method != "POST")
            {
                return(JsonConvert.SerializeObject(new { ok = false, total = 0, count = 0, msg = "The method action [" + m.action + "] for model [" + m.model + "] must be POST" }));
            }

            string json = "{}";

            try
            {
                string[] ids = new string[] { };
                try
                {
                    ids = JsonConvert.DeserializeObject <string[]>(m.input);
                }
                catch (Exception ex)
                {
                    return(JsonConvert.SerializeObject(new { ok = false, output = "The IDs is invalid format JSON" }));
                }

                if (ids.Length > 0)
                {
                    IDB db = dbi.Get(m.model);
                    {
                        string        id   = string.Empty;
                        List <string> lsOk = new List <string>()
                        {
                        };
                        List <string> lsFail = new List <string>()
                        {
                        };
                        for (int i = 0; i < ids.Length; i++)
                        {
                            id = ids[i];
                            if (id.Length == 24)
                            {
                                bool result = db.RemoveById(id);
                                if (result)
                                {
                                    lsOk.Add(id);
                                }
                                else
                                {
                                    lsFail.Add(id);
                                }
                            }
                            else
                            {
                                lsFail.Add(id);
                            }
                            //json = JsonConvert.SerializeObject(new { ok = false, output = "The field _id should be 24 hex characters" });
                        }
                        json = @"{""ok"":true,""total"":" + db.Count().ToString() + @",""remove"":{""ok"":" +
                               JsonConvert.SerializeObject(lsOk) + @", ""fail"":" + JsonConvert.SerializeObject(lsFail) + @"}}";
                    }
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new { ok = false, output = ex.Message });
            }
            return(json);
        }