Exemplo n.º 1
0
        /// <summary>
        /// Returns document by the specified docid from the specified db.
        /// Unless you request a specific revision, the latest revision of the document will always be returned.
        /// </summary>
        /// <param name="docId">Document ID.</param>
        /// <param name="queryParams">Additional query parameters for retrieving document.</param>
        /// <returns><see cref="string"/> containing document JSON.</returns>
        /// <exception cref="ArgumentNullException">Required parameter is null or empty.</exception>
        /// <exception cref="CouchDBClientException">Error response received from CouchDB server.</exception>
        public async Task <string> GetStringDocumentAsync(string docId, DocQueryParams queryParams = null)
        {
            if (string.IsNullOrWhiteSpace(docId))
            {
                throw new ArgumentNullException(nameof(docId));
            }

            var docQuery = QueryParams.AppendQueryParams(docId, queryParams);
            var response = await _handler.SendRequestAsync(docQuery, RequestMethod.GET, Request.Empty).Safe();

            if (response == null)
            {
                return(null);
            }

            return(await response.ReadAsStringAsync(true).Safe());
        }
        /// <summary>
        /// Returns document by the specified docid from the specified db.
        /// Unless you request a specific revision, the latest revision of the document will always be returned.
        /// </summary>
        /// <param name="this">Instance of <see cref="ICouchDBDatabase"/>.</param>
        /// <param name="docId">Document ID.</param>
        /// <param name="queryParams">Additional query parameters for retrieving document.</param>
        /// <returns><see cref="JObject"/> containing document JSON.</returns>
        /// <exception cref="ArgumentNullException">Required parameter is null or empty.</exception>
        /// <exception cref="CouchDBClientException">Error response received from CouchDB server.</exception>
        public static async Task <JObject> GetJsonDocumentAsync(this ICouchDBDatabase @this, string docId, DocQueryParams queryParams = null)
        {
            if (string.IsNullOrWhiteSpace(docId))
            {
                throw new ArgumentNullException(nameof(docId));
            }

            var jsonString = await @this.GetStringDocumentAsync(docId, queryParams).Safe();

            var jsonObject = jsonString != null
                ? JObject.Parse(jsonString)
                : null;

            return(jsonObject);
        }
        /// <summary>
        /// Returns document by the specified docid from the specified db.
        /// Unless you request a specific revision, the latest revision of the document will always be returned.
        /// </summary>
        /// <param name="this">Instance of <see cref="ICouchDBDatabase"/>.</param>
        /// <typeparam name="TResult">Specify type to which the document will be deserialized.</typeparam>
        /// <param name="docId">Document ID.</param>
        /// <param name="queryParams">Additional query parameters for retrieving document.</param>
        /// <returns>Object containing deserialized document.</returns>
        /// <exception cref="ArgumentNullException">Required parameter is null or empty.</exception>
        /// <exception cref="CouchDBClientException">Error response received from CouchDB server.</exception>
        public static async Task <TResult> GetObjectDocumentAsync <TResult>(this ICouchDBDatabase @this, string docId, DocQueryParams queryParams = null)
        {
            if (string.IsNullOrWhiteSpace(docId))
            {
                throw new ArgumentNullException(nameof(docId));
            }

            var jsonString = await @this.GetStringDocumentAsync(docId, queryParams).Safe();

            var resultObject = jsonString != null
                ? JsonConvert.DeserializeObject <TResult>(jsonString)
                : default(TResult);

            return(resultObject);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Retrieves entity from database.
 /// </summary>
 /// <typeparam name="TEntity">Type of entity to be retrieved.</typeparam>
 /// <param name="entityId">ID of entity to be retrieved.</param>
 /// <param name="entityQueryParams">Additional parameters for retrieving.</param>
 /// <returns>Entity.</returns>
 /// <exception cref="ArgumentNullException">Required parameter is null or empty.</exception>
 /// <exception cref="CouchDBClientException">Error response received from CouchDB server.</exception>
 public async Task <TEntity> GetEntityAsync <TEntity>(string entityId, DocQueryParams entityQueryParams = null)
     where TEntity : IEntity
 {
     return(await _db.GetObjectDocumentAsync <TEntity>(entityId, entityQueryParams).Safe());
 }