Skip to content
Browse files

Changed Save and Create methods to return a JObject with the response…

… from CouchDB instead of having to refetch the Document and incurring another request.

Also added JProperties attributes so that Document will Deserialize propertly from a string with out string replacement.
  • Loading branch information...
1 parent af945a6 commit af791ecc8cee46eb658e671eb13e977ea9bb9e99 @soitgoes soitgoes committed Sep 25, 2011
View
34 LoveSeat.IntegrationTest/CouchClientTest.cs
@@ -36,19 +36,16 @@ public void Setup()
[TestFixtureTearDown]
public void TearDown()
{
- //delete the test database
- if (client.HasDatabase(baseDatabase))
- {
- client.DeleteDatabase(baseDatabase);
- }
- if (client.HasDatabase(replicateDatabase))
- {
+ //delete the test database
+ if (client.HasDatabase(baseDatabase)) {
+ client.DeleteDatabase(baseDatabase);
+ }
+ if (client.HasDatabase(replicateDatabase)) {
client.DeleteDatabase(replicateDatabase);
}
- if (client.HasUser("Leela"))
- {
- client.DeleteAdminUser("Leela");
- }
+ if (client.HasUser("Leela")) {
+ client.DeleteAdminUser("Leela");
+ }
}
[Test]
@@ -63,7 +60,8 @@ public void Should_Create_Document_From_String()
string obj = @"{""test"": ""prop""}";
var db = client.GetDatabase(baseDatabase);
var result = db.CreateDocument("fdas", obj);
- Assert.IsNotNull(db.GetDocument("fdas"));
+ var document = db.GetDocument("fdas");
+ Assert.IsNotNull(document);
}
[Test]
public void Should_Save_Existing_Document()
@@ -73,7 +71,8 @@ public void Should_Save_Existing_Document()
var result = db.CreateDocument("fdas", obj);
var doc = db.GetDocument("fdas");
doc["test"] = "newprop";
- var newresult = db.SaveDocument(doc);
+ db.SaveDocument(doc);
+ var newresult= db.GetDocument("fdas");
Assert.AreEqual(newresult.Value<string>("test"), "newprop");
}
@@ -165,6 +164,15 @@ public void Should_Get_304_If_ETag_Matches()
ViewResult cachedResult = db.GetAllDocuments(new ViewOptions {Etag = result.Etag});
Assert.AreEqual(cachedResult.StatusCode, HttpStatusCode.NotModified);
}
+ [Test]
+ public void Should_Get_Id_From_Existing_Document()
+ {
+ var db = client.GetDatabase(baseDatabase);
+ string id = "test_should_get_id";
+ db.CreateDocument("{\"_id\":\""+ id+"\"}");
+ Document doc= db.GetDocument(id);
+ Assert.AreEqual(id, doc.Id);
+ }
}
public class Company
{
View
20 LoveSeat/CouchDatabase.cs
@@ -28,7 +28,7 @@ internal CouchDatabase(string baseUri, string databaseName, string username, str
/// <param name="id">Id of Document</param>
/// <param name="jsonForDocument"></param>
/// <returns></returns>
- public Document CreateDocument(string id, string jsonForDocument)
+ public JObject CreateDocument(string id, string jsonForDocument)
{
var jobj = JObject.Parse(jsonForDocument);
if (jobj.Value<object>("_rev") == null)
@@ -38,10 +38,10 @@ public Document CreateDocument(string id, string jsonForDocument)
.Data(jobj.ToString(Formatting.None))
.GetResponse();
return
- resp.GetCouchDocument();
+ resp.GetJObject();
}
- public Document CreateDocument(Document doc)
+ public JObject CreateDocument(Document doc)
{
return CreateDocument(doc.Id, doc.ToString());
}
@@ -50,14 +50,12 @@ public Document CreateDocument(Document doc)
/// </summary>
/// <param name="jsonForDocument">Json for creating the document</param>
/// <returns></returns>
- public Document CreateDocument(string jsonForDocument)
+ public JObject CreateDocument(string jsonForDocument)
{
- var json = JObject.Parse(jsonForDocument);
+ var json = JObject.Parse(jsonForDocument); //to make sure it's valid json
var jobj =
GetRequest(databaseBaseUri + "/").Post().Json().Data(jsonForDocument).GetResponse().GetJObject();
- json["_id"] = jobj["id"];
- json["_rev"] = jobj["rev"];
- return new Document(json);
+ return jobj;
}
public JObject DeleteDocument(string id, string rev)
{
@@ -142,15 +140,13 @@ public JObject DeleteAttachment(string id, string attachmentName)
return DeleteAttachment(doc.Id, doc.Rev, attachmentName);
}
- public Document SaveDocument(Document document)
+ public JObject SaveDocument(Document document)
{
if (document.Rev == null)
return CreateDocument(document);
var resp = GetRequest(databaseBaseUri + "/" + document.Id + "?rev=" + document.Rev).Put().Form().Data(document).GetResponse();
- var jobj = resp.GetJObject();
- //TODO: Change this so it simply alters the revision on the document past in so that there isn't an additional request.
- return GetDocument(document.Id);
+ return resp.GetJObject();
}
/// <summary>
View
34 LoveSeat/Document.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace LoveSeat
@@ -18,30 +19,27 @@ public Document(T obj, IObjectSerializer<T> objectSerializer) : base(objectSeria
public class Document : JObject
{
- public string Id
- {
- get { return this["_id"].Value<string>(); }
- set { this["_id"] = value; }
- }
- public string Rev
- {
- get
- {
- JToken rev;
- if (this.TryGetValue("_rev",out rev ))
- {
- return rev.Value<string>();
- }
- return null;
- }
- set { this["_rev"] = value; }
- }
+ [JsonProperty("_id")]
+ public string Id { get; set; }
+
+ [JsonProperty("_rev")]
+ public string Rev { get; set; }
+
protected Document()
{
}
public Document(JObject jobj)
: base(jobj)
{
+ JToken tmp;
+ if (jobj.TryGetValue("id", out tmp))
+ this.Id = tmp.Value<string>();
+ if (jobj.TryGetValue("rev", out tmp))
+ this.Rev = tmp.Value<string>();
+ if (jobj.TryGetValue("_id", out tmp))
+ this.Id = tmp.Value<string>();
+ if (jobj.TryGetValue("_rev", out tmp))
+ this.Rev = tmp.Value<string>();
}
public Document(string json)
: base(JObject.Parse(json))
View
12 LoveSeat/Interfaces/IDocumentDatabase.cs
@@ -12,17 +12,17 @@ public interface IDocumentDatabase
/// </summary>
/// <param name="id">Id of Document</param>
/// <param name="jsonForDocument"></param>
- /// <returns></returns>
- Document CreateDocument(string id, string jsonForDocument);
+ /// <returns>The status from CouchDb as a JObject</returns>
+ JObject CreateDocument(string id, string jsonForDocument);
- Document CreateDocument(Document doc);
+ JObject CreateDocument(Document doc);
/// <summary>
/// Creates a document when you intend for Couch to generate the id for you.
/// </summary>
/// <param name="jsonForDocument">Json for creating the document</param>
- /// <returns></returns>
- Document CreateDocument(string jsonForDocument);
+ /// <returns>Returns the status from Couchdb as a JObject</returns>
+ JObject CreateDocument(string jsonForDocument);
JObject DeleteDocument(string id, string rev);
@@ -60,7 +60,7 @@ public interface IDocumentDatabase
Stream GetAttachmentStream(string docId, string attachmentName);
JObject DeleteAttachment(string id, string rev, string attachmentName);
JObject DeleteAttachment(string id, string attachmentName);
- Document SaveDocument(Document document);
+ JObject SaveDocument(Document document);
/// <summary>
/// Gets the results of a view with no view parameters. Use the overload to pass parameters
View
4 LoveSeat/ObjectSerializer.cs
@@ -24,11 +24,11 @@ public ObjectSerializer()
public virtual T Deserialize(string json)
{
- return JsonConvert.DeserializeObject<T>(json.Replace("_id", "id").Replace("_rev", "rev"), settings);
+ return JsonConvert.DeserializeObject<T>(json, settings);
}
public virtual string Serialize(object obj)
{
- return JsonConvert.SerializeObject(obj, Formatting.Indented, settings).Replace("id", "_id").Replace("rev", "_rev");
+ return JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
}
}
}
View
3 LoveSeat/Support/ResponseExtensionMethods.cs
@@ -8,7 +8,8 @@ public static class ResponseExtensionMethods
{
public static Document GetCouchDocument(this HttpWebResponse response)
{
- return new Document(response.GetJObject());
+ var jobj = JObject.Parse(response.GetResponseString());
+ return new Document(jobj);
}
public static string GetResponseString(this HttpWebResponse response)

0 comments on commit af791ec

Please sign in to comment.
Something went wrong with that request. Please try again.