Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ViewManager

  • Loading branch information...
commit 2c04972b135782fc51868a66b533ebde3dfc8812 1 parent da1b0ef
@hhariri authored
Showing with 402 additions and 220 deletions.
  1. +11 −17 src/EasyCouchDB.Specs/AttachmentSpecs.cs
  2. +51 −70 src/EasyCouchDB.Specs/CRUDSpecs.cs
  3. +2 −0  src/EasyCouchDB.Specs/EasyCouchDB.Specs.csproj
  4. +2 −4 src/EasyCouchDB.Specs/Helpers/User.cs
  5. +4 −17 src/EasyCouchDB.Specs/LinqSpecs.cs
  6. +5 −2 src/EasyCouchDB.Specs/Properties/AssemblyInfo.cs
  7. +25 −0 src/EasyCouchDB.Specs/QueryManagerSpecs.cs
  8. +30 −0 src/EasyCouchDB.Specs/ViewManagerSpecs.cs
  9. +44 −56 src/EasyCouchDB/CouchDatabase.cs
  10. +7 −7 src/EasyCouchDB/CouchServer.cs
  11. +2 −0  src/EasyCouchDB/Document.cs
  12. +15 −8 src/EasyCouchDB/EasyCouchDB.csproj
  13. +1 −1  src/EasyCouchDB/ICouchDatabase.cs
  14. +2 −2 src/EasyCouchDB/{ → Infrastructure}/AllDocsRows.cs
  15. +1 −1  src/EasyCouchDB/{ → Infrastructure}/AttachmentException.cs
  16. +1 −1  src/EasyCouchDB/{ → Infrastructure}/DocsRows.cs
  17. +1 −1  src/EasyCouchDB/{ → Infrastructure}/DocumentConflictException.cs
  18. +1 −1  src/EasyCouchDB/{ → Infrastructure}/DocumentNotFoundException.cs
  19. +24 −0 src/EasyCouchDB/Infrastructure/DocumentUpdateException.cs
  20. +2 −2 src/EasyCouchDB/{ → Infrastructure}/MultiRowResponseWrapperForAllDocs.cs
  21. +2 −2 src/EasyCouchDB/{ → Infrastructure}/MultiRowResponseWrapperForDocs.cs
  22. +24 −0 src/EasyCouchDB/Infrastructure/ViewException.cs
  23. +5 −2 src/EasyCouchDB/Properties/AssemblyInfo.cs
  24. +12 −0 src/EasyCouchDB/Queries/QueryManager.cs
  25. +0 −26 src/EasyCouchDB/ViewManager.cs
  26. +11 −0 src/EasyCouchDB/Views/IViewManager.cs
  27. +19 −0 src/EasyCouchDB/Views/MapReduce.cs
  28. +19 −0 src/EasyCouchDB/Views/ViewDocument.cs
  29. +64 −0 src/EasyCouchDB/Views/ViewManager.cs
  30. +15 −0 src/EasyCouchDB/Views/Views.cs
