Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #44 from jvdgeest/master

Implemented attachment parameter for the GetDocument methods and improved readability a bit for CouchDatabase.cs
  • Loading branch information...
commit 81448cf2b96d18ff4003a095425bd2c6f6e3a91a 2 parents b9c3a58 + 487ce7f
Martin Murphy authored January 30, 2013

Showing 1 changed file with 44 additions and 24 deletions. Show diff stats Hide diff stats

  1. 68  LoveSeat/CouchDatabase.cs
68  LoveSeat/CouchDatabase.cs
@@ -75,29 +75,49 @@ public CouchResponse DeleteDocument(string id, string rev)
75 75
         /// </summary>
76 76
         /// <param name="id"></param>
77 77
         /// <returns></returns>
78  
-        public Document GetDocument(string id)
  78
+        public T GetDocument<T>(string id, bool attachments, IObjectSerializer objectSerializer)
79 79
         {
80  
-            var resp = GetRequest(databaseBaseUri + "/" + id).Get().Json().GetResponse();
81  
-            if (resp.StatusCode==HttpStatusCode.NotFound) return null;
82  
-            return resp.GetCouchDocument();
  80
+            var resp = GetRequest(String.Format("{0}/{1}{2}", databaseBaseUri, id, attachments ? "?attachments=true" : string.Empty)).Get().Json().GetResponse();
  81
+            if (resp.StatusCode == HttpStatusCode.NotFound) return default(T);
  82
+            return objectSerializer.Deserialize<T>(resp.GetResponseString());
83 83
         }
84  
-        public T GetDocument<T>(Guid id , IObjectSerializer objectSerializer)
  84
+        public T GetDocument<T>(string id, IObjectSerializer objectSerializer)
85 85
         {
86  
-            return GetDocument<T>(id.ToString(), objectSerializer);
  86
+            return GetDocument<T>(id, false, objectSerializer);
87 87
         }
88  
-        public T GetDocument<T>(Guid id)
  88
+        public T GetDocument<T>(string id, bool attachments)
89 89
         {
90  
-            return GetDocument<T>(id.ToString());
  90
+            return GetDocument<T>(id, attachments, ObjectSerializer);
91 91
         }
92 92
         public T GetDocument<T>(string id)
93 93
         {
94  
-            return GetDocument<T>(id, ObjectSerializer);
  94
+            return GetDocument<T>(id, false);
95 95
         }
96  
-        public T GetDocument<T>(string id, IObjectSerializer objectSerializer)
  96
+        public T GetDocument<T>(Guid id, bool attachments, IObjectSerializer objectSerializer)
97 97
         {
98  
-            var resp = GetRequest(databaseBaseUri + "/" + id).Get().Json().GetResponse();
99  
-            if (resp.StatusCode == HttpStatusCode.NotFound) return default(T);
100  
-            return objectSerializer.Deserialize<T>(resp.GetResponseString());
  98
+            return GetDocument<T>(id.ToString(), attachments, objectSerializer);
  99
+        }
  100
+        public T GetDocument<T>(Guid id, IObjectSerializer objectSerializer)
  101
+        {
  102
+            return GetDocument<T>(id, false, objectSerializer);
  103
+        }
  104
+        public T GetDocument<T>(Guid id, bool attachments)
  105
+        {
  106
+            return GetDocument<T>(id.ToString(), attachments);
  107
+        }
  108
+        public T GetDocument<T>(Guid id)
  109
+        {
  110
+            return GetDocument<T>(id, false);
  111
+        }
  112
+        public Document GetDocument(string id, bool attachments)
  113
+        {
  114
+            var resp = GetRequest(String.Format("{0}/{1}{2}", databaseBaseUri, id, attachments ? "?attachments=true" : string.Empty)).Get().Json().GetResponse();
  115
+            if (resp.StatusCode == HttpStatusCode.NotFound) return null;
  116
+            return resp.GetCouchDocument();
  117
+        }
  118
+        public Document GetDocument(string id)
  119
+        {
  120
+            return GetDocument(id, false);
101 121
         }
102 122
 
103 123
         /// <summary>
@@ -186,7 +206,7 @@ public CouchResponse AddAttachment(string id, byte[] attachment, string filename
186 206
         public CouchResponse AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType)
187 207
         {
188 208
             return
189  
-                GetRequest(databaseBaseUri + "/" + id + "/" + filename + "?rev=" + rev).Put().ContentType(contentType).Data(attachment).GetResponse().GetJObject();
  209
+                GetRequest(string.Format("{0}/{1}/{2}?rev={3}", databaseBaseUri, id, filename, rev)).Put().ContentType(contentType).Data(attachment).GetResponse().GetJObject();
190 210
         }
191 211
         /// <summary>
192 212
         /// Adds an attachment to a document.  If revision is not specified then the most recent will be fetched and used.  Warning: if you need document update conflicts to occur please use the method that specifies the revision
@@ -211,7 +231,7 @@ public CouchResponse AddAttachment(string id, Stream attachmentStream, string fi
211 231
         public CouchResponse AddAttachment(string id, string rev, Stream attachmentStream, string filename, string contentType)
212 232
         {
213 233
             return
214  
-                GetRequest(databaseBaseUri + "/" + id + "/" + filename + "?rev=" + rev).Put().ContentType(contentType).Data(attachmentStream).GetResponse().GetJObject();
  234
+                GetRequest(string.Format("{0}/{1}/{2}?rev={3}", databaseBaseUri, id, filename, rev)).Put().ContentType(contentType).Data(attachmentStream).GetResponse().GetJObject();
215 235
         }
216 236
 
217 237
         public Stream GetAttachmentStream(Document doc, string attachmentName)
@@ -220,7 +240,7 @@ public Stream GetAttachmentStream(Document doc, string attachmentName)
220 240
         }
