// -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); }
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)); }
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)); }
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, "上传成功!"))); }
partial void OnCreate(AppSettingDto appSetting, string username) { if (_context.AppSettings.Any(a => a.Key == appSetting.Key)) { throw new Exception("Record not unique."); } }
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); } } })); }
public async Task <AppSettingDto> GetAsync(string key) { var appSetting = await _appSettingRepo .QueryAsNoTracking() .SingleOrDefaultAsync(setting => setting.Key == key); return(AppSettingDto.Create(appSetting)); }
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())); } }
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); }
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); }
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(); } }
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)); }
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); }
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); }
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); }
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); } }
private static bool ConfiKeyExisted(AppSettingDto appSettingDto) { var appSetting = AppSettingBusiness.GetAppSettingByKeyAndAppId(appSettingDto.ConfigKey, appSettingDto.AppId); return(appSetting.IsNull()); }
partial void OnCreate(AppSettingDto appSetting, string username) { throw new NotImplementedException(); }
partial void ToEntity(ref AppSetting entity, AppSettingDto appSettingDto);
partial void OnDelete(AppSettingDto appSettingDto);
partial void OnUpdate(AppSettingDto appSettingDto, string username);