コード例 #1
0
        // -DtoQuery

        public async Task <AppSettingDto> CreateAppSettingAsync(AppSettingDto appSettingDto, string username)
        {
            OnCreate(appSettingDto, username);

            var entity = AppSettingDto.AsAppSettingFunc(appSettingDto);

            ToEntity(ref entity, appSettingDto);
            //entity.InsertUser = entity.LastActivityUser = username;
            //entity.InsertDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
            entity.AddTracker(username);

            _context.AppSettings.Add(entity);

            OnBeforeCreate(entity, username);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                // _context.Entry(entity).State = EntityState.Detached;
                throw new Exception("Add error", ex);
            }
            finally
            {
                // _context.Entry(entity).State = EntityState.Detached;
            }
            OnAfterCreate(entity, username);

            // appSettingDto = AppSettingDto.AsAppSettingDtoFunc(entity);
            appSettingDto = await GetAppSettingDtoAsync(entity.Id, AppSettingDto.IncludeNavigations());

            return(appSettingDto);
        }
コード例 #2
0
        public async Task <ActionResult <AppSettingDto> > PostAppSetting(AppSettingDto appSetting)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            AppSettingDto appSettingDto;

            try
            {
                appSettingDto = await appSettingService.CreateAppSettingAsync(appSetting, User.Identity.Name);

                if (appSettingDto != null)
                {
                    this.RemoveCache(CacheKeys.AppSetting);
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // var appSettingDto = await _context.AppSettings.Select(AppSettingDto.AsAppSettingDto).SingleOrDefaultAsync(m => m.Id == appSetting.Id);

            return(CreatedAtAction("GetAppSetting", new { id = appSettingDto.Id }, appSettingDto));
        }
コード例 #3
0
        public JsonResult SaveAppSetting(AppSettingDto appSettingDto)
        {
            ResponseResult responseResult;
            bool           isAdd = appSettingDto.Id == 0;

            try
            {
                var isConfiKeyExisted = ConfiKeyExisted(appSettingDto);
                if (isAdd)
                {
                    if (isConfiKeyExisted)
                    {
                        AppSettingBusiness.SaveAppSetting(appSettingDto);
                        responseResult = new ResponseResult(ResultEnum.IsSuccess.成功, "新增配置成功");
                    }
                    else
                    {
                        responseResult = new ResponseResult(ResultEnum.IsSuccess.失败, "已存在该项配置,请考虑清楚再添加!");
                    }
                }
                else
                {
                    AppSettingBusiness.SaveAppSetting(appSettingDto);
                    responseResult = new ResponseResult(ResultEnum.IsSuccess.成功, "新增配置成功");
                }
            }
            catch (Exception ex)
            {
                responseResult = new ResponseResult(ResultEnum.IsSuccess.失败, "添加配置异常,请联系管理员!" + appSettingDto.AppId);
            }
            return(Json(responseResult, JsonRequestBehavior.AllowGet));
        }
コード例 #4
0
        public ActionResult Import([FromQuery] int appid, [FromForm] IFormCollection formData)
        {
            try
            {
                int appIdSelf = appid;
                //存入本地
                IFormFile file = formData.Files.FirstOrDefault();
                //var file = Request.Files[0];

                string sheet = "配置";
                List <AppSettingDto> appSettings = new List <AppSettingDto>();
                //传入参数,将excel转换为Datatable,格式文件名加上固定模版Sheet1
                DataTable dt = Entities.ExcelToDataTableHelper(file, sheet);
                //循环这个集合并添加到数据表
                foreach (DataRow item in dt.Rows)
                {
                    AppSettingDto appSetting = new AppSettingDto();
                    appSetting.ConfigKey   = item[0].ToString();
                    appSetting.ConfigValue = item[1].ToString();
                    appSettings.Add(appSetting);
                }

                AppSettingBusiness.SaveAppSettings(appSettings, appIdSelf);
            }
            catch (Exception ex)
            {
                return(Json(new ResponseResult(false, "上传格式有问题!")));
            }

            return(Json(new ResponseResult(true, "上传成功!")));
        }
コード例 #5
0
ファイル: _Unique.cs プロジェクト: minos77/ResultChecker
 partial void OnCreate(AppSettingDto appSetting, string username)
 {
     if (_context.AppSettings.Any(a => a.Key == appSetting.Key))
     {
         throw new Exception("Record not unique.");
     }
 }
コード例 #6
0
        public static Task SaveAppSettingAsync(AppSettingDto appSettingDto)
        {
            return(Task.Run(() =>
            {
                using (var db = new ConfigCenterConnection())
                {
                    var appSetting = Mapper.Map <AppSettingDto, AppSetting>(appSettingDto);
                    db.AppSetting.Add(appSetting);
                    db.SaveChanges();

                    var app = db.App.FirstOrDefault(x => x.Id == appSettingDto.AppId);
                    if (app != null)
                    {
                        app.Version = DateTime.Now.ToString("yyyyMMddHHmmss");
                        db.Entry(app).State = EntityState.Modified;
                        db.SaveChanges();

                        //更新zookeeper的值
                        var path = ZooKeeperHelper.ZooKeeperRootNode + "/" + app.AppId;
                        if (!ZooKeeperHelper.Exists(path))
                        {
                            ZooKeeperHelper.Create(path, null);
                        }
                        ZooKeeperHelper.SetData(path, app.Version, -1);
                    }
                }
            }));
        }
コード例 #7
0
        public async Task <AppSettingDto> GetAsync(string key)
        {
            var appSetting = await _appSettingRepo
                             .QueryAsNoTracking()
                             .SingleOrDefaultAsync(setting => setting.Key == key);

            return(AppSettingDto.Create(appSetting));
        }
コード例 #8
0
        public async Task <ActionResult <IEnumerable <AppSettingDto> > > GetAppSettings(string searchText = null
                                                                                        , string key      = null
                                                                                        , string value    = null
                                                                                        /*, int pageNumber=1, int pageSize=7*/)
        {
            // var appSettings = _context.AppSettings.Select(AppSettingDto.AsAppSettingDto);
            List <Expression <Func <AppSettingDto, bool> > > filters = null;

            if (String.IsNullOrEmpty(searchText) &&
                (String.IsNullOrEmpty(key)) &&
                (String.IsNullOrEmpty(value))

                )
            {
                // return null;
            }
            else
            {
                filters = new List <Expression <Func <AppSettingDto, bool> > >();

                if (!String.IsNullOrEmpty(searchText))
                {
                    if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0)
                    {
                        filters.Add(x => x.Id.ToString().Contains(searchText));
                    }
                }

                if (!String.IsNullOrEmpty(key))
                {
                    filters.Add(x => x.Key == key);
                }

                if (!String.IsNullOrEmpty(value))
                {
                    filters.Add(x => x.Value == value);
                }
            }

            //sort
            //return appSettings.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize);

            // OnSelectQuery(ref appSettings);

            // return await appSettings.ToListAsync();

            if (filters == null)
            {
                return(await appSettingService.GetAppSettingDtoesAsync(AppSettingDto.IncludeNavigations()));
            }
            else
            {
                return(await appSettingService.GetAppSettingDtoesAsync(AppSettingDto.IncludeNavigations(), filters.ToArray()));
            }
        }
