public async Task <IActionResult> DeleteById(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceType.Media, id))
            {
                return(NotFound());
            }

            var status = _entityIndex.Status(ResourceType.Media, id).GetValueOrDefault();

            if (!UserPermissions.IsAllowedToDelete(User.Identity, status, _entityIndex.Owner(ResourceType.Media, id)))
            {
                return(Forbid());
            }

            if (!UserPermissions.IsAllowedToDelete(User.Identity, status, _entityIndex.Owner(ResourceType.Media, id)))
            {
                return(BadRequest(ErrorMessages.CannotBeDeleted(ResourceType.Media, id)));
            }

            if (status == ContentStatus.Published)
            {
                return(BadRequest(ErrorMessages.CannotBeDeleted(ResourceType.Media, id)));
            }

            if (_referencesIndex.IsUsed(ResourceType.Media, id))
            {
                return(BadRequest(ErrorMessages.ResourceInUse));
            }

            // Remove file
            var directoryPath = Path.GetDirectoryName(_mediaIndex.GetFilePath(id));

            if (directoryPath != null && Directory.Exists(directoryPath))
            {
                Directory.Delete(directoryPath, true);
            }

            var ev = new MediaDeleted
            {
                Id        = id,
                UserId    = User.Identity.GetUserIdentity(),
                Timestamp = DateTimeOffset.Now
            };

            await _eventStore.AppendEventAsync(ev);

            await InvalidateThumbnailCacheAsync(id);

            return(StatusCode(204));
        }
示例#2
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] ExhibitPageArgs2 args)
        {
            // if font family is not specified, fallback to the configured default font family
            if (args != null && args.FontFamily == null)
            {
                args.FontFamily = _exhibitPagesConfig.Value.DefaultFontFamily;
            }

            ValidateExhibitPageArgs(args);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceType.ExhibitPage, id))
            {
                return(NotFound());
            }

            // ReSharper disable once PossibleNullReferenceException (args == null is handled through ModelState.IsValid)
            if (!UserPermissions.IsAllowedToEdit(User.Identity, args.Status, _entityIndex.Owner(ResourceType.ExhibitPage, id)))
            {
                return(Forbid());
            }

            var oldStatus = _entityIndex.Status(ResourceType.ExhibitPage, id).GetValueOrDefault();

            if (args.Status == ContentStatus.Unpublished && oldStatus != ContentStatus.Published)
            {
                return(BadRequest(ErrorMessages.CannotBeUnpublished(ResourceType.ExhibitPage)));
            }

            // ReSharper disable once PossibleInvalidOperationException (.Value is safe here since we know the entity exists)
            var currentPageType = _exhibitPageIndex.PageType(id).Value;

            // ReSharper disable once PossibleNullReferenceException (args == null is handled through ModelState.IsValid)
            if (currentPageType != args.Type)
            {
                return(StatusCode(422, ErrorMessages.CannotChangeExhibitPageType(currentPageType, args.Type)));
            }

            // validation passed, emit event
            var ev = new ExhibitPageUpdated3
            {
                Id         = id,
                UserId     = User.Identity.GetUserIdentity(),
                Properties = args,
                Timestamp  = DateTimeOffset.Now
            };

            await _eventStore.AppendEventAsync(ev);

            return(StatusCode(204));
        }
示例#3
0
        /// <summary>
        /// Obtains reference information for a resource.
        /// </summary>
        /// <returns>
        /// "200 Ok" with reference info if successful.
        /// "404 Not Found" if no resource with the specified type and ID exists.
        /// </returns>
        public static IActionResult GetReferenceInfo(ResourceType type, int id, EntityIndex entityIndex, ReferencesIndex referencesIndex)
        {
            if (!entityIndex.Exists(type, id))
            {
                return(new NotFoundResult());
            }

            return(new OkObjectResult(new ReferenceInfoResult
            {
                OutgoingReferences = TransformToResult(referencesIndex.ReferencesOf(type, id)),
                IncomingReferences = TransformToResult(referencesIndex.ReferencesTo(type, id))
            }));

            IReadOnlyCollection <ReferenceInfoResult.ReferenceInfo> TransformToResult(IEnumerable <EntityId> refs)
            {
                return(refs
                       .GroupBy(entry => entry.Type)
                       .Select(group => new ReferenceInfoResult.ReferenceInfo
                {
                    Type = group.Key.Name,
                    Ids = group.Select(e => e.Id).OrderBy(i => i).ToList()
                })
                       .OrderBy(group => group.Type)
                       .ToList());
            }
        }
示例#4
0
        public async Task <IActionResult> UpdateByIdAsync(int id, [FromBody] TagArgs args)
        {
            ValidateTagArgs(args);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceType.Tag, id))
            {
                return(NotFound());
            }

            if (!UserPermissions.IsAllowedToEdit(User.Identity, args.Status, _entityIndex.Owner(ResourceType.Tag, id)))
            {
                return(Forbid());
            }

            var oldStatus = _entityIndex.Status(ResourceType.Tag, id).GetValueOrDefault();

            if (args.Status == ContentStatus.Unpublished && oldStatus != ContentStatus.Published)
            {
                return(BadRequest(ErrorMessages.CannotBeUnpublished(ResourceType.Tag)));
            }

            var tagIdWithSameTitle = _tagIndex.GetIdByTagTitle(args.Title);

            if (tagIdWithSameTitle != null && tagIdWithSameTitle != id)
            {
                return(StatusCode(409, ErrorMessages.TagNameAlreadyUsed));
            }

            var ev = new TagUpdated
            {
                Id         = id,
                UserId     = User.Identity.GetUserIdentity(),
                Properties = args,
                Timestamp  = DateTimeOffset.Now,
            };

            await _eventStore.AppendEventAsync(ev);

            return(NoContent());
        }
        public async Task <IActionResult> PutAsync(int id, [FromBody] FooArgs args)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceTypes.Foo, id))
            {
                return(NotFound());
            }

            var oldFoo = await _eventStore.EventStream.GetCurrentEntityAsync <FooArgs>(ResourceTypes.Foo, id);

            await EntityManager.UpdateEntityAsync(_eventStore, oldFoo, args, ResourceTypes.Foo, id, User.Identity.GetUserIdentity());

            return(NoContent());
        }
        public async Task <IActionResult> PutAsync(int id, [FromBody] RouteArgs args)
        {
            ValidateRouteArgs(args);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceType.Route, id))
            {
                return(NotFound());
            }

            if (!UserPermissions.IsAllowedToEdit(User.Identity, args.Status, _entityIndex.Owner(ResourceType.Route, id)))
            {
                return(Forbid());
            }

            var oldStatus = _entityIndex.Status(ResourceType.Route, id).GetValueOrDefault();

            if (args.Status == ContentStatus.Unpublished && oldStatus != ContentStatus.Published)
            {
                return(BadRequest(ErrorMessages.CannotBeUnpublished(ResourceType.Route)));
            }

            // validation passed, emit event
            var ev = new RouteUpdated
            {
                Id         = id,
                UserId     = User.Identity.GetUserIdentity(),
                Properties = args,
                Timestamp  = DateTimeOffset.Now,
            };

            await _eventStore.AppendEventAsync(ev);

            return(StatusCode(204));
        }