public async Task <bool> AddProductsasync(CommoditymodelView model) { try { var Commoditycategory = await _productdb.commodityCategories.FirstOrDefaultAsync(x => x.Categoryname == model.Commoditycategoryname); if (Commoditycategory == null) { throw new Exception("数据错误"); } var CommoditycategoryId = Commoditycategory.Id; var user = await _productdb.users.FirstOrDefaultAsync(x => x.Username == model.Username); if (user == null) { throw new Exception("数据错误"); } var userId = user.Id; if (model != null) { var Comdity = new Commodity(); Comdity.Id = model.Id; Comdity.Commodityname = model.Commodityname; Comdity.Filepath = model.Filepath; Comdity.phone = model.phone; Comdity.Price = model.Price; Comdity.UserId = userId; Comdity.CommoditycategoryId = CommoditycategoryId; Comdity.startdate = model.startdate; Comdity.transactionway = model.transactionway; Comdity.expiredate = model.expiredate; Comdity.status = model.status; Comdity.Desc = model.Desc; await _productdb.commodities.AddAsync(Comdity); await _productdb.SaveChangesAsync(); return(true); } else { return(false); } } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> AddProduct([FromBody] CommoditymodelView commoditymodel) { var result = await _productsInterface.AddProductsasync(commoditymodel); if (result) { return(Ok()); } else { return(BadRequest()); } }
public async Task <CommoditymodelView> GetProductsbyidasync(int id) { try { if (id >= 0) { var result = await _productdb.commodities.FirstOrDefaultAsync(x => x.Id == id); if (result != null) { var Commoditycategory = await _productdb.commodityCategories.FirstOrDefaultAsync(x => x.Id == result.CommoditycategoryId); var user = await _productdb.users.FirstOrDefaultAsync(x => x.Id == result.UserId); if (user == null) { throw new Exception("数据错误"); } var modelView = new CommoditymodelView(); modelView.Id = result.Id; modelView.Username = user.Username; modelView.Commodityname = result.Commodityname; modelView.Commoditycategoryname = Commoditycategory.Categoryname; modelView.Price = result.Price; modelView.phone = result.phone; modelView.startdate = result.startdate; modelView.status = result.status; modelView.expiredate = result.expiredate; modelView.transactionway = result.transactionway; modelView.Desc = result.Desc; modelView.Filepath = result.Filepath; return(modelView); } throw new Exception("未获取到数据"); } throw new Exception("未找到该商品"); } catch (Exception ex) { throw ex; } }