Пример #1
0
        /// <summary>
        /// Saves entity into the database.
        /// </summary>
        /// <param name="entity">Instance of entity to be saved.</param>
        /// <param name="entityUpdateParams">Additional parameters for saving.</param>
        /// <returns>Awaitable task.</returns>
        /// <exception cref="ArgumentNullException">Required parameter is null or empty.</exception>
        /// <exception cref="CouchDBClientException">Error response received from CouchDB server.</exception>
        public async Task SaveEntityAsync(IEntity entity, DocUpdateParams entityUpdateParams = null)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            var jsonEntity = EntityHelper.ConvertEntityToJSON(entity);

            await _db.SaveJsonDocumentAsync(jsonEntity, entityUpdateParams).Safe();

            entity._id  = jsonEntity[CouchDBDatabase.IdPropertyName]?.ToString();
            entity._rev = jsonEntity[CouchDBDatabase.RevisionPropertyName]?.ToString();
        }
        /// <summary>
        /// Creates a new named document, or creates a new revision of the existing document.
        /// </summary>
        /// <param name="this">Instance of <see cref="ICouchDBDatabase"/>.</param>
        /// <param name="documentObject">Document object to be saved.</param>
        /// <param name="updateParams">Query parameters for updating document.</param>
        /// <returns><see cref="SaveDocResponse"/> with operation results in it.</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 <SaveDocResponse> SaveObjectDocumentAsync(this ICouchDBDatabase @this, object documentObject, DocUpdateParams updateParams = null)
        {
            if (documentObject == null)
            {
                throw new ArgumentNullException(nameof(documentObject));
            }

            var documentJsonObject = JObject.FromObject(documentObject);

            if (string.IsNullOrWhiteSpace(documentJsonObject[CouchDBDatabase.IdPropertyName]?.ToString()))
            {
                documentJsonObject.Remove(CouchDBDatabase.IdPropertyName);
            }

            return(await @this.SaveStringDocumentAsync(documentJsonObject.ToString(), updateParams).Safe());
        }
        /// <summary>
        /// Creates a new named document, or creates a new revision of the existing document.
        /// </summary>
        /// <param name="this">Instance of <see cref="ICouchDBDatabase"/>.</param>
        /// <param name="documentJsonObject">JSON of document to be saved.</param>
        /// <param name="updateParams">Query parameters for updating document.</param>
        /// <returns>Awaitable task.</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 SaveJsonDocumentAsync(this ICouchDBDatabase @this, JObject documentJsonObject, DocUpdateParams updateParams = null)
        {
            if (documentJsonObject == null)
            {
                throw new ArgumentNullException(nameof(documentJsonObject));
            }

            var saveResponse = await @this.SaveStringDocumentAsync(documentJsonObject.ToString(), updateParams).Safe();

            documentJsonObject[CouchDBDatabase.IdPropertyName]       = saveResponse.Id;
            documentJsonObject[CouchDBDatabase.RevisionPropertyName] = saveResponse.Revision;
        }
Пример #4
0
        /// <summary>
        /// Creates a new named document, or creates a new revision of the existing document.
        /// </summary>
        /// <param name="documentJsonString">JSON of document to be saved.</param>
        /// <param name="updateParams">Query parameters for updating document.</param>
        /// <returns><see cref="SaveDocResponse"/> with operation results in it.</returns>
        /// <exception cref="ArgumentNullException">Required parameter is null or empty.</exception>
        /// <exception cref="CouchDBClientException">Error response received from CouchDB server.</exception>
        public async Task <SaveDocResponse> SaveStringDocumentAsync(string documentJsonString, DocUpdateParams updateParams = null)
        {
            if (string.IsNullOrWhiteSpace(documentJsonString))
            {
                throw new ArgumentNullException(nameof(documentJsonString));
            }

            var newDocUrl = QueryParams.AppendQueryParams(string.Empty, updateParams);
            var response  = await _handler.SendRequestAsync(newDocUrl, RequestMethod.POST, Request.JsonString(documentJsonString)).Safe();

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

            var docResponseDTO = await response.ReadAsAsync <SaveDocResponseDTO>(false).Safe();

            return(new SaveDocResponse(docResponseDTO));
        }