Exemple #1
0
        public async Task <Config> Handle(UpdateConfigCommand request, CancellationToken cancellationToken)
        {
            await using IDbContextTransaction transaction = _configContext.Database.BeginTransaction();
            try
            {
                (Config config, ConfigElement root, ConfigElement[] all)configSource = await _configService.GetConfigElement(
                    () => _configService.GetConfig(request, EnvironmentRole.Editor, cancellationToken),
                    cancellationToken);

                Config config = new Config(request.ConfigCode, configSource.config.Environment,
                                           request.VersionFrom);
                ObjectConfigReader reader        = new ObjectConfigReader(config);
                ConfigElement      configElement = await reader.Parse(request.Data);

                Compare(configSource.root, configElement);

                await _configContext.SaveChangesAsync(cancellationToken);

                await _cacheService.UpdateJsonConfig(configSource.config.ConfigId, request.Data, cancellationToken);

                await transaction.CommitAsync(cancellationToken);

                return(configSource.config);
            }
            catch (Exception ex)
            {
                await transaction.RollbackAsync(cancellationToken);

                _logger.LogError(ex, "{request} data:{Data}", request, request.Data);
                throw;
            }
        }
        public async Task <Config> Handle(CreateConfigCommand request, CancellationToken cancellationToken)
        {
            Environment env = await _environmentService.GetEnvironment(request, EnvironmentRole.Editor, cancellationToken);

            Config existConfig = await _configService.GetConfig(env.EnvironmentId, request, cancellationToken);

            if (existConfig != null && existConfig.VersionFrom == request.VersionFrom)
            {
                throw new EntityException($"{request} is exists");
            }

            Config config;

            if (existConfig != null && existConfig.VersionTo == null)
            {
                config = new Config(request.ConfigCode, env, request.VersionFrom);
                existConfig.SetVersionTo(request.From);
            }
            else if (existConfig != null && existConfig.VersionTo != null)
            {
                config = new Config(request.ConfigCode, env, request.VersionFrom, existConfig.VersionTo);
                existConfig.SetVersionTo(request.From);
            }
            else
            {
                long minVer = (await _configContext.Configs
                               .Where(w => w.EnvironmentId.Equals(env.EnvironmentId) && w.Code == request.ConfigCode)
                               .GroupBy(e => 1)
                               .Select(s => s.Min(m => m.VersionFrom)).ToListAsync(cancellationToken))[0];

                if (minVer != 0 && minVer < request.VersionFrom)
                {
                    throw new OperationException(
                              "Incorrect config latest version in storage, 'VersionTo' must be null. Contact your system administrator");
                }

                config = new Config(request.ConfigCode, env, request.VersionFrom, minVer);
            }

            ObjectConfigReader reader       = new ObjectConfigReader(config);
            ConfigElement      configElemnt = await reader.Parse(request.Data);

            _configContext.ConfigCache.Add(new ConfigCache(config, request.Data));

            await _configContext.SaveChangesAsync(cancellationToken);

            return(config);
        }
Exemple #3
0
        public async void ParseObject()
        {
            ObjectConfigReader reader       = new ObjectConfigReader(testConfig);
            JObject            origin       = JObject.FromObject(new TestEntity());
            ConfigElement      configElemnt = await reader.Parse(origin);

            Assert.Equal(TypeNode.Root, configElemnt.TypeElement.TypeNode);

            Log.WriteLine(origin);
            JsonReducer reducer        = new JsonReducer();
            JObject     reducerJobject = reducer.Parse(configElemnt).Result;

            Assert.Equal(reader.AllProperty.Count, reducer.AllProperty.Count);

            Log.WriteLine(reducerJobject);
            string strorigin         = origin.ToString();
            string strreducerJobject = reducerJobject.ToString();

            Assert.Equal(strorigin, strreducerJobject);
        }