221 241
         public Stream GetAttachmentStream(string docId, string rev, string attachmentName)
222 242
         {
223  
-            return GetRequest(databaseBaseUri + "/" + docId + "/" + HttpUtility.UrlEncode(attachmentName)).Get().GetResponse().GetResponseStream();
  243
+            return GetRequest(string.Format("{0}/{1}/{2}", databaseBaseUri, docId, HttpUtility.UrlEncode(attachmentName))).Get().GetResponse().GetResponseStream();
224 244
         }
225 245
         public Stream GetAttachmentStream(string docId, string attachmentName)
226 246
         {
@@ -230,7 +250,7 @@ public Stream GetAttachmentStream(string docId, string attachmentName)
230 250
         }
231 251
         public CouchResponse DeleteAttachment(string id, string rev, string attachmentName)
232 252
         {
233  
-            return GetRequest(databaseBaseUri + "/" + id + "/" + attachmentName + "?rev=" + rev).Json().Delete().GetResponse().GetJObject();
  253
+            return GetRequest(string.Format("{0}/{1}/{2}?rev={3}", databaseBaseUri, id, attachmentName, rev)).Json().Delete().GetResponse().GetJObject();
234 254
         }
235 255
         public CouchResponse DeleteAttachment(string id, string attachmentName)
236 256
         {
@@ -243,7 +263,7 @@ public CouchResponse SaveDocument(Document document)
243 263
             if (document.Rev == null)
244 264
                 return CreateDocument(document);
245 265
                     
246  
-            var resp = GetRequest(databaseBaseUri + "/" + document.Id + "?rev=" + document.Rev).Put().Form().Data(document).GetResponse();
  266
+            var resp = GetRequest(string.Format("{0}/{1}?rev={2}", databaseBaseUri, document.Id, document.Rev)).Put().Form().Data(document).GetResponse();
247 267
             return resp.GetJObject();
248 268
         }
249 269
 
@@ -311,7 +331,7 @@ private static JObject CheckAccepted(HttpWebResponse resp)
311 331
             }
312 332
 
313 333
             if (resp.StatusCode != HttpStatusCode.Accepted) {
314  
-                throw new System.Exception("Response return with a HTTP Code of " + resp.StatusCode + " - " + resp.StatusDescription);
  334
+                throw new System.Exception(string.Format("Response return with a HTTP Code of {0} - {1}", resp.StatusCode, resp.StatusDescription));
315 335
             }
316 336
 
317 337
             return resp.GetJObject();
@@ -334,13 +354,13 @@ private void ThrowDesignDocException()
334 354
         public string Show(string showName, string docId, string designDoc)
335 355
         {
336 356
             //TODO:  add in Etag support for Shows
337  
-            var uri = databaseBaseUri + "/_design/" + designDoc + "/_show/" + showName + "/" + docId;
  357
+            var uri = string.Format("{0}/_design/{1}/_show/{2}/{3}", databaseBaseUri, designDoc, showName, docId);
338 358
             var req = GetRequest(uri);
339 359
             return req.GetResponse().GetResponseString();
340 360
         }
341 361
         public IListResult List(string listName, string viewName, ViewOptions options,  string designDoc)
342 362
         {            
343  
-			var uri = databaseBaseUri + "/_design/" + designDoc + "/_list/" + listName + "/" + viewName + options.ToString();
  363
+			var uri = string.Format("{0}/_design/{1}/_list/{2}/{3}{4}", databaseBaseUri, designDoc, listName, viewName, options.ToString());
344 364
             var req = GetRequest(uri);
345 365
             return new ListResult(req.GetRequest(), req.GetResponse());
346 366
         }
@@ -380,7 +400,7 @@ public void SetDefaultDesignDoc(string designDoc)
380 400
         /// <returns></returns>
381 401
         public ViewResult<T> View<T>(string viewName, ViewOptions options, string designDoc)
382 402
         {
383  
-            var uri = databaseBaseUri + "/_design/" + designDoc + "/_view/" + viewName;
  403
+            var uri = string.Format("{0}/_design/{1}/_view/{2}", databaseBaseUri, designDoc, viewName);
384 404
             return ProcessGenericResults<T>(uri, options);
385 405
         }
386 406
         /// <summary>
@@ -398,14 +418,14 @@ public ViewResult<T>  View<T>(string viewName, ViewOptions options)
398 418
         
399 419
         public ViewResult View(string viewName, ViewOptions options, string designDoc)
400 420
         {
401  
-            var uri = databaseBaseUri + "/_design/" + designDoc + "/_view/" + viewName;
  421
+            var uri = string.Format("{0}/_design/{1}/_view/{2}", databaseBaseUri, designDoc, viewName);
402 422
             return ProcessResults(uri, options);
403 423
         }
404 424
 
405 425
         public ViewResult View(string viewName, ViewOptions options)
406 426
         {
407 427
             ThrowDesignDocException();
408  
-            var uri = databaseBaseUri + "/_design/" + this.defaultDesignDoc + "/_view/" + viewName;
  428
+            var uri = string.Format("{0}/_design/{1}/_view/{2}", databaseBaseUri, this.defaultDesignDoc, viewName);
409 429
             return ProcessResults(uri, options);
410 430
         }
411 431
         private ViewResult ProcessResults(string uri, ViewOptions options)

0 notes on commit 81448cf

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