Skip to content
Browse files

Update for making requests on design views for documents.

  • Loading branch information...
1 parent 4696657 commit 494e78e43e95abe7297e4c9a0a74f060cf340bbc Todd Anderson committed Mar 26, 2010
View
BIN bin/as3couchdb.swc
Binary file not shown.
View
15 src/com/custardbelly/as3couchdb/core/CouchDatabase.as
@@ -143,11 +143,24 @@ package com.custardbelly.as3couchdb.core
/**
* Invokes action mediator to return all documents related to database resolved as the supplied class type.
- * @param documentClass String
+ * @param documentClass String The fully qualified Class name. This is used to resolve results to a specified type of model.
*/
public function getAllDocuments( documentClass:String ):void
{
_actionMediator.handleGetAllDocuments( documentClass );
}
+
+ /**
+ * Invokes action mediator to return all documents returned from a design document view, filter by optional key value.
+ * @param documentClass String The fully qualified Class name. This is used to resolve results to a sepcified type of model.
+ * @param designDocumentName String The design document name within the database.
+ * @param viewName String The view name to query on which the map/reduce methods reside.
+ * @param keyValue String Optional filter on results by key type.
+ *
+ */
+ public function getDocumentsFromView( documentClass:String, designDocumentName:String, viewName:String, keyValue:String = null ):void
+ {
+ _actionMediator.handleGetDocumentsFromView( documentClass, designDocumentName, viewName, keyValue );
+ }
}
}
View
2 src/com/custardbelly/as3couchdb/enum/CouchActionType.as
@@ -37,6 +37,6 @@ package com.custardbelly.as3couchdb.enum
public static const UPDATE:String = "update";
public static const DELETE:String = "delete";
- public static const READ_ALL:String = "readAll";
+ public static const READ_DOCUMENTS:String = "readDocuments";
}
}
View
16 src/com/custardbelly/as3couchdb/mediator/CouchDatabaseActionMediator.as
@@ -39,6 +39,7 @@ package com.custardbelly.as3couchdb.mediator
import com.custardbelly.as3couchdb.responder.ICouchServiceResponder;
import com.custardbelly.as3couchdb.responder.ReadAllDocumentsResponder;
import com.custardbelly.as3couchdb.responder.ReadDatabaseResponder;
+ import com.custardbelly.as3couchdb.responder.ReadDocumentsFromViewResponder;
import com.custardbelly.as3couchdb.service.CouchDatabaseService;
import com.custardbelly.as3couchdb.service.ICouchDatabaseService;
import com.custardbelly.as3couchdb.service.ICouchRequest;
@@ -187,12 +188,25 @@ package com.custardbelly.as3couchdb.mediator
/**
* Invokes the ICouchDatabaseService to retrieve all documents from target CouchDatabase and resolve each return as a type of CouchDocument class.
- * @param documentClass String
+ * @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
*/
public function handleGetAllDocuments( documentClass:String ):void
{
var serviceResponder:ReadAllDocumentsResponder = new ReadAllDocumentsResponder( documentClass, _responder );
_service.getAllDocuments( _database.db_name, true, serviceResponder );
}
+
+ /**
+ * Invokes service to request documents based on design view map/reduce with optional key value filter.
+ * @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
+ * @param designDocumentName String
+ * @param viewName String
+ * @param keyValue String
+ */
+ public function handleGetDocumentsFromView( documentClass:String, designDocumentName:String, viewName:String, keyValue:String ):void
+ {
+ var serviceResponder:ReadDocumentsFromViewResponder = new ReadDocumentsFromViewResponder( documentClass, _responder );
+ _service.getDocumentsFromView( _database.db_name, designDocumentName, viewName, keyValue, serviceResponder );
+ }
}
}
View
10 src/com/custardbelly/as3couchdb/mediator/ICouchDatabaseActionMediator.as
@@ -34,8 +34,16 @@ package com.custardbelly.as3couchdb.mediator
function handleCompact( cleanup:Boolean ):void;
/**
* Invokes service to request all documents and resolve each document as a type of supplied class.
- * @param documentClass String
+ * @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
*/
function handleGetAllDocuments( documentClass:String ):void;
+ /**
+ * Invokes service to request documents based on design view map/reduce with optional key value filter.
+ * @param documentClass String The fully qualified Class name. This is used to resolve results to a specific model type.
+ * @param designDocumentName String
+ * @param viewName String
+ * @param keyValue String
+ */
+ function handleGetDocumentsFromView( documentClass:String, designDocumentName:String, viewName:String, keyValue:String ):void;
}
}
View
33 src/com/custardbelly/as3couchdb/responder/ReadAllDocumentsResponder.as
@@ -27,21 +27,28 @@ package com.custardbelly.as3couchdb.responder
public function handleResult( value:CouchServiceResult ):void
{
- // TODO: Check for fault.
- var documents:Array = _databaseReader.getDocumentListFromResult( value.data );
-
- var i:int;
- var document:CouchDocument;
- var documentList:Array = [];
- for( i = 0; i < documents.length; i++ )
+ var result:Object = value.data;
+ if( _databaseReader.isResultAnError( result ) )
{
- // Documents are returned form /_all_docs as {doc:Object, id:String, key:String, value:Object}
- // Supply the doc property to the reader.
- document = _documentReader.createDocumentFromResult( _documentClass, documents[i].doc );
- documentList.push( document );
+ handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
+ }
+ else
+ {
+ var documents:Array = _databaseReader.getDocumentListFromResult( result );
+
+ var i:int;
+ var document:CouchDocument;
+ var documentList:Array = [];
+ for( i = 0; i < documents.length; i++ )
+ {
+ // Documents are returned from /_all_docs as {doc:Object, id:String, key:String, value:Object}
+ // Supply the doc property to the reader.
+ document = _documentReader.createDocumentFromResult( _documentClass, documents[i].doc );
+ documentList.push( document );
+ }
+
+ if( _responder ) _responder.handleResult( new CouchServiceResult( CouchActionType.READ_DOCUMENTS, documentList ) );
}
-
- if( _responder ) _responder.handleResult( new CouchServiceResult( CouchActionType.READ_ALL, documentList ) );
}
public function handleFault( value:CouchServiceFault ):void
View
40 src/com/custardbelly/as3couchdb/responder/ReadDocumentsFromViewResponder.as
@@ -0,0 +1,40 @@
+package com.custardbelly.as3couchdb.responder
+{
+ import com.custardbelly.as3couchdb.core.CouchDocument;
+ import com.custardbelly.as3couchdb.core.CouchServiceFault;
+ import com.custardbelly.as3couchdb.core.CouchServiceResult;
+ import com.custardbelly.as3couchdb.enum.CouchActionType;
+
+ public class ReadDocumentsFromViewResponder extends ReadAllDocumentsResponder
+ {
+ public function ReadDocumentsFromViewResponder(documentClass:String, responder:ICouchServiceResponder)
+ {
+ super(documentClass, responder);
+ }
+
+ override public function handleResult( value:CouchServiceResult ):void
+ {
+ var result:Object = value.data;
+ if( _databaseReader.isResultAnError( result ) )
+ {
+ handleFault( new CouchServiceFault( result["error"], result["reason"] ) );
+ }
+ else
+ {
+ var documents:Array = _databaseReader.getDocumentListFromResult( result );
+
+ var i:int;
+ var document:CouchDocument;
+ var documentList:Array = [];
+ for( i = 0; i < documents.length; i++ )
+ {
+ // Documents are assume to be returned from map/reduce as ( key:id, value:doc ).
+ document = _documentReader.createDocumentFromResult( _documentClass, documents[i].value );
+ documentList.push( document );
+ }
+
+ if( _responder ) _responder.handleResult( new CouchServiceResult( CouchActionType.READ_DOCUMENTS, documentList ) );
+ }
+ }
+ }
+}
View
17 src/com/custardbelly/as3couchdb/service/CouchDatabaseService.as
@@ -24,6 +24,8 @@
* <p>Licensed under The MIT License</p>
* <p>Redistributions of files must retain the above copyright notice.</p>
*/
+// TODO: Support for upload of design documents.
+// TODO: Support for upload of attachements.
package com.custardbelly.as3couchdb.service
{
import com.custardbelly.as3couchdb.enum.CouchContentType;
@@ -251,6 +253,21 @@ package com.custardbelly.as3couchdb.service
}
/**
+ * Makes request on design document in CouchDB instance.
+ * @param id String
+ * @param responder ICouchServiceResponder
+ */
+ public function getDocumentsFromView( databaseName:String, documentName:String, viewName:String, byKeyValue:String = null, responder:ICouchServiceResponder = null ):void
+ {
+ var request:URLRequest = new URLRequest();
+ request.contentType = CouchContentType.JSON;
+ request.url = _baseUrl + "/" + databaseName + "/_design/" + documentName + "/_view/" + viewName;
+ if( byKeyValue ) request.url += "?key=\"" + byKeyValue + "\"";
+
+ makeRequest( request, CouchRequestMethod.GET, responder );
+ }
+
+ /**
* Access an instance of a ICouchDatabaseService based on the url of the CouchDb instance.
* @param baseUrl String
* @return ICouchDatabaseService
View
7 src/com/custardbelly/as3couchdb/service/ICouchDatabaseService.as
@@ -102,7 +102,12 @@ package com.custardbelly.as3couchdb.service
* @param responder ICouchServiceResponder
*/
function getAllDocumentsBySequence( databaseName:String, includeDocs:Boolean = false, responder:ICouchServiceResponder = null ):void;
-
+ /**
+ * Makes request on design document in CouchDB instance.
+ * @param id String
+ * @param responder ICouchServiceResponder
+ */
+ function getDocumentsFromView( databaseName:String, documentName:String, viewName:String, byKeyValue:String = null, responder:ICouchServiceResponder = null ):void;
/**
* Requests the list of databases in the target CouchDB instance.
* @param responder ICouchServiceResponder

0 comments on commit 494e78e

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