public object Post(ProductSave request) { ProductSaveResponse response=new ProductSaveResponse(); if (request.Goods.ID>0) { response.ID = request.Goods.ID; Db.Update(request.Goods); } else { try { response.ID = Db.Insert(request.Goods, true); } catch (Exception ex) { throw; } } return response; }
public async Task <ResponseModel> Edit([FromBody] ProductSave product) { if (ModelState.IsValid) { //这里只用try-catch就可以了。 //如果是同时有几个操作数据库的步骤。为了保持数据的完整性,用一个事务括起来。 var productEntity = await _productservice.GetOneAsync(product.Id); if (productEntity == null) { return new ResponseModel { code = 0, result = "产品不存在" } } ; productEntity.FistClassId = product.FistClassId; productEntity.SecondClassId = product.SecondClassId; productEntity.Sort = product.Sort; productEntity.Title = product.Title; productEntity.SmallTitle = product.SmallTitle; productEntity.Remark = product.Remark; productEntity.KeyTitle = product.KeyTitle; productEntity.Keywords = product.Keywords; productEntity.Description = product.Description; productEntity.Picture = product.Picture; productEntity.PictureTag = product.PictureTag; productEntity.Sales = product.Sales; productEntity.Score = product.Score; productEntity.TextContent = product.TextContent; productEntity.Parameter = product.Parameter; productEntity.GMTCreate = product.GMTCreate; productEntity.GMTModified = product.GMTModified; productEntity.Hits = product.Hits; productEntity.GMTLastHit = product.GMTLastHit; productEntity.Praise = product.Praise; productEntity.IsShow = product.IsShow; productEntity.IsHome = product.IsHome; productEntity.IsNew = product.IsNew; productEntity.IsBest = product.IsBest; productEntity.IsHot = product.IsHot; productEntity.IsSale = product.IsSale; productEntity.IsFocus = product.IsFocus; //数据库操作 var result = await _productservice.EditOneAsync(productEntity); if (result > 0) { return new ResponseModel { code = 200, result = "产品修改成功" } } ; return(new ResponseModel { code = 0, result = "产品修改失败" }); } else { string errorMsg = "参数验证失败"; if (ModelState.ErrorCount > 0) { foreach (var key in ModelState.Keys) { errorMsg += ModelState.GetValidationState(key) + "|"; } errorMsg.TrimEnd('|'); } return(new ResponseModel { code = 400, result = errorMsg }); } }