Esempio n. 1
0
        /// <inheritdoc />
        public async Task <TSource> AddOrUpdateAsync(TSource document, bool batch = false, CancellationToken cancellationToken = default)
        {
            Check.NotNull(document, nameof(document));

            if (string.IsNullOrEmpty(document.Id))
            {
                throw new InvalidOperationException("Cannot add or update a document without an ID.");
            }

            IFlurlRequest request = NewRequest()
                                    .AppendPathSegment(document.Id);

            if (batch)
            {
                request = request.SetQueryParam("batch", "ok");
            }

            DocumentSaveResponse response = await request
                                            .PutJsonAsync(document, cancellationToken)
                                            .ReceiveJson <DocumentSaveResponse>()
                                            .SendRequestAsync()
                                            .ConfigureAwait(false);

            document.ProcessSaveResponse(response);

            await UpdateAttachments(document, cancellationToken)
            .ConfigureAwait(false);

            return(document);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public async Task <TSource> AddAsync(TSource document, bool batch = false, CancellationToken cancellationToken = default)
        {
            Check.NotNull(document, nameof(document));

            if (!string.IsNullOrEmpty(document.Id))
            {
                return(await AddOrUpdateAsync(document, batch, cancellationToken)
                       .ConfigureAwait(false));
            }

            IFlurlRequest request = NewRequest();

            if (batch)
            {
                request = request.SetQueryParam("batch", "ok");
            }

            DocumentSaveResponse response = await request
                                            .PostJsonAsync(document, cancellationToken)
                                            .ReceiveJson <DocumentSaveResponse>()
                                            .SendRequestAsync()
                                            .ConfigureAwait(false);

            document.ProcessSaveResponse(response);

            await UpdateAttachments(document, cancellationToken)
            .ConfigureAwait(false);

            return(document);
        }
Esempio n. 3
0
        /// <summary>
        /// Update or Create document
        /// </summary>
        /// <param name="documentSaveRequest"></param>
        /// <returns></returns>
        public static DocumentSaveResponse DocumentSave(DocumentSaveRequest documentSaveRequest)
        {
            var repDocSaveResp = RepDocument.Save(
                documentSaveRequest.headerInfo,
                documentSaveRequest.inDocument,
                documentSaveRequest.saveType);

            var documentSaveResponse = new DocumentSaveResponse();

            documentSaveResponse.document     = documentSaveRequest.inDocument;
            documentSaveResponse.document.UID = repDocSaveResp;
            if (repDocSaveResp == 0)
            {
                documentSaveResponse.response            = new ResponseStatus(MessageType.Error);
                documentSaveResponse.response.Message    = "Error Saving Document.";
                documentSaveResponse.response.Successful = false;
                documentSaveResponse.response.ReturnCode = -0010;
            }
            else
            {
                documentSaveResponse.response = new ResponseStatus(MessageType.Informational);
            }

            return(documentSaveResponse);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a new document and returns it.
        /// </summary>
        /// <param name="document">The document to create.</param>
        /// <param name="batch">Stores document in batch mode.</param>
        /// <returns>A task that represents the asynchronous operation. The task result contains the element created.</returns>
        public async Task <TSource> CreateAsync(TSource document, bool batch = false)
        {
            if (!string.IsNullOrEmpty(document.Id))
            {
                return(await CreateOrUpdateAsync(document)
                       .ConfigureAwait(false));
            }

            IFlurlRequest request = NewRequest();

            if (batch)
            {
                request = request.SetQueryParam("batch", "ok");
            }

            DocumentSaveResponse response = await request
                                            .PostJsonAsync(document)
                                            .ReceiveJson <DocumentSaveResponse>()
                                            .SendRequestAsync()
                                            .ConfigureAwait(false);

            document.ProcessSaveResponse(response);

            await UpdateAttachments(document)
            .ConfigureAwait(false);

            return(document);
        }
Esempio n. 5
0
        public static void ProcessSaveResponse(this CouchDocument item, DocumentSaveResponse response)
        {
            if (!response.Ok)
            {
                throw new CouchException(response.Error, response.Reason);
            }

            item.Id  = response.Id;
            item.Rev = response.Rev;
        }
Esempio n. 6
0
        /// <summary>
        /// Creates or updates the document with the given ID.
        /// </summary>
        /// <param name="document">The document to create or update</param>
        /// <param name="batch">Stores document in batch mode.</param>
        /// <returns>A task that represents the asynchronous operation. The task result contains the element created or updated.</returns>
        public async Task <TSource> CreateOrUpdateAsync(TSource document, bool batch = false)
        {
            if (string.IsNullOrEmpty(document.Id))
            {
                throw new InvalidOperationException("Cannot add or update a document without an ID.");
            }

            IFlurlRequest request = NewRequest()
                                    .AppendPathSegment(document.Id);

            if (batch)
            {
                request = request.SetQueryParam("batch", "ok");
            }

            DocumentSaveResponse response = await request
                                            .PutJsonAsync(document)
                                            .ReceiveJson <DocumentSaveResponse>()
                                            .SendRequestAsync()
                                            .ConfigureAwait(false);

            document.ProcessSaveResponse(response);
            return(document);
        }