public void Should_Return_View_Results_As_JObject() { CouchDatabase db = client.GetDatabase(baseDatabase); db.CreateDocument(new JDocument()); db.CreateDocument(new JDocument()); db.CreateDocument(new JDocument()); JObject result = db.GetView("testviewitem", "testview", new Result <JObject>()).Wait(); Assert.IsNotNull(result); Assert.IsNotNull(result["rows"]); }
public void Should_Return_View_Results() { CouchDatabase db = client.GetDatabase(baseDatabase); db.CreateDocument(new JDocument()); db.CreateDocument(new JDocument()); db.CreateDocument(new JDocument()); ViewResult <string, JObject> result = db.GetView("testviewitem", "testview", new Result <ViewResult <string, JObject> >()).Wait(); Assert.IsNotNull(result); Assert.IsTrue(result.TotalRows > 0); }
public void BeforeEach() { couch = new Couch("localhost", 5984); // use my own routines -- even though I would be using my own code as a testing predicate, // bugs would still cause obvious failures, and I would only have to use a small slice of my code SallyFixture = new Person {Name = "Sally Acorn", Id = "sally", DocumentType = "Person"}; HayekFixture = new Person {Name = "Frederich Hayek", DocumentType = "Person"}; // hayek gets a generated Id PersonFixtures = new List<Person>() { SallyFixture, HayekFixture }; couch.EnsureDatabaseDeleted(TEST_DATABASE).Wait(); couch.EnsureDatabaseDeleted(CREATION_TEST_DATABASE).Wait(); couch.CreateDatabase(TEST_DATABASE).Wait(); var dbOpenTask = couch.OpenDatabase(TEST_DATABASE); dbOpenTask.Wait(); TestDatabase = dbOpenTask.Result; foreach (var personFixture in PersonFixtures) { TestDatabase.CreateDocument<Person>(personFixture).Wait(); } }
public T Insert <T>(T data) where T : BaseModel { var demo = _db.ObjectSerializer.Serialize(data); var a = _db.CreateDocument(data.Id.ToString(), demo); return(data); }
public static Guid?SignUp(string email, string password) { CouchDatabase db = couchClient.GetDatabase("dokuku"); Account acc = db.GetDocument <Account>(email); if (acc != null) { throw new ApplicationException(String.Format("{0} sudah terdaftar", acc._id)); } Document <Account> account = new Document <Account>(new Account { _id = email, Guid = Guid.NewGuid(), Email = email, Password = password, Roles = new string[2] { OWNER_ROLE, ADMIN_ROLE }, Type = ACCOUNT_TYPE }); db.CreateDocument(account); return(ValidateUser(email, password)); }
public static ImageAnnotation AddAnnotation(string library, Bitmap image, IBoundingBox region, JObject data) { CouchDatabase db = GetDatabase(library); string imgid = AddImage(db, image); ImageAnnotation toAdd = new ImageAnnotation(region, data, imgid); if (HasDocument(db, toAdd.Id)) { throw new InvalidOperationException("There already exists an annotation for this image with this location. Retreive that annotation and update its data accordingly."); } CouchDbAnnotation ca = new CouchDbAnnotation(); ca.data = toAdd.Data; ca.top = toAdd.Region.Top; ca.left = toAdd.Region.Left; ca.width = toAdd.Region.Width; ca.height = toAdd.Region.Height; ca.screenshotId = toAdd.ImageId; ca.type = "annotation"; Document <CouchDbAnnotation> document = new Document <CouchDbAnnotation>(ca); document.Id = toAdd.Id; db.CreateDocument(document); return(toAdd); }
public CouchDBService() { // connect to Cloudant client = new CouchClient("unieurocorn.cloudant.com", 443, "hermstaredgeshoreseembel", "36nDB1aax4CgOBgflrFgIpPU", true, AuthenticationType.Basic); database = client.GetDatabase("submissions"); var settings = new JsonSerializerSettings(); var converters = new List <JsonConverter> { new IsoDateTimeConverter() }; settings.Converters = converters; settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); settings.NullValueHandling = NullValueHandling.Ignore; var doc = new { _id = "_design/" + designDoc, Language = "javascript", Views = new { byDocType = new { Map = "function(doc) {\n emit(doc.docType, doc);\n}" } } }; var res = database.CreateDocument(doc._id, JsonConvert.SerializeObject(doc, Formatting.Indented, settings)); database.SetDefaultDesignDoc(designDoc); }
public static void ToCouchDb(RuntimeStorage intent) { CouchClient client = new CouchClient(); if (!client.HasDatabase(DATABASE)) { client.CreateDatabase(DATABASE); } CouchDatabase db = client.GetDatabase(DATABASE); CouchDbIntentData dbIntent; try{ Document doc = db.GetDocument(intent.filename); dbIntent = new CouchDbIntentData(); dbIntent.data = intent.data; Document <CouchDbIntentData> tosave = new Document <CouchDbIntentData>(dbIntent); tosave.Id = doc.Id; tosave.Rev = doc.Rev; db.SaveDocument(tosave); }catch { dbIntent = new CouchDbIntentData(); dbIntent.data = intent.data; Document <CouchDbIntentData> tosave = new Document <CouchDbIntentData>(dbIntent); tosave.Id = intent.filename; db.CreateDocument(tosave); } }
public static Guid?SignUp(string email, string password) { CouchDatabase db = couchClient.GetDatabase("dokuku"); ViewResult <Account> users = db.View <Account>("all_accounts", "view_accounts"); var userRecord = users.Items.Where(usr => usr.Email == email).FirstOrDefault(); if (userRecord != null) { throw new ApplicationException(String.Format("{0} sudah terdaftar", userRecord.Email)); } Document <Account> account = new Document <Account>(new Account { _id = Guid.NewGuid(), Email = email, Password = password, Roles = new string[2] { OWNER_ROLE, ADMIN_ROLE }, Type = ACCOUNT_TYPE }); db.CreateDocument(account); return(ValidateUser(email, password)); }
public string Set <T>(T value) where T : class { var json = JsonConvert.SerializeObject(value); var doc = database.CreateDocument(json); return(doc.GetValue("id").ToObject <string>()); }
public void Should_Return_View_Results_With_Documents() { CouchDatabase db = client.GetDatabase(baseDatabase); db.CreateDocument(new JDocument()); db.CreateDocument(new JDocument()); db.CreateDocument(new JDocument()); ViewResult <string, JObject, JDocument> result = db.GetView <string, JObject, JDocument>("testviewitem", "testview"); Assert.IsNotNull(result); foreach (ViewResultRow <string, JObject, JDocument> row in result.Rows) { Assert.IsNotNull(row.Doc); Assert.IsNotNull(row.Key); Assert.IsNotNull(row.Id); Assert.IsNotNull(row.Value); } }
public void WriteSale(Sale sale) { CouchClient couchClient = new CouchClient("192.168.1.100", 5984, null, null); CouchDatabase db = couchClient.GetDatabase(salesDB); db.CreateDocument(Serialize.ToJson(sale)); couchClient = null; }
private static CouchDatabase GetDatabase(string library) { CouchClient client = new CouchClient(); CouchDatabase db = null; if (!client.HasDatabase(library)) { client.CreateDatabase(library); JObject annotationViewsJson = JObject.Parse(File.ReadAllText(_annotationViewsFile)); JObject annotationDesignDoc = new JObject(); annotationDesignDoc["language"] = "javascript"; annotationDesignDoc["views"] = annotationViewsJson; JObject screenshotViewsJson = JObject.Parse(File.ReadAllText(_screenshotViewsFile)); JObject screenshotDesignDoc = new JObject(); screenshotDesignDoc["language"] = "javascript"; screenshotDesignDoc["views"] = screenshotViewsJson; db = client.GetDatabase(library); Document aView = new Document(annotationDesignDoc); aView.Id = "_design/annotations"; Document sView = new Document(screenshotDesignDoc); sView.Id = "_design/screenshots"; db.CreateDocument(aView); db.CreateDocument(sView); } db = client.GetDatabase(library); return(db); }
public static void UpdateDesignDocuments(CouchDatabase db) { var view = new CouchDesignDocument("explosionview"); foreach (var pair in views) { var processed = pair.Value.Replace("$MODEL_NAME", typeof(Note).Name); view.Views.Add(pair.Key, new CouchView(processed)); } db.CreateDocument(view, new Result<CouchDesignDocument>()).WhenDone((designDocument) => { Console.Out.WriteLine("Successfully written design doc!"); }, (designDocumentError) => { Console.Out.WriteLine("Hah, couldn't write design doc: " + designDocumentError.Message); }); }
public static void UpdateDesignDocuments(CouchDatabase db) { var view = new CouchDesignDocument("explosionview"); foreach (var pair in views) { var processed = pair.Value.Replace("$MODEL_NAME", typeof(Note).Name); view.Views.Add(pair.Key, new CouchView(processed)); } db.CreateDocument(view, new Result <CouchDesignDocument>()).WhenDone((designDocument) => { Console.Out.WriteLine("Successfully written design doc!"); }, (designDocumentError) => { Console.Out.WriteLine("Hah, couldn't write design doc: " + designDocumentError.Message); }); }
private Yield LoadContactsHelper(Result <ViewResult <string, string, Contact> > aResult) { Result <bool> exists = new Result <bool>(); yield return(theDatabase.DocumentExists("_design/contactview", exists)); if (exists.HasException) { aResult.Throw(exists.Exception); yield break; } if (!exists.Value) { CouchDesignDocument view = new CouchDesignDocument("contactview"); view.Views.Add("all", new CouchView( @"function(doc){ if(doc.type && doc.type == 'contact'){ emit(doc.lastName, doc.firstName) } }" )); Result <CouchDesignDocument> creationResult = new Result <CouchDesignDocument>(); yield return(theDatabase.CreateDocument(view, creationResult)); if (creationResult.HasException) { aResult.Throw(creationResult.Exception); yield break; } } var viewRes = new Result <ViewResult <string, string, Contact> >(); yield return(theDatabase.GetView("contactview", "all", viewRes)); if (viewRes.HasException) { aResult.Throw(viewRes.Exception); yield break; } aResult.Return(viewRes.Value); }
private static string AddImage(CouchDatabase db, Bitmap image) { string imgid = ImageAnnotation.GetImageId(image); if (!HasDocument(db, imgid)) { JObject imgJson = new JObject(); imgJson["_id"] = imgid; imgJson["type"] = "screenshot"; Document doc = new Document(imgJson); db.CreateDocument(doc); System.Drawing.Bitmap bmp = Bitmap.ToSystemDrawingBitmap(image); ImageConverter converter = new ImageConverter(); byte[] imgbytes = (byte[])converter.ConvertTo(bmp, typeof(byte[])); db.AddAttachment(imgid, imgbytes, "image", "image/png"); } return(imgid); }
public void Should_Get_Results_Asychronously() { string obj = @"{""test"": ""prop""}"; CouchDatabase db = client.GetDatabase(baseDatabase); db.CreateDocument("TEST", obj, new Result <string>()).Wait(); string val1 = null; string val2 = null; Result <string> res1 = new Result <string>(); Result <string> res2 = new Result <string>(); db.GetDocument("TEST", res1); db.GetDocument("TEST", res2); res2.Wait(); res1.Wait(); }
public CouchDbIntent(string name) { _intent = new RuntimeStorage(); _intent.filename = name; _intent.data = new Dictionary <string, JToken>(); CouchClient client = new CouchClient(); if (!client.HasDatabase(DATABASE)) { client.CreateDatabase(DATABASE); } CouchDatabase database = client.GetDatabase(DATABASE); CouchDbIntentData data = null; try{ Document d = database.GetDocument(name); data = JsonConvert.DeserializeObject <CouchDbIntentData>(d.ToString()); }catch { } if (data == null) { data = new CouchDbIntentData(); data.data = new Dictionary <string, JToken>(); Document <CouchDbIntentData> tosave = new Document <CouchDbIntentData>(data); tosave.Id = name; database.CreateDocument(tosave); } if (data.data != null) { foreach (var entry in data.data) { _intent.data.Add(entry.Key, entry.Value); } } }
public void Should_Trigger_Replication_using_replicator_db() { CouchDatabase replDb = client.GetDatabase("_replicator"); ICouchDocument existingDoc = replDb.GetDocument <JDocument>("C"); if (existingDoc != null) { replDb.DeleteDocument(existingDoc); } CouchReplicationDocument doc = replDb.CreateDocument( new CouchReplicationDocument { Id = "C", Source = baseDatabase, Target = String.Format("http://{0}:5984/{1}", couchdbHostName, replicateDatabase), Continuous = true, UserContext = new UserContext { Name = "bob", Roles = new[] { "role1", "role2" } } }); //Sleep two second to ensure the replicationid is set by couchdb System.Threading.Thread.Sleep(2000); CouchReplicationDocument doc2 = replDb.GetDocument <CouchReplicationDocument>("C"); Assert.IsNotEmpty(doc2.ReplicationId); Assert.IsNotEmpty(doc2.ReplicationState); Assert.IsTrue(doc2.Continuous); Assert.IsTrue(doc2.ReplicationStateTime.HasValue); Assert.IsNotNull(doc2.UserContext); Assert.AreEqual("bob", doc2.UserContext.Name); Assert.AreEqual(2, doc2.UserContext.Roles.Length); Assert.AreEqual("role1", doc2.UserContext.Roles[0]); Assert.AreEqual("role2", doc2.UserContext.Roles[1]); replDb.DeleteDocument(doc2); }
public static void Setup(TestContext o) #endif { client = new CouchClient(aHost: couchdbHostName); if (client.HasDatabase(baseDatabase)) { client.DeleteDatabase(baseDatabase); } client.CreateDatabase(baseDatabase); if (client.HasDatabase(replicateDatabase)) { client.DeleteDatabase(replicateDatabase); } client.CreateDatabase(replicateDatabase); CouchDatabase db = client.GetDatabase(baseDatabase); CouchDesignDocument view = new CouchDesignDocument("testviewitem"); view.Views.Add("testview", new CouchView("function(doc) {emit(doc._rev, doc)}")); db.CreateDocument(view); }
static DbModel GetDbModel( CouchDatabase db, Element projectInfo) { string uid = projectInfo.UniqueId; DbModel dbModel; if (db.DocumentExists(uid)) { dbModel = db.GetDocument <DbModel>(uid); Debug.Assert( dbModel.Id.Equals(projectInfo.UniqueId), "expected equal ids"); dbModel.Description = Util.ElementDescription( projectInfo); dbModel.Name = projectInfo.Document.Title; dbModel = db.UpdateDocument <DbModel>( dbModel); } else { dbModel = new DbModel(uid); dbModel.Description = Util.ElementDescription( projectInfo); dbModel.Name = projectInfo.Name; dbModel = db.CreateDocument <DbModel>(dbModel); } return(dbModel); }
private static string AddImage(CouchDatabase db, Bitmap image) { string imgid = ImageAnnotation.GetImageId(image); if (!HasDocument(db, imgid)) { JObject imgJson = new JObject(); imgJson["_id"] = imgid; imgJson["type"] = "screenshot"; Document doc = new Document(imgJson); db.CreateDocument(doc); System.Drawing.Bitmap bmp = Bitmap.ToSystemDrawingBitmap(image); ImageConverter converter = new ImageConverter(); byte[] imgbytes = (byte[])converter.ConvertTo(bmp, typeof(byte[])); db.AddAttachment(imgid, imgbytes, "image", "image/png"); } return imgid; }
/// <summary> /// Upload model, sheet, views it contains and /// their BIM elements to a CouchDB data repository. /// </summary> static public void DbUploadSheet( ViewSheet sheet, JtLoops sheetViewportLoops, SheetModelCollections modelCollections) { bool pre_existing = false; RoomEditorDb rdb = new RoomEditorDb(); CouchDatabase db = rdb.Db; // Sheet Document doc = sheet.Document; Element e = GetProjectInfo(doc); DbModel dbModel = GetDbModel(db, e); DbSheet dbSheet = rdb.GetOrCreate <DbSheet>( ref pre_existing, sheet.UniqueId); dbSheet.Description = Util.SheetDescription(sheet); dbSheet.Name = sheet.Name; dbSheet.ModelId = e.UniqueId; dbSheet.Width = sheetViewportLoops[0].BoundingBox.Width; dbSheet.Height = sheetViewportLoops[0].BoundingBox.Height; dbSheet = pre_existing ? db.UpdateDocument <DbSheet>(dbSheet) : db.CreateDocument <DbSheet>(dbSheet); // Symbols Dictionary <ElementId, GeomData> geometryLookup = modelCollections.Symbols; foreach (KeyValuePair <ElementId, GeomData> p in geometryLookup) { ElementId id = p.Key; e = doc.GetElement(id); DbSymbol symbol = rdb.GetOrCreate <DbSymbol>( ref pre_existing, e.UniqueId); symbol.Description = Util.ElementDescription(e); symbol.Name = e.Name; symbol.Loop = p.Value.Loop.SvgPath; symbol = pre_existing ? db.UpdateDocument <DbSymbol>(symbol) : db.CreateDocument <DbSymbol>(symbol); } // Views and BIM elements List <ViewData> views = modelCollections .ViewsInSheet[sheet.Id]; View view; DbView dbView; DbBimel dbBimel; DbInstance dbInstance = null; DbPart dbPart = null; JtBoundingBox2dInt bbFrom; JtBoundingBox2dInt bbTo; foreach (ViewData viewData in views) { ElementId vid = viewData.Id; if (!modelCollections.BimelsInViews .ContainsKey(vid)) { // This is not a floor plan view, so // we have nothing to display in it. continue; } view = doc.GetElement(vid) as View; dbView = rdb.GetOrCreate <DbView>( ref pre_existing, view.UniqueId); dbView.Description = Util.ElementDescription(view); dbView.Name = view.Name; dbView.SheetId = dbSheet.Id; bbFrom = viewData.BimBoundingBox; bbTo = viewData.ViewportBoundingBox; dbView.X = bbTo.Min.X; dbView.Y = bbTo.Min.Y; dbView.Width = bbTo.Width; dbView.Height = bbTo.Height; dbView.BimX = bbFrom.Min.X; dbView.BimY = bbFrom.Min.Y; dbView.BimWidth = bbFrom.Width; dbView.BimHeight = bbFrom.Height; dbView = pre_existing ? db.UpdateDocument <DbView>(dbView) : db.CreateDocument <DbView>(dbView); // Retrieve the list of BIM elements // displayed in this view. List <ObjData> bimels = modelCollections .BimelsInViews[vid]; foreach (ObjData bimel in bimels) { e = doc.GetElement(bimel.Id); InstanceData inst = bimel as InstanceData; if (null != inst) { dbInstance = rdb.GetOrCreate <DbInstance>( ref pre_existing, e.UniqueId); dbInstance.SymbolId = doc.GetElement( inst.Symbol).UniqueId; dbInstance.Transform = inst.Placement .SvgTransform; dbBimel = dbInstance; } else { Debug.Assert(bimel is GeomData, "expected part with geometry"); dbPart = rdb.GetOrCreate <DbPart>( ref pre_existing, e.UniqueId); dbPart.Loop = ((GeomData)bimel).Loop .SvgPath; dbBimel = dbPart; } dbBimel.Description = Util.ElementDescription(e); dbBimel.Name = e.Name; JtUidSet uids = new JtUidSet(dbBimel.ViewIds); uids.Add(view.UniqueId); dbBimel.ViewIds = uids.Uids; dbBimel.Properties = Util.GetElementProperties(e); if (null != inst) { dbInstance = pre_existing ? db.UpdateDocument <DbInstance>(dbInstance) : db.CreateDocument <DbInstance>(dbInstance); } else { dbPart = pre_existing ? db.UpdateDocument <DbPart>(dbPart) : db.CreateDocument <DbPart>(dbPart); } } } }