public IActionResult Edit(NaturalViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = _dbContext.NaturalResources.FirstOrDefault(x => x.NaturalResourcesUuid == model.NaturalResourcesUuid); if (entity == null) { response.SetFailed("该信息不存在"); return(Ok(response)); } if (_dbContext.NaturalResources.Any(x => x.Title == model.Title && x.NaturalResourcesUuid != model.NaturalResourcesUuid)) { response.SetFailed("标题已存在"); return(Ok(response)); } entity.Title = model.Title; entity.Content = model.Content; entity.Cover = model.Cover; entity.IsRelease = model.IsRelease; entity.ReleaseTime = model.ReleaseTime; entity.Photo = model.Photo; int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("编辑", "成功:编辑:自然资源列表数据", _dbContext); } return(Ok(response)); } }
public IActionResult Create(NaturalViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { if (_dbContext.NaturalResources.Any(x => x.Title == model.Title)) { response.SetFailed("标题名称已存在"); return(Ok(response)); } //var entity = _mapper.Map<NaturalViewModel, NaturalResources>(model); var entity = new NaturalResources(); entity.IsRelease = model.IsRelease; if (!string.IsNullOrEmpty(model.ReleaseTime.ToString())) { entity.ReleaseTime = Convert.ToDateTime(model.ReleaseTime); } entity.Title = model.Title; entity.Content = model.Content; entity.Cover = model.Cover; entity.NaturalResourcesUuid = Guid.NewGuid(); entity.IsDeleted = 0; entity.AddPeople = AuthContextService.CurrentUser.LoginName; entity.AddTime = DateTime.Now; entity.Photo = model.Photo; _dbContext.NaturalResources.Add(entity); int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("添加", "成功:添加:自然资源列表数据", _dbContext); } response.SetSuccess(); return(Ok(response)); } }