コード例 #9
0
        private static Mock <IAppSettingService> GetMockAppSettingService()
        {
            var mockAppSettingService = new Mock <IAppSettingService>();
            var shopeeSetting         = new AppSetting {
                Value = "2"
            };
            var shopeeSettingDto = AppSettingDto.Create(shopeeSetting);

            mockAppSettingService.Setup(service => service.GetAsync(AppSettingKey.ShopeeFee)).Returns(Task.FromResult(shopeeSettingDto));
            return(mockAppSettingService);
        }
コード例 #10
0
        public async Task <bool> DeleteAppSettingAsync(AppSettingDto appSettingDto)
        {
            OnDelete(appSettingDto);

            var entity = _context.AppSettings
                         .Where(x => x.Id == appSettingDto.Id)
                         .FirstOrDefault();

            if (entity != null)
            {
                _context.AppSettings.Remove(entity);

                OnBeforeDelete(entity);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException ex)
                {
                    // _context.Entry(entity).State = EntityState.Detached;

                    var sqlException = ex.GetBaseException() as SqlException;

                    if (sqlException != null)
                    {
                        var errorMessage = "deleting error";

                        var number = sqlException.Number;

                        if (number == 547)
                        {
                            string table = GetErrorTable(sqlException) ?? "descendant";
                            errorMessage = $"Must delete {table} records before deleting App Setting";
                        }

                        throw new Exception(errorMessage, ex);
                    }
                }
                finally
                {
                    // _context.Entry(entity).State = EntityState.Detached;
                }
                OnAfterDelete(entity);
            }
            else
            {
                return(false);
            }

            return(true);
        }
