Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Attachment Deleting now supported

  • Loading branch information...
commit be629f659b97b547be8ae94811ad887614582d65 1 parent 307de16
@soitgoes authored
View
29 LoveSeat.IntegrationTest/CouchClientTest.cs
@@ -99,7 +99,32 @@ public void Should_Delete_Admin_User()
client.DeleteAdminUser("Leela");
}
-
-
+ [Test]
+ public void Should_Get_Attachment()
+ {
+ var db = client.GetDatabase(baseDatabase);
+ db.CreateDocument(@"{""_id"":""test_upload""}");
+ var doc = db.GetDocument("test_upload");
+ var attachment = File.ReadAllBytes("../../Files/test_upload.txt");
+ db.AddAttachment("test_upload", attachment, "test_upload.txt", "text/html");
+ var stream = db.GetAttachmentStream(doc, "test_upload.txt");
+ using (StreamReader sr = new StreamReader(stream))
+ {
+ string result = sr.ReadToEnd();
+ Assert.IsTrue(result == "test");
+ }
+ }
+ [Test]
+ public void Should_Delete_Attachment()
+ {
+ var db = client.GetDatabase(baseDatabase);
+ db.CreateDocument(@"{""_id"":""test_delete""}");
+ var doc = db.GetDocument("test_delete");
+ var attachment = File.ReadAllBytes("../../Files/test_upload.txt");
+ db.AddAttachment("test_delete", attachment, "test_upload.txt", "text/html");
+ db.DeleteAttachment("test_delete", "test_upload.txt");
+ var retrieved = db.GetDocument("test_delete");
+ Assert.IsFalse(retrieved.HasAttachment);
+ }
}
}
View
1  LoveSeat.IntegrationTest/Files/test_upload.txt
@@ -0,0 +1 @@
+test
View
1  LoveSeat.IntegrationTest/LoveSeat.IntegrationTest.csproj
@@ -60,6 +60,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="Files\martin.jpg" />
+ <Content Include="Files\test_upload.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
View
1  LoveSeat/CouchClient.cs
@@ -168,6 +168,7 @@ public bool HasUser(string userId)
{
return GetUser(userId) != null;
}
+
/// <summary>
/// Get's the user.
/// </summary>
View
34 LoveSeat/CouchDatabase.cs
@@ -1,3 +1,5 @@
+using System.IO;
+using System.Web;
using LoveSeat.Support;
using Newtonsoft.Json.Linq;
@@ -33,14 +35,14 @@ public CouchDocument CreateDocument(string id, string jsonForDocument)
/// </summary>
/// <param name="jsonForDocument">Json for creating the document</param>
/// <returns></returns>
- public CouchDocument CreateDocument(string jsonForDocument)
+ public JObject CreateDocument(string jsonForDocument)
{
return
- GetRequest(databaseBaseUri + "/").Post().Json().Data(jsonForDocument).GetResponse().GetCouchDocument();
+ GetRequest(databaseBaseUri + "/").Post().Json().Data(jsonForDocument).GetResponse().GetJObject();
}
- public CouchDocument DeleteDocument(string id, string rev)
+ public JObject DeleteDocument(string id, string rev)
{
- return GetRequest(databaseBaseUri + "/" + id + "?rev=" + rev).Delete().Form().GetResponse().GetCouchDocument();
+ return GetRequest(databaseBaseUri + "/" + id + "?rev=" + rev).Delete().Form().GetResponse().GetJObject();
}
/// <summary>
/// Returns null if document is not found
@@ -87,6 +89,28 @@ public JObject AddAttachment(string id, string rev, byte[] attachment,string fi
GetRequest(databaseBaseUri + "/" + id + "/" +filename + "?rev=" + rev).Put().ContentType(contentType).Data(attachment).GetResponse().GetJObject();
}
-
+ public Stream GetAttachmentStream(CouchDocument doc, string attachmentName)
+ {
+ return GetAttachmentStream(doc.Id, doc.Rev, attachmentName);
+ }
+ public Stream GetAttachmentStream(string docId, string rev, string attachmentName)
+ {
+ return GetRequest(databaseBaseUri + "/" + docId + "/" + HttpUtility.UrlEncode(attachmentName)).Get().GetResponse().GetResponseStream();
+ }
+ public Stream GetAttachmentStream(string docId, string attachmentName)
+ {
+ var doc = GetDocument(docId);
+ if (doc == null) return null;
+ return GetAttachmentStream(docId, doc.Rev, attachmentName);
+ }
+ public JObject DeleteAttachment(string id, string rev, string attachmentName)
+ {
+ return GetRequest(databaseBaseUri + "/" + id + "/" + attachmentName + "?rev=" + rev).Json().Delete().GetResponse().GetJObject();
+ }
+ public JObject DeleteAttachment(string id, string attachmentName)
+ {
+ var doc = GetDocument(id);
+ return DeleteAttachment(doc.Id, doc.Rev, attachmentName);
+ }
}
}
View
3  LoveSeat/Support/CouchException.cs
@@ -1,4 +1,5 @@
using System;
+using System.Net;
using Newtonsoft.Json.Linq;
namespace LoveSeat.Support
@@ -7,7 +8,7 @@ public class CouchException : Exception
{
private readonly JObject response;
- public CouchException(JObject response): base(response.ToString())
+ public CouchException(JObject response, HttpWebRequest request): base(response.ToString()+ request.ToString())
{
this.response = response;
}
View
5 LoveSeat/Support/CouchRequest.cs
@@ -86,15 +86,16 @@ public HttpWebResponse GetResponse()
{
try
{
- var response = (HttpWebResponse)request.GetResponse();
+ var response = (HttpWebResponse)request.GetResponse();
return response;
}
catch (WebException webEx)
{
var response = (HttpWebResponse)webEx.Response;
+
if (response != null)
{
- throw new CouchException(response.GetCouchDocument());
+ throw new CouchException(response.GetCouchDocument(), request);
}
throw;
}
Please sign in to comment.
Something went wrong with that request. Please try again.