Пример #1
0
        /// <inheritdoc/>
        protected override async Task <bool> ProtectedHandleAsync(DeleteObjectCommand request, CancellationToken cancellationToken)
        {
            var entity = await Context.ObjectRepository
                         .GetFirstOrDefaultAsync(e => e.Id == Guid.Parse(request.Id), cancellationToken);

            if (entity == null)
            {
                return(true);
            }

            if (!AuthService.IsAuthorized(entity.UserId))
            {
                throw new ForbiddenException("Not authorized");
            }

            Context.ObjectRepository.Remove(entity);

            var directoryName = fileSystem.Path.GetDirectoryName(entity.SourcePath);

            if (!directoryName.EndsWith(entity.Id.ToString()))
            {
                throw new AmiException(string.Format(
                                           "The directory name of object {0} ends with an unexpected name: {1}",
                                           entity.Id,
                                           directoryName));
            }

            await Context.SaveChangesAsync(cancellationToken);

            fileSystem.Directory.Delete(fileSystem.Path.Combine(configuration.Options.WorkingDirectory, directoryName), true);

            var result = ObjectModel.Create(entity);

            await Events.CreateAsync(entity.UserId, EventType.ObjectDeleted, result, cancellationToken);

            await Gateway.NotifyGroupsAsync(entity.UserId, EventType.ObjectDeleted, result, cancellationToken);

            return(true);
        }