public async Task AddRangeAsyncTest()
        {
            var id     = Guid.NewGuid().ToString();
            var source = new Config
            {
                AppId        = "001",
                Id           = id,
                Group        = "group",
                Key          = "key",
                Value        = "v",
                Description  = "d",
                CreateTime   = DateTime.Now,
                UpdateTime   = DateTime.Now,
                Status       = ConfigStatus.Enabled,
                OnlineStatus = OnlineStatus.Online
            };
            var id1     = Guid.NewGuid().ToString();
            var source1 = new Config
            {
                AppId        = "001",
                Id           = id1,
                Group        = "g",
                Key          = "k",
                Value        = "v",
                Description  = "d",
                CreateTime   = DateTime.Now,
                UpdateTime   = DateTime.Now,
                Status       = ConfigStatus.Deleted,
                OnlineStatus = OnlineStatus.Online
            };

            var result = await service.AddRangeAsync(new List <Config> {
                source,
                source1
            });

            Assert.IsTrue(result);

            var config = fsq.Select <Config>(new
            {
                Id = id
            }).ToOne();

            Assert.IsNotNull(config);
            var config1 = fsq.Select <Config>(new
            {
                Id = id1
            }).ToOne();

            Assert.IsNotNull(config1);
        }
Esempio n. 2
0
        public async Task <IActionResult> AddRange([FromBody] List <ConfigVM> model)
        {
            if (model == null || model.Count == 0)
            {
                throw new ArgumentNullException("model");
            }

            var configs = await _configService.GetByAppId(model.First().AppId);

            var oldDict = new Dictionary <string, string>();

            configs.ForEach(item =>
            {
                var newkey = item.Key;
                if (!string.IsNullOrEmpty(item.Group))
                {
                    newkey = $"{item.Group}:{item.Key}";
                }
                oldDict.Add(newkey, item.Value);
            });

            var addConfigs = new List <Config>();

            //judge if json key already in configs
            foreach (var item in model)
            {
                var newkey = item.Key;
                if (!string.IsNullOrEmpty(item.Group))
                {
                    newkey = $"{item.Group}:{item.Key}";
                }
                if (oldDict.ContainsKey(newkey))
                {
                    return(Json(new
                    {
                        success = false,
                        message = "存在重复的配置:" + item.Key
                    }));
                }

                var config = new Config();
                config.Id           = Guid.NewGuid().ToString("N");
                config.Key          = item.Key;
                config.AppId        = item.AppId;
                config.Description  = item.Description;
                config.Value        = item.Value;
                config.Group        = item.Group;
                config.Status       = ConfigStatus.Enabled;
                config.CreateTime   = DateTime.Now;
                config.UpdateTime   = null;
                config.OnlineStatus = OnlineStatus.WaitPublish;
                addConfigs.Add(config);
            }

            var result = await _configService.AddRangeAsync(addConfigs);

            if (result)
            {
                //add syslogs
                var addSysLogs = new List <SysLog>();
                addConfigs.ForEach(c =>
                {
                    addSysLogs.Add(new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        AppId   = c.AppId,
                        LogText = $"新增配置【Key:{c.Key}】【Value:{c.Value}】【Group:{c.Group}】【AppId:{c.AppId}】"
                    });
                });
                await _sysLogService.AddRangeAsync(addSysLogs);

                //add modify log
                var addModifyLogs = new List <ModifyLog>();
                addConfigs.ForEach(c =>
                {
                    addModifyLogs.Add(new ModifyLog
                    {
                        Id         = Guid.NewGuid().ToString("N"),
                        ConfigId   = c.Id,
                        Key        = c.Key,
                        Group      = c.Group,
                        Value      = c.Value,
                        ModifyTime = c.CreateTime
                    });
                });
                await _modifyLogService.AddRangAsync(addModifyLogs);
            }

            return(Json(new
            {
                success = result,
                message = !result ? "批量新增配置失败,请查看错误日志" : ""
            }));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddRange([FromBody] List <ConfigVM> model)
        {
            if (model == null || model.Count == 0)
            {
                throw new ArgumentNullException("model");
            }

            var configs = await _configService.GetByAppIdAsync(model.First().AppId);

            var oldDict = new Dictionary <string, string>();

            configs.ForEach(item =>
            {
                var newkey = item.Key;
                if (!string.IsNullOrEmpty(item.Group))
                {
                    newkey = $"{item.Group}:{item.Key}";
                }
                oldDict.Add(newkey, item.Value);
            });

            var addConfigs = new List <Config>();

            //judge if json key already in configs
            foreach (var item in model)
            {
                var newkey = item.Key;
                if (!string.IsNullOrEmpty(item.Group))
                {
                    newkey = $"{item.Group}:{item.Key}";
                }
                if (oldDict.ContainsKey(newkey))
                {
                    return(Json(new
                    {
                        success = false,
                        message = "存在重复的配置:" + item.Key
                    }));
                }

                var config = new Config();
                config.Id           = Guid.NewGuid().ToString("N");
                config.Key          = item.Key;
                config.AppId        = item.AppId;
                config.Description  = item.Description;
                config.Value        = item.Value;
                config.Group        = item.Group;
                config.Status       = ConfigStatus.Enabled;
                config.CreateTime   = DateTime.Now;
                config.UpdateTime   = null;
                config.OnlineStatus = OnlineStatus.WaitPublish;
                addConfigs.Add(config);
            }

            var result = await _configService.AddRangeAsync(addConfigs);

            if (result)
            {
                addConfigs.ForEach(c =>
                {
                    TinyEventBus.Instance.Fire(EventKeys.ADD_CONFIG_SUCCESS, c);
                });
            }

            return(Json(new
            {
                success = result,
                message = !result ? "批量新增配置失败,请查看错误日志" : ""
            }));
        }