public void CreateDocument(Document document)
        {
            var xml = XDocument.Load(_path);

            var docElement = new XElement("document",
                new XElement("meta",
                    new XElement("id", document.Id),
                    new XElement("rev", document.Revision),
                    new XElement("expiration", document.Expiration),
                    new XElement("flags", document.Flags)
                ),
                new XElement("value", new XCData(document.Value))
            );

            xml.Document.Root.Add(docElement);
            xml.Save(_path);
        }
 public void CreateDocument(Document document)
 {
     throw new NotImplementedException();
 }
        public XdcrModule(IReplicationHandler handler)
        {
            this.RequiresAuthentication();

            Get["/pools"] = x =>
                {
                    var output = new
                    {
                        pools = new object[]
                        {
                            new { name = "default", uri = "/pools/default?uuid=" + UUID_POOL }
                        },
                        uuid = UUID_POOL
                    };

                    return Response.AsJson(output);
                };

            Get["pools/default"] = x =>
                {
                    var output = new
                    {
                        buckets = new { uri = "/pools/default/buckets?uuid=" + UUID_POOL },
                        nodes = new object[]
                        {
                            new
                            {
                                ports = new { direct = XDCR_PORT } ,
                                couchApiBase = string.Concat("http://", XDCR_RECEIVER, ":", XDCR_PORT, "/"),
                                hostname = string.Concat(XDCR_RECEIVER, ":", XDCR_PORT)
                            }
                        }
                    };

                    return Response.AsJson(output);
                };

            Get["pools/default/buckets"] = x =>
            {
                var output = new object[]
                {
                    new
                    {
                        bucketCapabilities = new string[] { "couchapi" },
                        bucketType = "membase",
                        nodes = new object[]
                        {
                            new
                            {
                                ports = new { direct = XDCR_PORT } ,
                                couchApiBase = string.Concat("http://", XDCR_RECEIVER, ":", XDCR_PORT, "/default"),
                                hostname = string.Concat(XDCR_RECEIVER, ":", XDCR_PORT)
                            }
                        },
                        name = XDCR_BUCKET,
                        vBucketServerMap = new
                        {
                            serverList = new string[] { string.Concat(XDCR_RECEIVER, ":", XDCR_PORT) },
                            vBucketMap = VBucketMap
                        },
                        uuid = UUID_BUCKET,
                        uri = string.Concat("/pools/default/buckets/", XDCR_BUCKET, "?bucket_uuid=", UUID_BUCKET)
                    }
                };

                return Response.AsJson(output);
            };

            Get["pools/default/buckets/{bucket}"] = x =>
            {
                var output = new object[]
                {
                    new
                    {
                        bucketCapabilities = new string[] { "couchapi" },
                        bucketType = "membase",
                        nodes = new object[]
                        {
                            new
                            {
                                ports = new { direct = XDCR_PORT } ,
                                couchApiBase = string.Concat("http://", XDCR_RECEIVER, ":", XDCR_PORT, "/default"),
                                hostname = string.Concat(XDCR_RECEIVER, ":", XDCR_PORT)
                            }
                        },
                        name = XDCR_BUCKET,
                        vBucketServerMap = new
                        {
                            serverList = new string[] { string.Concat(XDCR_RECEIVER, ":", XDCR_PORT) },
                            vBucketMap = VBucketMap
                        },
                        uuid = UUID_BUCKET,
                        uri = string.Concat("/pools/default/buckets/", XDCR_BUCKET, "?bucket_uuid=", UUID_BUCKET)
                    }
                };

                return Response.AsJson(output);
            };

            Get[REGEX_VBUCKET] = x =>
            {
                var status = getBucketExistsStatusCode(x.bucket);

                if (Request.Method == "HEAD")
                {
                    return status;
                }

                var result = new { db_name = XDCR_BUCKET };

                return status == HttpStatusCode.OK ? Response.AsJson(result) : null;
            };

            Get[REGEX_MASTER_VBUCKET] = x =>
            {
                var status = getBucketExistsStatusCode(x.bucket);

                if (Request.Method == "HEAD")
                {
                    return status;
                }

                var result = new { db_name = XDCR_BUCKET };

                return status == HttpStatusCode.OK ? Response.AsJson(result) : null;
            };

            //TODO: figure out a regex for both this pattern and master_vbucket - Sinatra version works like that
            Get[REGEX_MASTER_VBUCKET_LOCAL] = x =>
            {
                var status = getBucketExistsStatusCode(x.bucket);

                if (Request.Method == "HEAD")
                {
                    return status;
                }

                var result = new { db_name = XDCR_BUCKET };

                return status == HttpStatusCode.OK ? Response.AsJson(result) : null;
            };

            Post[REGEX_REVS_DIFF] = x =>
            {
                var body = "";
                Context.Request.Body.Position = 0;
                using (var sr = new StreamReader(Context.Request.Body))
                {
                    body = sr.ReadToEnd();
                }
                var jobj = JObject.Parse(body);

                var outDict = new Dictionary<string, object>();
                foreach (var item in jobj)
                {
                    var key = item.Key;
                    var rev = item.Value.ToString();
                    if (handler.IsMissing(key, rev))
                    {
                        outDict[key] = new { missing = rev };
                    }
                }

                return Response.AsJson(outDict);
            };

            Post[REGEX_FULL_COMMIT] = x =>
            {
                return Response.AsJson(new { ok = true }, HttpStatusCode.Created);
            };

            Post[REGEX_BULK_DOCS] = x =>
            {
                var body = "";
                Context.Request.Body.Position = 0;
                using (var sr = new StreamReader(Context.Request.Body))
                {
                    body = sr.ReadToEnd();
                }
                var jobj = JObject.Parse(body);

                var newEdits = jobj.Value<bool>("new_edits");
                var docs = jobj.Value<JArray>("docs");
                foreach (var doc in docs)
                {
                    var originalDoc = Encoding.UTF8.GetString(Convert.FromBase64String(doc.Value<string>("base64")));
                    var meta = doc["meta"] as JObject;

                    var document = new Document
                    {
                        Id = meta.Value<string>("id"),
                        Revision = meta.Value<string>("rev"),
                        Expiration = meta.Value<int>("expiration"),
                        Flags = meta.Value<int>("flags"),
                        Value = originalDoc
                    };

                    handler.CreateDocument(document);
                }

                return HttpStatusCode.Created;
            };
        }