Beispiel #1
0
        public async Task DeleteFileAsync(int id)
        {
            var user = await Context.GetCurrentUserAsync();

            if (user.IsNotAdmin() && user.IsNotLecturer())
            {
                throw ExceptionHelper.NoAccess();
            }

            var model = await RepositoryFile.FindFirstAsync(new FilesById(id)) ??
                        throw ExceptionHelper.NotFound <DatabaseFile>(id);

            if (user.IsNotAdmin() && !new FilesByOwnerId(user.Id).IsSatisfiedBy(model))
            {
                throw ExceptionHelper.NoAccess();
            }

            var file = Mapper.Map <TFile>(model);

            await RepositoryFile.RemoveAsync(model);

            if (await HelperFile.FileExistsAsync(file) == false)
            {
                return;
            }

            var path = await HelperPath.GetAbsoluteFilePathAsync(file);

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }

            await RepositoryFile.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task ValidateAsync(Material model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (string.IsNullOrWhiteSpace(model.Name))
            {
                throw ExceptionHelper.CreatePublicException("Не указано название материала.");
            }

            if (model.Name.Length > 255)
            {
                throw ExceptionHelper.CreatePublicException("Название материала не может превышать 255 символов.");
            }

            if (string.IsNullOrWhiteSpace(model.Template))
            {
                throw ExceptionHelper.CreatePublicException("Не указан шаблон материала.");
            }

            if (model.Files.IsNotEmpty())
            {
                if (model.Files.GroupBy(x => x.Id).Any(x => x.Count() > 1))
                {
                    throw ExceptionHelper.CreatePublicException("В материале указаны повторяющиеся файлы.");
                }

                if (await model.Files.AllAsync(x => _helperFile.FileExistsAsync(x)) == false)
                {
                    throw ExceptionHelper.CreatePublicException("Один или несколько указанных файлов не существуют.");
                }

                var ids = model.Files
                          .Select(x => x.Id)
                          .ToArray();

                var user = await _context.GetCurrentUserAsync();

                var specification =
                    new FilesByIds(ids) &
                    new FilesByOwnerId(user.Id);

                var files = await _repositoryFile.FindAllAsync(specification);

                if (files.Any(x => x.Type != FileType.Document))
                {
                    throw ExceptionHelper.CreatePublicException("Один или несколько указанных файлов имеют неверный тип.");
                }

                if (files.Count != ids.Length)
                {
                    throw ExceptionHelper.CreatePublicException("Один или несколько указанных файлов не существуют или недоступны.");
                }
            }

            if (model.Anchors.IsNotEmpty())
            {
                if (model.Anchors.Any(x => string.IsNullOrWhiteSpace(x.Token) || string.IsNullOrWhiteSpace(x.Name)))
                {
                    throw ExceptionHelper.CreatePublicException("Один или несколько указанных якорей не заполнены.");
                }

                if (model.Anchors.Any(x => x.Token.Length > 255 || x.Name.Length > 255))
                {
                    throw ExceptionHelper.CreatePublicException(
                              "Один или несколько указанных якорей заполнены некорректно. " +
                              "Максимальная длина токена: 255 символов. " +
                              "Максимальная длина названия: 255 символов.");
                }

                if (model.Anchors.GroupBy(x => x.Token).Any(x => x.Count() > 1))
                {
                    throw ExceptionHelper.CreatePublicException("В материале указаны повторяющиеся якоря.");
                }
            }
        }
        public async Task ValidateAsync(Question model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (string.IsNullOrWhiteSpace(model.Text))
            {
                throw ExceptionHelper.CreatePublicException("Не указан текст вопроса.");
            }

            if (model.Time.HasValue == false)
            {
                throw ExceptionHelper.CreatePublicException("Не указано время ответа на вопрос.");
            }

            // Секунды.
            // От 10 до 3600 секунд.
            // От 10 секунды до 1 часа.
            if (model.Time.Value < 5 || model.Time.Value > 60 * 60)
            {
                throw ExceptionHelper.CreatePublicException(
                          "Указано некорректное время ответа на вопрос. " +
                          "Минимальное: 10 секунд. Максималньое: 1 час.");
            }

            if (model.Type.HasValue == false)
            {
                throw ExceptionHelper.CreatePublicException("Не указан тип вопроса.");
            }

            if (model.Complexity.HasValue == false)
            {
                throw ExceptionHelper.CreatePublicException("Не указана сложность вопроса.");
            }

            if (model.ThemeId.HasValue == false)
            {
                throw ExceptionHelper.CreatePublicException("Не указана тема.");
            }

            var theme = await _repositoryTheme.FindFirstAsync(new ThemesById(model.ThemeId.Value)) ??
                        throw ExceptionHelper.CreatePublicException("Указанная тема не существует.");

            var user = await _context.GetCurrentUserAsync();

            if (new ThemesByLecturerId(user.Id).IsSatisfiedBy(theme) == false)
            {
                throw ExceptionHelper.CreatePublicException("Указанная тема недоступна.");
            }

            if (model.Image != null)
            {
                var image = await _repositoryFile.FindFirstAsync(new FilesById(model.Image.Id)) ??
                            throw ExceptionHelper.CreatePublicException("Указанное изображение не существует.");

                if (await _helperFile.FileExistsAsync(model.Image) == false)
                {
                    throw ExceptionHelper.CreatePublicException("Указанное изображение не существует.");
                }

                if (new FilesByOwnerId(user.Id).IsSatisfiedBy(image) == false)
                {
                    throw ExceptionHelper.CreatePublicException("Указанное изображение недоступно.");
                }
            }

            if (model.Material != null)
            {
                var material = await _repositoryMaterial.FindFirstAsync(new MaterialsById(model.Material.Id)) ??
                               throw ExceptionHelper.CreatePublicException("Указанный материал не существует.");

                if (new MaterialsByOwnerId(user.Id).IsSatisfiedBy(material) == false)
                {
                    throw ExceptionHelper.CreatePublicException("Указанный материал недоступен.");
                }

                if (model.MaterialAnchors.IsNotEmpty())
                {
                    var ids = model.MaterialAnchors
                              .Select(x => x.Id)
                              .ToArray();

                    var specification =
                        new MaterialAnchorsByIds(ids) &
                        new MaterialAnchorsByMaterialId(material.Id);

                    if (await _repositoryMaterialAnchor.GetCountAsync(specification) != ids.Length)
                    {
                        throw ExceptionHelper.CreatePublicException("Один или несколько выбранных якорей не существуют или недоступны.");
                    }
                }
            }

            ValidateByQuestionType(model);
        }