示例#1
0
        public async Task <IActionResult> Get(long id, [FromHeader(Name = HeaderNames.IfNoneMatch)] string ifNoneMatch)
        {
            try
            {
                var objectDescriptor = await _objectsStorageReader.GetObjectDescriptor(id, null, CancellationToken.None);

                Response.Headers[HeaderNames.ETag]         = $"\"{objectDescriptor.VersionId}\"";
                Response.Headers[HeaderNames.LastModified] = objectDescriptor.LastModified.ToString("R");

                if (!string.IsNullOrEmpty(ifNoneMatch) && ifNoneMatch.Trim('"') == objectDescriptor.VersionId)
                {
                    return(NotModified());
                }

                return(Json(
                           new
                {
                    objectDescriptor.Id,
                    objectDescriptor.VersionId,
                    objectDescriptor.LastModified,
                    objectDescriptor.TemplateId,
                    objectDescriptor.TemplateVersionId,
                    objectDescriptor.Language,
                    objectDescriptor.Metadata.Author,
                    objectDescriptor.Metadata.AuthorLogin,
                    objectDescriptor.Metadata.AuthorName,
                    objectDescriptor.Properties,
                    objectDescriptor.Elements
                }));
            }
            catch (ObjectNotFoundException)
            {
                return(NotFound());
            }
        }
示例#2
0
        public async Task <string> Modify(long id, string versionId, AuthorInfo authorInfo, IObjectDescriptor modifiedObjectDescriptor)
        {
            CheckRequiredProperties(id, modifiedObjectDescriptor);

            if (string.IsNullOrEmpty(versionId))
            {
                throw new InputDataValidationException("Object version must be set");
            }

            using (await _distributedLockManager.AcquireLockAsync(id))
            {
                var objectDescriptor = await _objectsStorageReader.GetObjectDescriptor(id, null, CancellationToken.None);

                if (!versionId.Equals(objectDescriptor.VersionId, StringComparison.OrdinalIgnoreCase))
                {
                    throw new ConcurrencyException(id, versionId, objectDescriptor.VersionId);
                }

                if (modifiedObjectDescriptor.TemplateId != objectDescriptor.TemplateId)
                {
                    throw new ObjectInconsistentException(
                              id,
                              $"Modified and latest objects templates do not match ({modifiedObjectDescriptor.TemplateId} and {objectDescriptor.TemplateId}).");
                }

                if (!string.Equals(modifiedObjectDescriptor.TemplateVersionId, objectDescriptor.TemplateVersionId, StringComparison.OrdinalIgnoreCase))
                {
                    throw new ObjectInconsistentException(
                              id,
                              $"Modified and latest objects template versions do not match ({modifiedObjectDescriptor.TemplateVersionId} and {objectDescriptor.TemplateVersionId}).");
                }

                if (modifiedObjectDescriptor.Language != objectDescriptor.Language)
                {
                    throw new ObjectInconsistentException(
                              id,
                              $"Modified and latest objects languages do not match ({modifiedObjectDescriptor.Language} and {objectDescriptor.Language}).");
                }

                var modifiedElementsIds = new HashSet <long>(modifiedObjectDescriptor.Elements.Select(x => x.Id));
                if (modifiedElementsIds.Count != modifiedObjectDescriptor.Elements.Count)
                {
                    throw new ObjectInconsistentException(id, "Some elements have non-unique identifiers.");
                }

                var currentElementsIds = new HashSet <long>(objectDescriptor.Elements.Select(x => x.Id));
                if (!modifiedElementsIds.IsSubsetOf(currentElementsIds))
                {
                    throw new ObjectInconsistentException(id, "Modified object contains non-existing elements.");
                }

                EnsureObjectElementsState(id, objectDescriptor.Elements, modifiedObjectDescriptor.Elements);

                return(await PutObject(id, versionId, objectDescriptor.VersionIndex, authorInfo, objectDescriptor.Elements, modifiedObjectDescriptor, currentElementsIds));
            }
        }
示例#3
0
        public async Task <string> Modify(long id, string versionId, AuthorInfo authorInfo, IObjectDescriptor modifiedObjectDescriptor)
        {
            CheckRequredProperties(id, modifiedObjectDescriptor);

            if (string.IsNullOrEmpty(versionId))
            {
                throw new ArgumentException("Object version must be set", nameof(versionId));
            }

            using (await _distributedLockManager.AcquireLockAsync(id))
            {
                var objectDescriptor = await _objectsStorageReader.GetObjectDescriptor(id, null, CancellationToken.None);

                if (objectDescriptor == null)
                {
                    throw new ObjectNotFoundException($"Object '{id}' not found.");
                }

                if (!versionId.Equals(objectDescriptor.VersionId, StringComparison.OrdinalIgnoreCase))
                {
                    throw new ConcurrencyException(id, versionId, objectDescriptor.VersionId);
                }

                var currentTemplateIds  = new HashSet <long>(objectDescriptor.Elements.Select(x => x.Id));
                var modifiedTemplateIds = new HashSet <long>(modifiedObjectDescriptor.Elements.Select(x => x.Id));
                if (!modifiedTemplateIds.IsSubsetOf(currentTemplateIds))
                {
                    throw new ObjectInconsistentException(id, "Modified object contains non-existing elements.");
                }

                EnsureObjectElementsState(id, objectDescriptor.Elements, modifiedObjectDescriptor.Elements);

                return(await PutObject(id, versionId, authorInfo, objectDescriptor.Elements, modifiedObjectDescriptor));
            }
        }