/// <summary> /// Inserts a new Document object into a Document Collection. /// </summary> /// <param name="req">The request object.</param> /// <param name="token">The cancellation token.</param> /// <returns></returns> /// <exception cref="ArgumentException">collection cannot be null or blank</exception> private async Task<object> OnInsertDocumentAsync(dynamic req, CancellationToken token) { var stopwatch = new Stopwatch(); stopwatch.Start(); var collectionName = (string)req["collection"]; if (String.IsNullOrWhiteSpace(collectionName)) throw new ArgumentException("collection cannot be null or blank"); // Get the Document object from the request. // Note: Bind() will get resolved to DynamicModelBinder.Bind(). var excludedFields = new[] { "collection" }; var dictionary = this.Bind<DynamicDictionary>(excludedFields).ToDictionary(); var document = new Document(dictionary); // Get the target Document Collection; it will be auto-created if it doesn't exist. var collection = _database[collectionName]; // Insert the Document object into the target Document Collection. var docId = await collection.InsertAsync(document).ConfigureAwait(false); stopwatch.Stop(); var responseDto = this.BuildInsertResponseDto(collectionName, docId, stopwatch.Elapsed); return Response.AsJson(responseDto); }
/// <summary> /// Replaces a Document object identified by its _id, in a Document Collection. /// </summary> /// <param name="req">The request object.</param> /// <param name="token">The cancellation token.</param> /// <returns></returns> /// <exception cref="System.InvalidOperationException">There is no data for this operation</exception> private async Task<object> OnReplaceDocumentAsync(dynamic req, CancellationToken token) { var stopwatch = new Stopwatch(); stopwatch.Start(); var collectionName = (string)req["collection"]; if (String.IsNullOrWhiteSpace(collectionName)) throw new ArgumentException("collection cannot be null or blank"); if (!_database.ContainsCollection(collectionName)) return HttpStatusCode.NotFound; var guid = (Guid)req["id"]; if (guid == Guid.Empty) throw new ArgumentException("id cannot be Guid.Empty"); var collection = _database[collectionName]; var existingDoc = await collection.GetAsync(guid); if (existingDoc == null) return HttpStatusCode.NotFound; var excludedFields = new[] { "collection", "id" }; var dictionary = this.Bind<DynamicDictionary>(excludedFields).ToDictionary(); if (dictionary == null || dictionary.Count == 0) throw new InvalidOperationException("There is no data for this operation"); var document = new Document(dictionary); document._id = guid; if (document._modifiedTimestamp != null && document._modifiedTimestamp != existingDoc._modifiedTimestamp) throw new InvalidOperationException("The document was modified by another user or process."); var updatedCount = await collection.UpdateAsync(document).ConfigureAwait(false); stopwatch.Stop(); var responseDto = this.BuildUpdateResponseDto(collectionName, updatedCount, stopwatch.Elapsed); return Response.AsJson(responseDto); }