コード例 #11
0
        public static void SaveAppSetting(AppSettingDto appSettingDto)
        {
            var appSetting = Mapper.Map <AppSettingDto, AppSetting>(appSettingDto);

            appSetting.Save();

            var app = App.SingleOrDefault(appSettingDto.AppId);

            if (app != null)
            {
                app.Version = DateTime.Now.ToString("yyyyMMddHHmmss");
                app.Save();
            }
        }
コード例 #12
0
        public JsonResult SaveAppSetting(AppSettingDto appSettingDto)
        {
            ResponseResult responseResult;

            try
            {
                AppSettingBusiness.SaveAppSetting(appSettingDto);
                responseResult = new ResponseResult(true, "");
            }
            catch (Exception)
            {
                responseResult = new ResponseResult(false, "");
            }
            return(Json(responseResult, JsonRequestBehavior.AllowGet));
        }
コード例 #13
0
        public async Task <ActionResult <AppSettingDto> > GetAppSetting(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var appSetting = await appSettingService.GetAppSettingDtoAsync(id, AppSettingDto.IncludeNavigations());

            if (appSetting == null)
            {
                return(NotFound());
            }

            return(appSetting);
        }
コード例 #14
0
        public async Task <bool> UpdateAppSettingAsync(AppSettingDto appSettingDto, string username /*, String[] includeNavigations, params Expression<Func<AppSetting, bool>>[] filters*/)
        {
            OnUpdate(appSettingDto, username);

            // Get AppSetting
            var entity = EntityQuery(_context, AppSettingDto.IncludeNavigations())
                         .FirstOrDefault(x => x.Id == appSettingDto.Id);

            if (entity != null)
            {
                entity = AppSettingDto.ToAppSettingFunc(entity, appSettingDto);

                ToEntity(ref entity, appSettingDto);
                //entity.UpdateUser = entity.LastActivityUser = username;
                //entity.UpdateDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
                entity.EditTracker(username);

                OnBeforeUpdate(entity, username);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    // _context.Entry(entity).State = EntityState.Detached;
                    throw new Exception("Update error", ex);
                }
                finally
                {
                    // _context.Entry(entity).State = EntityState.Detached;
                }
                OnAfterUpdate(entity, username);
            }
            else
            {
                return(false);
            }

            return(true);
        }
コード例 #15
0
        public async Task <IActionResult> PutAppSetting(int id, AppSettingDto appSetting)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != appSetting.Id)
            {
                return(BadRequest());
            }

            try
            {
                var updated = await appSettingService.UpdateAppSettingAsync(appSetting, User.Identity.Name);

                if (updated)
                {
                    this.RemoveCache(CacheKeys.AppSetting);
                }
                else
                {
                    return(BadRequest("Update failed!."));
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AppSettingExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
            // return Ok(appSetting);
        }
コード例 #16
0
        public static void SaveAppSetting(AppSettingDto appSettingDto)
        {
            var appSetting = Mapper.Map <AppSettingDto, AppSetting>(appSettingDto);

            appSetting.Save();

            var app = App.SingleOrDefault(appSettingDto.AppId);

            if (app != null)
            {
                app.Version = DateTime.Now.ToString("yyyyMMddHHmmss");
                app.Save();

                //更新zookeeper的值
                var path = ZooKeeperHelper.ZooKeeperRootNode + "/" + app.AppId;
                if (!ZooKeeperHelper.Exists(path))
                {
                    ZooKeeperHelper.Create(path, null);
                }
                ZooKeeperHelper.SetData(path, app.Version, -1);
            }
        }
コード例 #17
0
        private static bool ConfiKeyExisted(AppSettingDto appSettingDto)
        {
            var appSetting = AppSettingBusiness.GetAppSettingByKeyAndAppId(appSettingDto.ConfigKey, appSettingDto.AppId);

            return(appSetting.IsNull());
        }
コード例 #18
0
 partial void OnCreate(AppSettingDto appSetting, string username)
 {
     throw new NotImplementedException();
 }
コード例 #19
0
 partial void ToEntity(ref AppSetting entity, AppSettingDto appSettingDto);
コード例 #20
0
 partial void OnDelete(AppSettingDto appSettingDto);
コード例 #21
0
 partial void OnUpdate(AppSettingDto appSettingDto, string username);