protected async Task <File> Handle(IFileCommand request, CancellationToken cancellationToken)
        {
            await this.Authorize();

            using (var lockResult = await _lockService.GetFileLock(request.Id).LockAsync(0))
            {
                if (!lockResult.AcquiredLock)
                {
                    throw new FileConflictException();
                }

                var file = await _db.Files.FindAsync(request.Id);

                if (file == null)
                {
                    throw new EntityNotFoundException <File>();
                }

                await this.PerformOperation(file);

                await _db.SaveChangesAsync(cancellationToken);
            }

            return(await _fileQuery.ExecuteAsync(request.Id));
        }
示例#2
0
        private async Task <FileUpdateResult> UpdateFile(Domain.Models.File dbFile, Domain.Models.File file)
        {
            var result = new FileUpdateResult();

            result.LockResult = await _lockService.GetFileLock(dbFile.Id).LockAsync(0);

            // Don't need to update or throw error if contents haven't changed
            if (!dbFile.Content.Equals(file.Content))
            {
                if (!result.LockResult.AcquiredLock)
                {
                    result.UnableToLock = true;
                    result.FileUpdated  = false;
                }
                else if (dbFile.CanLock(_userId, _isAdmin))
                {
                    dbFile.Content = file.Content;
                    dbFile.Save(
                        _userId,
                        _isAdmin,
                        bypassLock: true);

                    result.FileUpdated  = true;
                    result.UnableToLock = false;
                }
                else
                {
                    result.FileUpdated  = false;
                    result.UnableToLock = true;
                }
            }

            return(result);
        }