public async Task <int> AddProjectDataModel(int projectId, string name, string description, string label, bool?isManaged, string selectKey, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var project = await _projectRepository.GetById(projectId, cancellationToken);

            if (project == null)
            {
                throw new ProjectNotFoundException(projectId);
            }

            var projectDataModelPropertyByProjectSpec = new ProjectDataModelFilterSpecification(name, projectId);

            if (await _dataModelRepository.CountBySpec(projectDataModelPropertyByProjectSpec, cancellationToken) > 0)
            {
                throw new DuplicateProjectDataModelException(name);
            }

            var newDataModel = new ProjectDataModel
            {
                ProjectId   = projectId,
                Name        = name,
                Description = description,
                Label       = label,
                IsManaged   = isManaged,
                SelectKey   = selectKey
            };

            if (string.IsNullOrEmpty(newDataModel.Label))
            {
                newDataModel.Label = TextHelper.SplitTextOnCapitalLetters(name);
            }

            return(await _dataModelRepository.Create(newDataModel, cancellationToken));
        }
Example #2
0
        public async Task DeleteDataModel(int id, bool validateRelatedModel = true, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (validateRelatedModel)
            {
                var model = await _dataModelRepository.GetById(id, cancellationToken);

                var relatedDataModelsSpec = new ProjectDataModelFilterSpecification(model.ProjectId, id);
                var relatedDataModels     = await _dataModelRepository.GetBySpec(relatedDataModelsSpec, cancellationToken);

                if (relatedDataModels.Any())
                {
                    throw new RelatedProjectDataModelException(model.Name, relatedDataModels.Select(m => m.Name).ToArray());
                }
            }

            var propertyByDataModelSpec = new ProjectDataModelPropertyFilterSpecification(id);
            var properties = await _dataModelPropertyRepository.GetBySpec(propertyByDataModelSpec, cancellationToken);

            foreach (var property in properties.ToList())
            {
                await DeleteDataModelProperty(property.Id, cancellationToken);
            }

            await _dataModelRepository.Delete(id, cancellationToken);
        }
Example #3
0
        public async Task DeleteDataModels(int projectId, int[] ids, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var modelsSpec = new ProjectDataModelFilterSpecification(ids);

            modelsSpec.IncludeStrings.Add("Properties.RelatedProjectDataModel");
            var models = (await _dataModelRepository.GetBySpec(modelsSpec, cancellationToken)).ToList();

            // search for models to be deleted that dependency is not included in the current deletion
            var relatedDataModelsSpec = new ProjectDataModelFilterSpecification(projectId, ids.Select(id => (int?)id).ToArray());
            var relatedDataModels     = (await _dataModelRepository.GetBySpec(relatedDataModelsSpec, cancellationToken)).ToList();

            if (relatedDataModels.Count > 0)
            {
                throw new RelatedProjectDataModelException(relatedDataModels.Select(m => m.Name).Distinct().ToArray());
            }

            // sort the deletion order so the conflict foreign key error is not thrown
            models.Sort(CompareDataModelRelation);

            foreach (var model in models)
            {
                await this.DeleteDataModel(model.Id, false, cancellationToken);
            }
        }
        public async Task <ProjectDataModel> GetProjectDataModelByName(int projectId, string modelName, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var dataModelByNameSpec = new ProjectDataModelFilterSpecification(modelName, projectId);

            dataModelByNameSpec.IncludeStrings.Add("Properties.RelatedProjectDataModel");

            return(await _dataModelRepository.GetSingleBySpec(dataModelByNameSpec, cancellationToken));
        }
        public async Task <List <ProjectDataModel> > GetProjectDataModels(int projectId, bool includeProperties, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var dataModelByProjectSpec = new ProjectDataModelFilterSpecification(projectId);

            if (includeProperties)
            {
                dataModelByProjectSpec.IncludeStrings.Add("Properties.RelatedProjectDataModel");
            }

            var dataModels = await _dataModelRepository.GetBySpec(dataModelByProjectSpec, cancellationToken);

            return(dataModels.ToList());
        }
        public async Task UpdateDataModel(ProjectDataModel updatedDataModel, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var dataModel = await _dataModelRepository.GetById(updatedDataModel.Id, cancellationToken);

            if (dataModel != null)
            {
                var dataModelByNameSpec = new ProjectDataModelFilterSpecification(updatedDataModel.Name, dataModel.ProjectId, dataModel.Id);
                if (await _dataModelRepository.CountBySpec(dataModelByNameSpec, cancellationToken) > 0)
                {
                    throw new DuplicateProjectDataModelException(updatedDataModel.Name);
                }

                dataModel.Name        = updatedDataModel.Name;
                dataModel.Description = updatedDataModel.Description;
                dataModel.Label       = updatedDataModel.Label;
                dataModel.IsManaged   = updatedDataModel.IsManaged;
                dataModel.SelectKey   = updatedDataModel.SelectKey;

                await _dataModelRepository.Update(dataModel, cancellationToken);
            }
        }