Ejemplo n.º 1
0
        private async Task<object> OnPatchContentAsync(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 (!_db.ContainsCollection(collectionName))
                return HttpStatusCode.NotFound;

            var guid = (Guid)req["id"];
            if (guid == Guid.Empty)
                throw new ArgumentException("id cannot be Guid.Empty");

            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 contentPatch = new Content(dictionary);
            var collection = _db[collectionName];

            var count = collection.Count(new SearchCriteria { Query = "_id:" + guid.ToString() });
            if (count == 0)
                return HttpStatusCode.NotFound;

            var content = await collection.GetAsync(guid);
            if (content == null)
                return HttpStatusCode.NotFound;

            var patchDictionary = contentPatch.AsDictionary();
            var contentDictionary = content.AsDictionary();

            var keysToUpdate = patchDictionary.Keys.Except(new[] { "collection", Content.ID_FIELD_NAME, Content.CREATED_TIMESTAMP_FIELD_NAME, Content.MODIFIED_TIMESTAMP_FIELD_NAME });
            foreach (var key in keysToUpdate)            
                contentDictionary[key] = patchDictionary[key];

            var affected = await collection.UpdateAsync(content);

            stopwatch.Stop();

            var responseDto = new UpdateResponseDto
            {
                elapsed = stopwatch.Elapsed.ToString(),
                fromCollection = collectionName,
                affectedCount = affected
            };

            return responseDto;
        }
Ejemplo n.º 2
0
        private async Task<object> OnDeleteContentAsync(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 (!_db.ContainsCollection(collectionName))
                return HttpStatusCode.NotFound;

            var guid = (Guid)req["id"];
            if (guid == Guid.Empty)
                throw new ArgumentException("id cannot be Guid.Empty");

            var collection = _db[collectionName];

            var count = collection.Count(new SearchCriteria { Query = "_id:" + guid.ToString() });
            if (count == 0)
                return HttpStatusCode.NotFound;

            var affected = await collection.DeleteAsync(guid);

            stopwatch.Stop();

            var responseDto = new UpdateResponseDto
            {
                elapsed = stopwatch.Elapsed.ToString(),
                fromCollection = collectionName,
                affectedCount = affected
            };

            return responseDto;
        }