public async Task<StoreResponse> Store(string bucketName, SiaqodbDocument document) { var db = client.GetDatabase(DbName); var collection = db.GetCollection<BsonDocument>(bucketName); var newDoc = Mapper.ToBsonDoc(document); newDoc["_rev"] = this.GenerateNewVersion(); try { var result = await collection.ReplaceOneAsync(filter: new BsonDocument { { "_id", document.Key }, { "_rev", BsonTypeMapper.MapToBsonValue(document.Version) } }, options: new UpdateOptions { IsUpsert = true }, replacement: newDoc); var cnorResponse = new StoreResponse(); cnorResponse.Version = newDoc["_rev"].AsString; cnorResponse.Key = document.Key; return cnorResponse; } catch (MongoWriteException ex) { if (ex.Message.Contains("duplicate key"))//conflict { throw new ConflictException("There is a document with the same key and another version already stored"); } throw ex; } }
public async Task<StoreResponse> Store(string bucketName, SiaqodbDocument document) { using (var client = new MyCouchClient(DbServerUrl, bucketName)) { await CheckTagsViews(client, bucketName, document.Tags); CouchDBDocument doc = Mapper.ToCouchDBDoc(document); var serializedObj = client.Serializer.Serialize<CouchDBDocument>(doc); var response = await client.Documents.PostAsync(serializedObj); if (response.IsSuccess) { var cnorResponse = new StoreResponse(); cnorResponse.Version = response.Rev; cnorResponse.Key = response.Id; await this.StartRebuildViews(client, document); return cnorResponse; } else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { throw new BucketNotFoundException(bucketName); } else if (response.StatusCode == System.Net.HttpStatusCode.Conflict) { throw new ConflictException(response.Reason); } else throw new GenericCouchDBException(response.Reason, response.StatusCode); } }