View
28 src/EasyCouchDB.Specs/AttachmentSpecs.cs
@@ -4,43 +4,37 @@
namespace EasyCouchDB.Specs
{
-
-
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_adding_an_attachment_to_an_existing_document : DatabaseContext
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_adding_an_attachment_to_an_existing_document : ServerAndDatabaseContext
{
Because of = () =>
{
- var imageFile = Path.Combine("Helpers", "test.jpg");
+ string imageFile = Path.Combine("Helpers", "test.jpg");
- couchDb.SaveAttachment(DocumentId, imageFile, "image/jpeg");
+ Database.SaveAttachment(DocumentId, imageFile, "image/jpeg");
};
It should_add_it = () => { };
static User user;
static string id;
- }
-
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_deleting_an_attachment : DatabaseContext
+ }
+
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_deleting_an_attachment : ServerAndDatabaseContext
{
Establish context = () =>
{
- var imageFile = Path.Combine("Helpers", "test.jpg");
+ string imageFile = Path.Combine("Helpers", "test.jpg");
- couchDb.SaveAttachment(DocumentId, imageFile, "image/jpeg");
+ Database.SaveAttachment(DocumentId, imageFile, "image/jpeg");
};
- Because of = () =>
- {
- couchDb.DeleteAttachment(DocumentId, "test.jpg");
- };
+ Because of = () => { Database.DeleteAttachment(DocumentId, "test.jpg"); };
It should_delete_it = () => { };
static User user;
static string id;
}
-
}
View
121 src/EasyCouchDB.Specs/CRUDSpecs.cs
@@ -1,20 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using EasyCouchDB.Infrastructure;
using EasyCouchDB.Specs.Helpers;
using Machine.Specifications;
namespace EasyCouchDB.Specs
{
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_creating_a_new_document_with_no_id: DatabaseContext
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_creating_a_new_document_with_no_id : ServerAndDatabaseContext
{
Because of = () =>
{
user = new User {Fullname = "Jackson"};
- id = couchDb.Save(user);
-
+ id = Database.Save(user);
};
It should_create_the_document_and_return_a_generated_id = () => id.ShouldNotBeEmpty();
@@ -23,17 +23,16 @@ public class when_creating_a_new_document_with_no_id: DatabaseContext
static string id;
}
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_creating_a_new_document_provided_an_id: DatabaseContext
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_creating_a_new_document_provided_an_id : ServerAndDatabaseContext
{
Because of = () =>
{
- var randomDocumentId = GetRandomDocumentId();
-
- user = new User {Id = randomDocumentId , Fullname = "Jackson"};
+ string randomDocumentId = GetRandomDocumentId();
- id = couchDb.Save(user);
+ user = new User {Id = randomDocumentId, Fullname = "Jackson"};
+ id = Database.Save(user);
};
It should_create_the_document_and_return_the_given_id = () => id.ShouldEqual(user.Id);
@@ -42,49 +41,43 @@ public class when_creating_a_new_document_provided_an_id: DatabaseContext
static string id;
}
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_updating_an_existing_document: DatabaseContext
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_updating_an_existing_document : ServerAndDatabaseContext
{
Establish context = () =>
{
- var randomDocumentId = GetRandomDocumentId();
-
- user = new User { Id = randomDocumentId, Fullname = "Jackson" };
+ string randomDocumentId = GetRandomDocumentId();
- id = couchDb.Save(user);
+ user = new User {Id = randomDocumentId, Fullname = "Jackson"};
+ id = Database.Save(user);
};
Because of = () =>
{
- var document = couchDb.Load(id);
+ User document = Database.Load(id);
document.Fullname = "New Name";
- couchDb.Save(document);
-
+ Database.Save(document);
};
It should_update_the_document = () =>
{
- var updatedDocument = couchDb.Load(id);
+ User updatedDocument = Database.Load(id);
updatedDocument.Fullname.ShouldEqual("New Name");
};
+
static User user;
static string id;
}
-
-
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_getting_a_document_by_id_that_exists: DatabaseContext
- {
- Because of = () =>
- {
- user = couchDb.Load(DocumentId);
- };
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_getting_a_document_by_id_that_exists : ServerAndDatabaseContext
+ {
+ Because of = () => { user = Database.Load(DocumentId); };
It should_retrieve_the_document = () => user.ShouldNotBeNull();
@@ -97,97 +90,85 @@ public class when_getting_a_document_by_id_that_exists: DatabaseContext
static User user;
}
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_deleting_an_existing_document: DatabaseContext
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_deleting_an_existing_document : ServerAndDatabaseContext
{
Establish context = () =>
{
randomDocumentId = string.Format("{0}DeleteTest", GetRandomDocumentId());
- couchDb.Save(new User() { Id = randomDocumentId });
+ Database.Save(new User {Id = randomDocumentId});
};
- Because of = () =>
- {
- couchDb.Delete(randomDocumentId);
- };
+ Because of = () => { Database.Delete(randomDocumentId); };
It should_delete_the_document = () =>
{
try
{
- couchDb.Load(randomDocumentId);
+ Database.Load(randomDocumentId);
}
catch (DocumentNotFoundException)
{
true.ShouldBeTrue();
}
- };
+ };
static string randomDocumentId;
}
- [Subject(typeof(CouchDatabase<User, string>), "given a document database")]
- public class when_getting_a_list_of_documents: DatabaseContext
+ [Subject(typeof (CouchDatabase<User, string>), "given a document database")]
+ public class when_getting_a_list_of_documents : ServerAndDatabaseContext
{
Because of = () =>
{
- documents = from d in couchDb.Documents()
+ documents = from d in Database.GetDocuments()
select d;
-
};
- It should_return_all_documents = () =>
- {
- documents.ShouldNotBeEmpty();
- };
+ It should_return_all_documents = () => { documents.ShouldNotBeEmpty(); };
- It should_set_document_properties = () =>
- {
- documents.First().Fullname.ShouldNotBeEmpty();
- };
+ It should_set_document_properties = () => { documents.First().Fullname.ShouldNotBeEmpty(); };
static IEnumerable<User> documents;
}
-
- public class DatabaseContext
+ public class ServerAndDatabaseContext
{
- Establish context = () =>
- {
- DocumentId = GetRandomDocumentId();
-
- var connection = new CouchServer("localhost", 5984, "easycouchdb");
-
- couchDb = new CouchDatabase<User, string>(connection);
-
- var user = new User { Id = DocumentId, Fullname = "My First User", EmailAddress = "MyEmail@MyDomain.com" };
-
- couchDb.Save(user);
-
- };
+ protected static ICouchDatabase<User, string> Database;
+ protected static ICouchServer Server;
+ protected static string DocumentId;
Cleanup cleanup = () =>
{
try
{
- couchDb.Delete("_design/easycouchdb_views");
+ Database.Delete("_design/easycouchdb_view_all");
}
catch (Exception)
{
-
throw;
}
};
- protected static ICouchDatabase<User, string> couchDb;
- protected static string DocumentId;
+ Establish context = () =>
+ {
+ DocumentId = GetRandomDocumentId();
+
+ Server = new CouchServer("localhost", 5984, "easycouchdb");
+
+ Database = new CouchDatabase<User, string>(Server);
+
+ var user = new User {Id = DocumentId, Fullname = "My First User", EmailAddress = "MyEmail@MyDomain.com"};
+
+ Database.Save(user);
+ };
protected static string GetRandomDocumentId()
{
var random = new Random();
-
+
return String.Format("{0}{1}", DateTime.Now.Ticks, random.Next(10, 10000));
}
}
View
2  src/EasyCouchDB.Specs/EasyCouchDB.Specs.csproj
@@ -49,6 +49,8 @@
<Compile Include="LinqSpecs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CRUDSpecs.cs" />
+ <Compile Include="QueryManagerSpecs.cs" />
+ <Compile Include="ViewManagerSpecs.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EasyCouchDB\EasyCouchDB.csproj">
View
6 src/EasyCouchDB.Specs/Helpers/User.cs
@@ -1,8 +1,6 @@
-using System;
-
-namespace EasyCouchDB.Specs.Helpers
+namespace EasyCouchDB.Specs.Helpers
{
- public class User: Document<string>
+ public class User : Document<string>
{
public string Fullname { get; set; }
public string EmailAddress { get; set; }
View
21 src/EasyCouchDB.Specs/LinqSpecs.cs
@@ -2,26 +2,13 @@
namespace EasyCouchDB.Specs
{
- [Subject( "given document database")]
+ [Subject("given document database")]
public class when_context
{
- Establish context = () =>
- {
+ Establish context = () => { };
- };
-
- Because of = () =>
- {
-
-
- };
-
- It should_objective = () =>
- {
-
- };
+ Because of = () => { };
+ It should_objective = () => { };
}
-
-
}
View
7 src/EasyCouchDB.Specs/Properties/AssemblyInfo.cs
@@ -1,10 +1,10 @@
using System.Reflection;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
+
[assembly: AssemblyTitle("EasyCouchDB.Specs")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
+
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
+
[assembly: Guid("1a70a720-7373-4687-bcba-f06c0b92d01f")]
// Version information for an assembly consists of the following four values:
@@ -32,5 +34,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
+
[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
25 src/EasyCouchDB.Specs/QueryManagerSpecs.cs
@@ -0,0 +1,25 @@
+using EasyCouchDB.Queries;
+using Machine.Specifications;
+
+namespace EasyCouchDB.Specs
+{
+ [Subject(typeof (QueryManager), "given document database")]
+ public class when_executing_a_query : ServerAndDatabaseContext
+ {
+ Establish context = () => { queryManager = new QueryManager(Server); };
+
+ Because of = () =>
+ {
+ // queryManager.ExecuteQuery();
+
+ //queryManager.ExecuteQuery()
+ };
+
+ //It should_create_the_view_document = () =>
+ //{
+ // queryManager.ViewExists(DocumentId).ShouldBeTrue();
+ //};
+
+ static QueryManager queryManager;
+ }
+}
View
30 src/EasyCouchDB.Specs/ViewManagerSpecs.cs
@@ -0,0 +1,30 @@
+using EasyCouchDB.Views;
+using Machine.Specifications;
+
+namespace EasyCouchDB.Specs
+{
+ [Subject(typeof (ViewManager), "given document database")]
+ public class when_asking_if_a_non_existing_view_eixsts : ServerAndDatabaseContext
+ {
+ Establish context = () => { _viewManager = new ViewManager(Server); };
+
+ Because of = () => { exists = _viewManager.ViewExists("some_random_view_doc"); };
+
+ It should_return_false = () => { exists.ShouldBeFalse(); };
+
+ static ViewManager _viewManager;
+ static bool exists;
+ }
+
+ [Subject(typeof (ViewManager), "given document database")]
+ public class when_creating_a_view_document : ServerAndDatabaseContext
+ {
+ Establish context = () => { viewManager = new ViewManager(Server); };
+
+ Because of = () => { viewManager.CreateView(DocumentId, "", ""); };
+
+ It should_create_the_view_document = () => { viewManager.ViewExists(DocumentId).ShouldBeTrue(); };
+
+ static ViewManager viewManager;
+ }
+}
View
100 src/EasyCouchDB/CouchDatabase.cs
@@ -4,18 +4,25 @@
using System.IO;
using System.Linq;
using System.Net;
-using EasyCouchDB;
+using EasyCouchDB.Infrastructure;
+using EasyCouchDB.Views;
using EasyHttp.Http;
namespace EasyCouchDB
{
- public class CouchDatabase<TDocument, TId>: ICouchDatabase<TDocument, TId> where TDocument: class, IDocument<TId>
+ public class CouchDatabase<TDocument, TId> : ICouchDatabase<TDocument, TId> where TDocument : class, IDocument<TId>
{
- readonly CouchServer _couchServer;
+ readonly ICouchServer _server;
+ readonly IViewManager _viewManager;
- public CouchDatabase(CouchServer couchServer)
+ public CouchDatabase(ICouchServer server) : this(server, new ViewManager(server))
{
- _couchServer = couchServer;
+ }
+
+ public CouchDatabase(ICouchServer server, IViewManager viewManager)
+ {
+ _server = server;
+ _viewManager = viewManager;
}
public string Save(TDocument document)
@@ -29,7 +36,7 @@ public string Save(TDocument document)
if (document.Id != null)
{
- var getResponse = _couchServer.Get(document.Id.ToString());
+ HttpResponse getResponse = _server.Get(document.Id.ToString());
if (getResponse.StatusCode == HttpStatusCode.OK)
{
@@ -42,34 +49,41 @@ public string Save(TDocument document)
}
document.Revision = currentDocument.Revision;
- }
- var response = _couchServer.Put(document.Id.ToString(), document);
+ }
+ HttpResponse response = _server.Put(document.Id.ToString(), document);
+ if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.OK)
+ {
+ throw new DocumentUpdateException(response.StatusDescription);
+ }
return response.DynamicBody.id;
}
// It's a new insert with auto-assign
- var postResponse = _couchServer.Post("/", document);
+ HttpResponse postResponse = _server.Post("/", document);
- return postResponse.DynamicBody.id;
+ if (postResponse.StatusCode != HttpStatusCode.Created && postResponse.StatusCode != HttpStatusCode.OK)
+ {
+ throw new DocumentUpdateException(postResponse.StatusDescription);
+ }
+ return postResponse.DynamicBody.id;
}
public TDocument Load(TId id)
{
+ HttpResponse response = _server.Get(id.ToString());
- var response = _couchServer.Get(id.ToString());
-
if (response.StatusCode != HttpStatusCode.NotFound)
{
return response.StaticBody<TDocument>();
}
- throw new DocumentNotFoundException("id");
+ throw new DocumentNotFoundException(id.ToString());
}
public void Delete(TId id)
{
- var response = _couchServer.Head(id.ToString());
+ HttpResponse response = _server.Head(id.ToString());
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -77,51 +91,44 @@ public void Delete(TId id)
}
- response = _couchServer.Delete(String.Format("{0}?rev={1}", id, response.ETag));
+ response = _server.Delete(String.Format("{0}?rev={1}", id, response.ETag));
if (response.StatusCode != HttpStatusCode.OK)
{
- throw new DocumentNotFoundException();
+ throw new DocumentNotFoundException(id.ToString());
}
}
public IEnumerable<dynamic> GetAllDocuments()
{
- var response = _couchServer.Get("_all_docs?include_docs=true");
+ HttpResponse response = _server.Get("_all_docs?include_docs=true");
var wrapper = response.StaticBody<MultiRowResponseWrapperForAllDocs<TDocument>>();
return wrapper.Rows.Select(t => t.Document).ToList();
}
- public IEnumerable<TDocument> Documents()
+ public IEnumerable<TDocument> GetDocuments()
{
- var response = _couchServer.Head("_design/easycouchdb_views/_view/all");
-
- if (response.StatusCode != HttpStatusCode.OK)
+ if (!_viewManager.ViewExists("all"))
{
- // Save
- CreateView();
-
- response = _couchServer.Get("_design/easycouchdb_views/_view/all");
+ _viewManager.CreateView("all",
+ "if (doc.internalDocType=='" + typeof (TDocument).Name +
+ "') { emit(doc.id,doc);}");
}
-
- var wrapper = response.StaticBody<MultiRowResponseWrapperForDocs<TDocument>>();
-
- return wrapper.Rows.Select(t => t.Document).ToList();
+ return _viewManager.ExecuteView<TDocument>("all");
}
public void SaveAttachment(TId id, string filename, string contentType)
{
-
- var response = _couchServer.Head(id.ToString());
+ HttpResponse response = _server.Head(id.ToString());
string url;
if (response.StatusCode == HttpStatusCode.OK)
{
- var revision = response.ETag;
+ string revision = response.ETag;
url = String.Format("{0}/{1}?rev={2}", id, Path.GetFileName(filename), revision);
}
@@ -130,52 +137,33 @@ public void SaveAttachment(TId id, string filename, string contentType)
url = String.Format("{0}/{1}", id, Path.GetFileName(filename));
}
- response = _couchServer.PutFile(url, filename, contentType);
+ response = _server.PutFile(url, filename, contentType);
if (response.StatusCode != HttpStatusCode.Created)
{
- throw new AttachmentException(response.StatusDescription);
+ throw new AttachmentException(response.StatusDescription);
}
}
public void DeleteAttachment(TId id, string attachmentName)
{
- var response = _couchServer.Head(id.ToString());
+ HttpResponse response = _server.Head(id.ToString());
if (response.StatusCode != HttpStatusCode.OK)
{
throw new AttachmentException(response.StatusDescription);
}
- var url = String.Format("{0}/{1}?rev={2}", id, Path.GetFileName(attachmentName), response.ETag);
+ string url = String.Format("{0}/{1}?rev={2}", id, Path.GetFileName(attachmentName), response.ETag);
- response = _couchServer.Delete(url);
+ response = _server.Delete(url);
if (response.StatusCode != HttpStatusCode.OK)
{
throw new AttachmentException(response.StatusDescription);
}
-
}
-
- void CreateView()
- {
- dynamic mappingFunction = new ExpandoObject();
-
- mappingFunction.map = "function (doc) { if (doc.internalDocType=='" + typeof(TDocument).Name + "') { emit(doc.id,doc);}}";
- dynamic mapping = new ExpandoObject();
-
- mapping.all = mappingFunction;
-
- dynamic viewDocument = new ExpandoObject();
-
- viewDocument._id = "_design/easycouchdb_views";
- viewDocument.language = "javascript";
- viewDocument.views = mapping;
-
- _couchServer.Put("_design/easycouchdb_views", viewDocument);
- }
}
}
View
14 src/EasyCouchDB/CouchServer.cs
@@ -17,21 +17,16 @@ public CouchServer(string host, int port, string database)
_connection.Request.Accept = HttpContentTypes.ApplicationJson;
}
-
+
public HttpResponse Get(string uri)
{
return _connection.Get(GetFullUri(uri));
}
- string GetFullUri(string uri)
- {
- return (uri == "/") ? _baseUrl : String.Format("{0}/{1}", _baseUrl, uri);
- }
-
public HttpResponse Put(string uri, object data)
{
_connection.Put(GetFullUri(uri), data, HttpContentTypes.ApplicationJson);
-
+
return _connection.Response;
}
@@ -62,5 +57,10 @@ public HttpResponse Delete(string uri)
return _connection.Response;
}
+
+ string GetFullUri(string uri)
+ {
+ return (uri == "/") ? _baseUrl : String.Format("{0}/{1}", _baseUrl, uri);
+ }
}
}
View
2  src/EasyCouchDB/Document.cs
@@ -7,9 +7,11 @@ public class Document<TKey> : IDocument<TKey>
{
[JsonName("_id")]
public TKey Id { get; set; }
+
[JsonName("_rev")]
[DefaultValue("")]
public string Revision { get; set; }
+
[JsonName("internalDocType")]
[DefaultValue("")]
public string DocumentType { get; set; }
View
23 src/EasyCouchDB/EasyCouchDB.csproj
@@ -49,21 +49,28 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="AttachmentException.cs" />
<Compile Include="CouchServer.cs" />
- <Compile Include="DocsRows.cs" />
<Compile Include="Document.cs" />
- <Compile Include="DocumentConflictException.cs" />
- <Compile Include="DocumentNotFoundException.cs" />
<Compile Include="ICouchServer.cs" />
<Compile Include="IDocument.cs" />
<Compile Include="ICouchDatabase.cs" />
- <Compile Include="MultiRowResponseWrapperForAllDocs.cs" />
- <Compile Include="MultiRowResponseWrapperForDocs.cs" />
+ <Compile Include="Infrastructure\AllDocsRows.cs" />
+ <Compile Include="Infrastructure\AttachmentException.cs" />
+ <Compile Include="Infrastructure\DocsRows.cs" />
+ <Compile Include="Infrastructure\DocumentConflictException.cs" />
+ <Compile Include="Infrastructure\DocumentUpdateException.cs" />
+ <Compile Include="Infrastructure\DocumentNotFoundException.cs" />
+ <Compile Include="Infrastructure\MultiRowResponseWrapperForAllDocs.cs" />
+ <Compile Include="Infrastructure\MultiRowResponseWrapperForDocs.cs" />
+ <Compile Include="Infrastructure\ViewException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CouchDatabase.cs" />
- <Compile Include="AllDocsRows.cs" />
- <Compile Include="ViewManager.cs" />
+ <Compile Include="Queries\QueryManager.cs" />
+ <Compile Include="Views\IViewManager.cs" />
+ <Compile Include="Views\MapReduce.cs" />
+ <Compile Include="Views\ViewDocument.cs" />
+ <Compile Include="Views\ViewManager.cs" />
+ <Compile Include="Views\Views.cs" />
</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
2  src/EasyCouchDB/ICouchDatabase.cs
@@ -8,7 +8,7 @@ public interface ICouchDatabase<TDocument, TId> where TDocument : class, IDocume
TDocument Load(TId id);
void Delete(TId id);
IEnumerable<dynamic> GetAllDocuments();
- IEnumerable<TDocument> Documents();
+ IEnumerable<TDocument> GetDocuments();
void SaveAttachment(TId id, string filename, string imageJpeg);
void DeleteAttachment(TId id, string attachmentName);
}
View
4 src/EasyCouchDB/AllDocsRows.cs → ...EasyCouchDB/Infrastructure/AllDocsRows.cs
@@ -1,8 +1,8 @@
using JsonFx.Json;
-namespace EasyCouchDB
+namespace EasyCouchDB.Infrastructure
{
- public class AllDocsRows<TDocument>
+ public class AllDocsRows<TDocument>
{
[JsonName("doc")]
public TDocument Document { get; set; }
View
2  src/EasyCouchDB/AttachmentException.cs → ...hDB/Infrastructure/AttachmentException.cs
@@ -1,7 +1,7 @@
using System;
using System.Runtime.Serialization;
-namespace EasyCouchDB
+namespace EasyCouchDB.Infrastructure
{
public class AttachmentException : Exception
{
View
2  src/EasyCouchDB/DocsRows.cs → src/EasyCouchDB/Infrastructure/DocsRows.cs
@@ -1,6 +1,6 @@
using JsonFx.Json;
-namespace EasyCouchDB
+namespace EasyCouchDB.Infrastructure
{
public class DocsRows<TDocument>
{
View
2  src/EasyCouchDB/DocumentConflictException.cs → ...frastructure/DocumentConflictException.cs
@@ -1,7 +1,7 @@
using System;
using System.Runtime.Serialization;
-namespace EasyCouchDB
+namespace EasyCouchDB.Infrastructure
{
public class DocumentConflictException : Exception
{
View
2  src/EasyCouchDB/DocumentNotFoundException.cs → ...frastructure/DocumentNotFoundException.cs
@@ -1,7 +1,7 @@
using System;
using System.Runtime.Serialization;
-namespace EasyCouchDB
+namespace EasyCouchDB.Infrastructure
{
public class DocumentNotFoundException : Exception
{
View
24 src/EasyCouchDB/Infrastructure/DocumentUpdateException.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Runtime.Serialization;
+
+namespace EasyCouchDB.Infrastructure
+{
+ public class DocumentUpdateException : Exception
+ {
+ public DocumentUpdateException()
+ {
+ }
+
+ public DocumentUpdateException(string message) : base(message)
+ {
+ }
+
+ public DocumentUpdateException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+
+ protected DocumentUpdateException(SerializationInfo info, StreamingContext context) : base(info, context)
+ {
+ }
+ }
+}
View
4 ...chDB/MultiRowResponseWrapperForAllDocs.cs → ...ture/MultiRowResponseWrapperForAllDocs.cs
@@ -1,6 +1,6 @@
-namespace EasyCouchDB
+namespace EasyCouchDB.Infrastructure
{
- public class MultiRowResponseWrapperForAllDocs<TDocument>
+ public class MultiRowResponseWrapperForAllDocs<TDocument>
{
public int TotalRows { get; set; }
public AllDocsRows<TDocument>[] Rows { get; set; }
View
4 ...CouchDB/MultiRowResponseWrapperForDocs.cs → ...ructure/MultiRowResponseWrapperForDocs.cs
@@ -1,6 +1,6 @@
-namespace EasyCouchDB
+namespace EasyCouchDB.Infrastructure
{
- public class MultiRowResponseWrapperForDocs<TDocument>
+ public class MultiRowResponseWrapperForDocs<TDocument>
{
public int TotalRows { get; set; }
public DocsRows<TDocument>[] Rows { get; set; }
View
24 src/EasyCouchDB/Infrastructure/ViewException.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Runtime.Serialization;
+
+namespace EasyCouchDB.Infrastructure
+{
+ public class ViewException : Exception
+ {
+ public ViewException()
+ {
+ }
+
+ public ViewException(string message) : base(message)
+ {
+ }
+
+ public ViewException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+
+ protected ViewException(SerializationInfo info, StreamingContext context) : base(info, context)
+ {
+ }
+ }
+}
View
7 src/EasyCouchDB/Properties/AssemblyInfo.cs
@@ -1,10 +1,10 @@
using System.Reflection;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
+
[assembly: AssemblyTitle("EasyCouchDB")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
+
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
+
[assembly: Guid("b0a05de0-0f2a-4256-84a7-8599a47d8202")]
// Version information for an assembly consists of the following four values:
@@ -32,5 +34,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
+
[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
12 src/EasyCouchDB/Queries/QueryManager.cs
@@ -0,0 +1,12 @@
+namespace EasyCouchDB.Queries
+{
+ public class QueryManager
+ {
+ readonly ICouchServer _server;
+
+ public QueryManager(ICouchServer server)
+ {
+ _server = server;
+ }
+ }
+}
View
26 src/EasyCouchDB/ViewManager.cs
@@ -1,26 +0,0 @@
-using System.Dynamic;
-
-namespace EasyCouchDB
-{
- public class ViewManager
- {
-
- public dynamic Create(string viewName, string mapCode, string reduceCode)
- {
- dynamic mappingFunction = new ExpandoObject();
-
- mappingFunction.map = string.Format("function (doc) {{ {0} }}", mapCode);
- dynamic mapping = new ExpandoObject();
-
- mapping.all = mappingFunction;
-
- dynamic viewDocument = new ExpandoObject();
-
- viewDocument._id = "_design/easycouchdb_views";
- viewDocument.language = "javascript";
- viewDocument.views = mapping;
-
- return viewDocument;
- }
- }
-}
View
11 src/EasyCouchDB/Views/IViewManager.cs
@@ -0,0 +1,11 @@
+using System.Collections.Generic;
+
+namespace EasyCouchDB.Views
+{
+ public interface IViewManager
+ {
+ void CreateView(string viewName, string mapCode, string reduceCode = "");
+ bool ViewExists(string viewName);
+ IEnumerable<TDocument> ExecuteView<TDocument>(string viewName);
+ }
+}
View
19 src/EasyCouchDB/Views/MapReduce.cs
@@ -0,0 +1,19 @@
+using JsonFx.Json;
+
+namespace EasyCouchDB.Views
+{
+ public class MapReduce
+ {
+ public MapReduce(string map, string reduce)
+ {
+ Map = string.Format("function (doc) {{{0}}}", map);
+ Reduce = string.IsNullOrEmpty(reduce) ? null : string.Format("function (keys, values) {{{0}}}", reduce);
+ }
+
+ [JsonName("map")]
+ public string Map { get; set; }
+
+ [JsonName("reduce")]
+ public string Reduce { get; set; }
+ }
+}
View
19 src/EasyCouchDB/Views/ViewDocument.cs
@@ -0,0 +1,19 @@
+using JsonFx.Json;
+
+namespace EasyCouchDB.Views
+{
+ public class ViewDocument : Document<string>
+ {
+ public ViewDocument(string designDocName)
+ {
+ Id = string.Format("_design/easycouchdb_{0}", designDocName);
+ Language = "javascript";
+ }
+
+ [JsonName("language")]
+ public string Language { get; set; }
+
+ [JsonName("views")]
+ public Views Views { get; set; }
+ }
+}
View
64 src/EasyCouchDB/Views/ViewManager.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using EasyCouchDB.Infrastructure;
+using EasyHttp.Http;
+
+namespace EasyCouchDB.Views
+{
+ public class ViewManager : IViewManager
+ {
+ // Due to limitations, we are treating each View as a separate design document and
+ // the name of the view refers to the design document
+
+ readonly ICouchServer _server;
+
+ public ViewManager(ICouchServer server)
+ {
+ _server = server;
+ }
+
+ public void CreateView(string viewName, string mapCode, string reduceCode = "")
+ {
+ var mapReduce = new MapReduce(mapCode, reduceCode);
+
+ var views = new Views(mapReduce);
+
+ var viewDocument = new ViewDocument(viewName) {Views = views};
+
+ HttpResponse response = _server.Put(GetDesignDocUri(viewName), viewDocument);
+
+ if (response.StatusCode != HttpStatusCode.Created)
+ {
+ throw new DocumentUpdateException(response.StatusDescription);
+ }
+ }
+
+ public bool ViewExists(string viewName)
+ {
+ HttpResponse response = _server.Head(GetDesignDocUri(viewName));
+
+ return response.StatusCode == HttpStatusCode.OK;
+ }
+
+ public IEnumerable<TDocument> ExecuteView<TDocument>(string viewName)
+ {
+ HttpResponse response = _server.Get(GetDesignDocUri(viewName) + "/_view/mapreduce");
+
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ throw new ViewException(response.StatusDescription);
+ }
+
+ var wrapper = response.StaticBody<MultiRowResponseWrapperForDocs<TDocument>>();
+
+ return wrapper.Rows.Select(t => t.Document).ToList();
+ }
+
+ static string GetDesignDocUri(string viewName)
+ {
+ return String.Format("_design/easycouchdb_view_{0}", viewName);
+ }
+ }
+}
View
15 src/EasyCouchDB/Views/Views.cs
@@ -0,0 +1,15 @@
+using JsonFx.Json;
+
+namespace EasyCouchDB.Views
+{
+ public class Views
+ {
+ public Views(MapReduce mapReduce)
+ {
+ MapReduce = mapReduce;
+ }
+
+ [JsonName("mapreduce")]
+ public MapReduce MapReduce { get; set; }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.