public T Update <T>(long id, T data) where T : BaseModel { var doc = _db.GetDocument <T>(id.ToString()); var serialized = _db.ObjectSerializer.Serialize(data); _db.SaveDocument(new Document(serialized)); return(doc); }
public void Store(Summoner summoner) { var existing = _summoners.GetDocument <Summoner>(summoner.Id); if (existing != null) { existing = UpdateExisting(existing, summoner); } var result = _summoners.SaveDocument(new Document <Summoner>(existing)); }
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"); 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 TDocument GetOrCreate <TDocument>( ref bool pre_existing, string uid) where TDocument : DbObj { //return _db.GetDocument<TDocument>( uid ); TDocument doc; if (_db.DocumentExists(uid)) { pre_existing = true; doc = _db.GetDocument <TDocument>(uid); Debug.Assert( doc.Id.Equals(uid), "expected equal ids"); } else { pre_existing = false; doc = (TDocument)Activator.CreateInstance( typeof(TDocument), uid); } return(doc); }
public static void Delete(string library, string id) { CouchDatabase db = GetDatabase(library); Document ca = db.GetDocument(id); db.DeleteDocument(ca.Id, ca.Rev); }
public void UpdateIfNecessary() { var repo = new CouchRepository <DesignDoc>(db); foreach (var file in Directory.GetFiles(this.filePath)) { //get the hash of the file. Compare against the hash on the design doc. If they don't match then update. string designId = "_design/" + Path.GetFileNameWithoutExtension(file); string contents = File.ReadAllText(file); JObject obj = JObject.Parse(contents); string hash = Sha1Util.Sha1HashStringForUtf8String(contents); var doc = db.GetDocument <DesignDoc>(designId); if (doc != null) { db.DeleteDocument(doc.Id, doc.Rev); } var newDoc = new DesignDoc { Id = designId, Hash = hash, Views = obj }; repo.Save(newDoc); return; } }
private void OnContactChanged(CouchChangeResult aChangeResult) { //add to the list of changes Items.Add(aChangeResult); //record the sequence number of the change so when we run the application we //tell that we only need changes > this number. //(Otherwise you get a change message for every doc in the db at the startup) SetSequence(aChangeResult.Sequence); //verifiy type of change announced so we know if we need to refresh a contact if (aChangeResult.Id.StartsWith("_design")) { return; } if (aChangeResult.Deleted) { OnContactDeleted(aChangeResult.Id); } else { Console.WriteLine("Retrieving last version of {0}", aChangeResult.Id); //get the latest version of this document theDatabase.GetDocument(aChangeResult.Id, new Result <Contact>()).WhenDone( a => BeginInvoke((MethodInvoker)(() => OnContactChanged(a))), ErrorManagement.ProcessException ); } }
public Item GetItem(string name) { CouchClient couchClient = new CouchClient("192.168.1.100", 5984, null, null); CouchDatabase db = couchClient.GetDatabase(itemsDB); JDocument doc = db.GetDocument <JDocument>(name); return(Item.FromJson(doc.ToString())); }
public Account GetAccount(string email) { CouchClient couchClient = new CouchClient("192.168.1.100", 5984, null, null); CouchDatabase db = couchClient.GetDatabase(accountsDB); JDocument doc = db.GetDocument <JDocument>(email); return(Account.FromJson(doc.ToString())); }
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 static AccountUser GetAccountByUsername(string username) { CouchDatabase db = couchClient.GetDatabase("dokuku"); AccountUser accUser = db.GetDocument <AccountUser>(username); if (accUser == null) { accUser.CompanyId = username; } return(accUser); }
public JObject GetDocument(string id) { if (string.IsNullOrEmpty(id)) { throw new System.ArgumentNullException("id"); } Document doc = couchDB.GetDocument(id); return(doc); }
public static Guid?ValidateUser(string email, string password) { CouchDatabase db = couchClient.GetDatabase("dokuku"); Account acc = db.GetDocument <Account>(email); if (acc == null) { return(null); } return(acc.Guid); }
public static IUserIdentity GetUserFromUsername(string username) { CouchDatabase db = couchClient.GetDatabase("dokuku"); Account acc = db.GetDocument <Account>(username); return(acc == null ? null : new UserIdentity { UserName = acc._id, Claims = acc.Roles }); }
/// <summary> /// Get's the user. /// </summary> /// <param name="aUserId"></param> /// <returns></returns> public JDocument GetUser(string aUserId) { if (String.IsNullOrEmpty(aUserId)) { throw new ArgumentException("aUser cannot be null nor empty"); } var db = new CouchDatabase(BasePlug.At("_users")); aUserId = "org.couchdb.user:" + HttpUtility.UrlEncode(aUserId); return(db.GetDocument(aUserId, new Result <JDocument>()).Wait()); }
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 ImageAnnotation GetAnnotation(string library, Bitmap image, IBoundingBox region) { CouchDatabase db = GetDatabase(library); string imgid = ImageAnnotation.GetImageId(image); ImageAnnotation ia = new ImageAnnotation(region, null, imgid); try { CouchDbAnnotation ca = db.GetDocument <CouchDbAnnotation>(ia.Id); return(new ImageAnnotation(region, ca.data, imgid)); } catch { return(null); } }
public void UpdateAccount(Account account) { CouchClient couchClient = new CouchClient("192.168.1.100", 5984, null, null); string docId = account.Email; CouchDatabase db = couchClient.GetDatabase(accountsDB); JDocument doc = db.GetDocument <JDocument>(docId); JDocument newDoc = new JDocument(Serialize.ToJson(account)); newDoc.Rev = doc.Rev; db.UpdateDocument(newDoc); couchClient = null; }
private static bool HasDocument(CouchDatabase db, string id) { Document d = null; try { d = db.GetDocument(id); } catch { } if (d == null) { return(false); } return(true); }
public void UpdateItem(Item item) { CouchClient couchClient = new CouchClient("192.168.1.100", 5984, null, null); string docId = item.Name; CouchDatabase db = couchClient.GetDatabase(itemsDB); JDocument doc = db.GetDocument <JDocument>(docId); JDocument newDoc = new JDocument(Serialize.ToJson(item)); newDoc.Rev = doc.Rev; db.UpdateDocument(newDoc); couchClient = null; }
public List <Item> GetItems() { List <Item> items = new List <Item>(); CouchClient couchClient = new CouchClient("192.168.1.100", 5984, null, null); CouchDatabase db = couchClient.GetDatabase(itemsDB); JObject viewResult = db.GetView("itemsDesignDoc", "getAllItems"); foreach (JObject doc in viewResult.GetValue("rows")) { JDocument itemstDoc = db.GetDocument <JDocument>((doc.GetValue("id").ToString())); items.Add(Item.FromJson(itemstDoc.ToString())); } couchClient = null; return(items); }
public List <Account> GetAccounts() { List <Account> accounts = new List <Account>(); CouchClient couchClient = new CouchClient("192.168.1.100", 5984, null, null); CouchDatabase db = couchClient.GetDatabase(accountsDB); JObject viewResult = db.GetView("accountsDesignDoc", "getAllAccounts"); foreach (JObject doc in viewResult.GetValue("rows")) { JDocument accountDoc = db.GetDocument <JDocument>((doc.GetValue("id").ToString())); accounts.Add(Account.FromJson(accountDoc.ToString())); } couchClient = null; return(accounts); }
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 static ImageAnnotation UpdateExisting(string library, string id, JObject data) { CouchDbAnnotation toUpdate = null; CouchDatabase db = GetDatabase(library); try { toUpdate = db.GetDocument <CouchDbAnnotation>(id); } catch { return(null); } toUpdate.data = data; Document <CouchDbAnnotation> document = new Document <CouchDbAnnotation>(toUpdate); db.SaveDocument(document); return(CouchDbAnnotationToImageAnnotation(toUpdate)); }
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); }
public override IReconcilingDocument GetDatabaseCopy(CouchDatabase db) { return db.GetDocument<Car>(Id); }
/// <summary> /// Get's the user. /// </summary> /// <param name="userId"></param> /// <returns></returns> public Document GetUser(string userId) { var db = new CouchDatabase(baseUri, "_users", username, password, this.authType); userId = "org.couchdb.user:" + HttpUtility.UrlEncode(userId); return db.GetDocument(userId); }
public virtual IReconcilingDocument GetDatabaseCopy(CouchDatabase db) { return db.GetDocument<CouchDocument>(Id); }
public T Get <T>(string id) where T : class { var doc = database.GetDocument(id); return(doc != null?doc.ToObject <T>() : null); }
/// <summary> /// Get's the user. /// </summary> /// <param name="userId"></param> /// <returns></returns> public Document GetUser(string userId) { var db = new CouchDatabase(couchConnection); userId = "org.couchdb.user:" + HttpUtility.UrlEncode(userId); return db.GetDocument(userId); }
public virtual T Find(Guid id) { return(db.GetDocument <T>(id.ToString())); }
/// <summary> /// Get's the user. /// </summary> /// <param name="aUserId"></param> /// <returns></returns> public JDocument GetUser(string aUserId) { if (String.IsNullOrEmpty(aUserId)) throw new ArgumentException("aUser cannot be null nor empty"); var db = new CouchDatabase(BasePlug.At("_users")); aUserId = "org.couchdb.user:" + HttpUtility.UrlEncode(aUserId); return db.GetDocument(aUserId, new Result<JDocument>()).Wait(); }
private static bool HasDocument(CouchDatabase db, string id) { Document d = null; try { d = db.GetDocument(id); } catch { } if (d == null) return false; return true; }