Пример #1
0
        private Response _GetMessage(string boxName, string id)
        {
            Response ret;

            using (var db = RavenDbManager.OpenSession())
            {
                var box = db.Query <Box>().Where(x => x.Name == boxName).FirstOrDefault();

                if (box == null)
                {
                    ret = new ActionFailedResponse("Box doesn't exist", HttpStatusCode.NotFound);
                }
                else
                {
                    if (!box.Messages.ContainsKey(id))
                    {
                        ret = new ActionFailedResponse("Message doesn't exist", HttpStatusCode.NotFound);
                    }
                    else
                    {
                        ret = new SimplifiedJsonResponse(new { box.Messages[id].Body });
                    }
                }
            }

            return(ret);
        }
Пример #2
0
        private Response _CreateMessage(string boxName, string id, Message msg)
        {
            Response ret;

            using (var db = RavenDbManager.OpenSession())
            {
                var box = db.Query <Box>().Where(x => x.Name == boxName).FirstOrDefault();

                if (box == null)
                {
                    ret = new ActionFailedResponse("Box doesn't exist", HttpStatusCode.NotFound);
                }
                else
                {
                    if (box.Messages.ContainsKey(id))
                    {
                        ret = new ActionFailedResponse("Message ID Already Exists", HttpStatusCode.Conflict);
                    }
                    else
                    {
                        box.Messages.Add(id, msg);
                        db.SaveChanges();
                        ret = new SimplifiedJsonResponse(new { Result = "Message Added" });
                    }
                }
            }

            return(ret);
        }
Пример #3
0
        private Response _GetBoxList()
        {
            Dictionary <string, string> ret;

            using (var db = RavenDbManager.OpenSession())
            {
                var urlBase = Request.Url.ToUri() + "/";
                var data    = db.Query <Box>().ToList();
                ret = data.ToDictionary(x => x.Name, x => urlBase + x.Name);
            }

            return(new SimplifiedJsonResponse(ret));
        }
Пример #4
0
        private Response _CreateBox(string boxName)
        {
            //todo: validate the box name to make sure it's valid
            Response ret;

            using (var db = RavenDbManager.OpenSession())
            {
                if (db.Query <Box>().Where(x => x.Name == boxName).Any())
                {
                    ret = new ActionFailedResponse("Box already exists", HttpStatusCode.Conflict);
                }
                else
                {
                    var box = new Box(boxName);
                    db.Store(box);
                    db.SaveChanges();
                    ret = new BoxCreatedResponse(Request.Url.ToUri().ToString());
                }
            }

            return(ret);
        }
Пример #5
0
        private Response _DeleteBox(string boxName)
        {
            Response ret;

            using (var db = RavenDbManager.OpenSession())
            {
                var box = db.Query <Box>().Where(x => x.Name == boxName).FirstOrDefault();

                if (box == null)
                {
                    ret = new ActionFailedResponse("Box doesn't exist", HttpStatusCode.NotFound);
                }
                else
                {
                    db.Delete(box);
                    db.SaveChanges();
                    ret = new SimplifiedJsonResponse(new { Result = "Box Deleted" });
                }
            }

            return(ret);
        }
Пример #6
0
        private Response _GetMessagesInBox(string boxName)
        {
            Response ret;

            using (var db = RavenDbManager.OpenSession())
            {
                var box = db.Query <Box>().Where(x => x.Name == boxName).FirstOrDefault();

                if (box == null)
                {
                    ret = new ActionFailedResponse("Box doesn't exist", HttpStatusCode.NotFound);
                }
                else
                {
                    var urlBase = Request.Url.ToUri() + "/";

                    var messages = box.Messages.Keys.ToDictionary(x => x, x => urlBase + x);
                    ret = new SimplifiedJsonResponse(messages);
                }
            }

            return(ret);
        }