public async Task <ActionResult <ResponseModel> > Create(DictionaryCreateViewModel model) { var response = ResponseModelFactory.CreateInstance; if (model.Name.Trim().Length <= 0) { response.SetFailed("请输入字典名称"); return(Ok(response)); } await using (_dbContext) { if (_dbContext.SystemDictionary.Count(x => x.Name == model.Name) > 0) { response.SetFailed("字典已存在"); return(Ok(response)); } var entity = _mapper.Map <DictionaryCreateViewModel, SystemDictionary>(model); entity.Code = RandomHelper.GetRandomizer(8, true, false, true, true); await _dbContext.SystemDictionary.AddAsync(entity); await _dbContext.SaveChangesAsync(); //更新缓存 _dictionaryService.ClearDictionaryCache(); response.SetSuccess(); return(Ok(response)); } }
public async Task <ActionResult <ResponseModel> > Edit(DictionaryCreateViewModel model) { var response = ResponseModelFactory.CreateInstance; if (ConfigurationManager.AppSettings.IsTrialVersion) { response.SetIsTrial(); return(Ok(response)); } await using (_dbContext) { if (await _dbContext.SystemDictionary.CountAsync(x => x.Name == model.Name && x.Code != model.Code) > 0) { response.SetFailed("字典已存在"); return(Ok(response)); } var entity = _mapper.Map <DictionaryCreateViewModel, SystemDictionary>(model); _dbContext.Entry(entity).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); //更新缓存 _dictionaryService.ClearDictionaryCache(); return(Ok(response)); } }