Beispiel #1
0
        /// <summary>
        /// Process batch for CRUD operations.  All batches are run the same way.
        /// </summary>
        /// <param name="batch">Object submitted to perform the operation.  Differs for each operation, see non-batch methods for specific type required. </param>
        /// <param name="operation">Enumerated type of operation to run.</param>
        /// <returns></returns>
        private async Task <List <WHResponse> > RunBatchAsync(IEnumerable <object> batch, OpsType operation)
        {
            try
            {
                //initialize query object
                IEnumerable <Task <WHResponse> > iQuery = null;

                // create a query to get each doc object from collection submitted (cannot use switch due to select statement)
                if (operation == OpsType.AddDoc)
                {
                    iQuery = from docObj in batch select AddDocAsync(docObj);
                }
                else
                {
                    if (operation == OpsType.DeleteDoc)
                    {
                        iQuery = from docID in (batch as List <string>) select DeleteDocAsync(docID);
                    }
                    else
                    {
                        if (operation == OpsType.ReplaceDoc)
                        {
                            iQuery = from docObj in batch select ReplaceDocAsync(docObj);
                        }
                        else        //(operation == OpsType.UpdateDoc)
                        {
                            //iQuery = from docObj in batch select UpdateDocAsync(docObj);
                        }
                    }
                }

                // execute the query into an array of tasks
                Task <WHResponse>[] iTasks = iQuery.ToArray();

                // load the results of each task into an array
                WHResponse[] results = await Task.WhenAll(iTasks);

                // iterate array to list for performance
                List <WHResponse> respList = new List <WHResponse>();
                for (int i = 0; i < results.Length; i++)
                {
                    respList.Add(results[i]);
                }
                //return results as a list
                return(respList);
            }
            catch (Exception ex)
            {
                // return bad request
                List <WHResponse> respList = new List <WHResponse>();
                respList.Add(new WHResponse(WHResponse.ResponseCodes.BadRequest, null, true, ex.Message, ex));
                return(respList);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Perform document operation (add, update or replace) on the database.
        /// </summary>
        /// <param name="newDoc">The document to be created. Can be anything that evaluates to JSON: a JSON document or string, XML document or string,
        /// a POCO (plain old CLR object), or just a string that converts to JSON</param>
        /// <param name="operation">The enumerated operation to perform (add, update, replace).</param>
        /// <param name="selfID">The selfID of the document to replace (replace operation only).</param>
        /// <returns>String containing the ID of the document that was added. </returns>
        private async Task <WHResponse> DocOpsAsync(object newDoc, OpsType operation, string selfID = null)
        {
            try
            {
                // if the document is empty, return bad request
                if (newDoc.ToString().Replace(" ", "") == EMPTY_DOC)
                {
                    return(new WHResponse(WHResponse.ResponseCodes.BadRequest, null, true, DOC_NULL));
                }
                else
                {
                    // if the parameter passed was a string and not a formal object
                    if (newDoc is string)
                    {
                        try
                        {
                            //try converting to JSON object and reassigning
                            JObject jStr = JObject.Parse(newDoc.ToString());
                            newDoc = jStr;
                        }
                        catch (Exception jEx)
                        {
                            //if string is not XML
                            string testStr = newDoc as string;
                            if (testStr.First() != Convert.ToChar("<"))
                            {
                                // return invalid string
                                return(new WHResponse(WHResponse.ResponseCodes.BadRequest, null, true, BAD_STRING, jEx));
                            }
                            else
                            {
                                try
                                {
                                    XmlDocument docXML = new XmlDocument();
                                    docXML.InnerXml = newDoc.ToString();
                                    newDoc          = docXML;
                                }
                                catch (Exception xEx)
                                {
                                    // return invalid string
                                    return(new WHResponse(WHResponse.ResponseCodes.BadRequest, null, true, BAD_STRING, xEx));
                                }
                            }
                        }
                    }
                    try
                    {
                        switch (operation)
                        {
                        case OpsType.AddDoc:

                            // call create document method and return ID of created document
                            Document created = await Client.CreateDocumentAsync(CollectionID, newDoc);

                            return(new WHResponse(WHResponse.ResponseCodes.SuccessAdd, created.Id, false, null, null, WHResponse.ContentType.Text, created.SelfLink));

                        //case OpsType.UpdateDoc:
                        //    break;

                        case OpsType.ReplaceDoc:

                            // call create document method and return ID of created document
                            created = await Client.ReplaceDocumentAsync(selfID, newDoc);

                            return(new WHResponse(WHResponse.ResponseCodes.SuccessGetOrUpdate, created.Id, false, null, null, WHResponse.ContentType.Text, created.SelfLink));

                        default:
                            return(new WHResponse(WHResponse.ResponseCodes.BadRequest, null, true, BAD_STRING));
                        }
                    }
                    catch (DocumentClientException docEx)
                    {
                        // if there is a conflict, return error message
                        if (docEx.Error.Code.ToLower() == CONFLICT_TEXT)
                        {
                            return(new WHResponse(WHResponse.ResponseCodes.Conflict, newDoc.ToString(), true, string.Concat(CONFLICT_MSG, docEx.Message), docEx, WHResponse.ContentType.Text));
                        }
                        // if document not found, return error message
                        if (docEx.Error.Code.ToLower() == NOTFOUND_TEXT)
                        {
                            return(new WHResponse(WHResponse.ResponseCodes.NotFound, newDoc.ToString(), true, string.Concat(NOTFOUND_MSG, docEx.Message), docEx, WHResponse.ContentType.Text));
                        }
                        //throw any other exceptions
                        throw;
                    }
                    catch (Exception)
                    {
                        //throw any other exceptions
                        throw;
                    }
                }
            }
            catch (ApplicationException appEx)
            {
                // return invalid client
                return(new WHResponse(WHResponse.ResponseCodes.BadRequest, null, true, appEx.Message, appEx));
            }
            catch (Exception)
            {
                throw;
            }
        }