public void Invoke(int projectId, string name, ConfigurationType type, ProjectConfiguration configuration,
                           string oldName)
        {
            var project = _projectRepository.GetByIdWithCustomDatas(projectId);

            if (project == null)
            {
                throw new ArgumentNullException($"Could not find project with id: {projectId}.");
            }

            var customData = project.ProjectCustomData.FirstOrDefault(data => data.ConfigurationName == oldName);

            if (customData == null)
            {
                throw new Exception($"You cannot edit non-existing custom data (name: {oldName}).");
            }

            customData.ConfigurationName       = name;
            customData.ConfigurationType       = type;
            customData.SerializedConfiguration = _jsonConvertService.Serialize(configuration);

            EditConfigurationForUsers(name, type, configuration, project);

            _projectRepository.Edit(project);
        }
        public void Invoke(int projectId, string name, ConfigurationType type, ProjectConfiguration configuration)
        {
            var project = _projectRepository.GetByIdWithCustomDatas(projectId);

            if (project == null)
            {
                throw new ArgumentNullException($"Could not find project with id: {projectId}.");
            }

            var customData = project.ProjectCustomData.FirstOrDefault(data => data.ConfigurationName == name);

            if (customData == null)
            {
                project.ProjectCustomData.Add(new ProjectCustomData
                {
                    ConfigurationName       = name,
                    ConfigurationType       = type,
                    SerializedConfiguration = _jsonConvertService.Serialize(configuration)
                });
                AddConfigurationForUsers(name, type, configuration, project);
            }
            else
            {
                throw new Exception($"Custom data of name: {name} already exists.");
            }

            _projectRepository.Edit(project);
            _eventBus.Publish(new CustomDataAdded(projectId, name, type));
        }
        public void Invoke(int projectId, int userId, string name, UserConfiguration configuration)
        {
            var userCustomData =
                userCustomDataRepository.FindBy(
                    m =>
                    (m.ProjectCustomData.ProjectId == projectId) && (m.UserId == userId) &&
                    (m.ProjectCustomData.ConfigurationName == name)).FirstOrDefault() ?? new UserCustomData();

            userCustomData.SerializedConfiguration = customDataJsonConvertService.Serialize(configuration);
            userCustomData.ModificationDate        = DateTime.UtcNow;

            userCustomDataRepository.Edit(userCustomData);
        }
        public void SetDefaultUserCustomData(Project project, User newUser)
        {
            var projectCustomData = project.ProjectCustomData;

            foreach (var customData in projectCustomData)
            {
                switch (customData.ConfigurationType)
                {
                case ConfigurationType.Boolean:
                {
                    var projectConfiguration =
                        (BooleanProjectConfiguration)
                        customDataJsonConvertService.DeserializeProject(ConfigurationType.Boolean,
                                                                        customData.SerializedConfiguration);
                    newUser.UserCustomData.Add(new UserCustomData
                        {
                            ProjectCustomDataId     = customData.Id,
                            SerializedConfiguration =
                                customDataJsonConvertService.Serialize(new BooleanUserConfiguration
                            {
                                Value = projectConfiguration.Default
                            })
                        });
                    break;
                }

                case ConfigurationType.Number:
                {
                    var projectConfiguration =
                        (NumberProjectConfiguration)
                        customDataJsonConvertService.DeserializeProject(ConfigurationType.Number,
                                                                        customData.SerializedConfiguration);
                    newUser.UserCustomData.Add(new UserCustomData
                        {
                            ProjectCustomDataId     = customData.Id,
                            SerializedConfiguration =
                                customDataJsonConvertService.Serialize(new NumberUserConfiguration
                            {
                                Value = projectConfiguration.Default
                            })
                        });
                    break;
                }

                case ConfigurationType.Text:
                {
                    var projectConfiguration =
                        (TextProjectConfiguration)
                        customDataJsonConvertService.DeserializeProject(ConfigurationType.Text,
                                                                        customData.SerializedConfiguration);

                    newUser.UserCustomData.Add(new UserCustomData
                        {
                            ProjectCustomDataId     = customData.Id,
                            SerializedConfiguration =
                                customDataJsonConvertService.Serialize(new TextUserConfiguration
                            {
                                Value = projectConfiguration.Default
                            })
                        });
                    break;
                }
                }
            }
        }