public int createSupply(SupplyVM vm) { int ret = 0; using (var tran = new TransactionScope()) { using (var ctx = new ShtxSms2008Entities()) { try { var supply = new Supply(); supply.CatalogID = vm.CatalogId; supply.Quantity = vm.Quantity; supply.Mobile = vm.Mobile; supply.Contact = vm.Contact; supply.Description = vm.Description; supply.DeliveryType = vm.DeliveryType; supply.SupplyType = vm.SupplyType; supply.Product = vm.Product; supply.Creater = vm.Creater; supply.Price = vm.Price; //supply.isChecked = false;初始为空,0拒绝,1同意 var provice = ctx.Provinces.FirstOrDefault(o => o.Name == vm.Provice); if (provice != null) { var city = ctx.Provinces.FirstOrDefault(o => o.ParentID == provice.ID && o.Name == vm.City); if (city != null) { supply.ProviceID = city.ID; } } supply.CreateDate = DateTime.Now; ctx.Supplies.Add(supply); ctx.SaveChanges(); foreach (var imgName in vm.Images) { Image img = new Image(); img.Name = imgName; img.SupplyID = supply.ID; ctx.Images.Add(img); } ctx.SaveChanges(); tran.Complete(); ret = supply.ID; } catch (DbEntityValidationException) { } catch (Exception) { } } } return(ret); }
public IHttpActionResult DeleteSupply(SupplyVM supply) { var supp = db.Supplies.FirstOrDefault(s => s.SlackId.Equals(supply.SlackId) && s.Product.Id == supply.ProductId); if (supp == null) { return(NotFound()); } db.Supplies.Remove(supp); db.SaveChanges(); supp = db.Supplies.FirstOrDefault(s => s.SlackId.Equals(supply.SlackId) && s.Product.Id == supply.ProductId); if (supp != null) { return(Ok(false)); } return(Ok(true)); }
public IHttpActionResult PutSupply(SupplyVM supply) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Supply supp = null; if (db.Supplies.Any()) { supp = db.Supplies.FirstOrDefault(s => s.SlackId.Equals(supply.SlackId) && s.Product.Id == supply.ProductId); } if (supp != null) { supp.Quantity = supply.Quantity; db.SaveChanges(); return(Ok(supp)); } return(NotFound()); }
public IHttpActionResult PostSupply(SupplyVM supply) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Supply supp = null; if (db.Supplies.Any()) { supp = db.Supplies.FirstOrDefault(s => s.SlackId.Equals(supply.SlackId) && s.Product.Id == supply.ProductId); } if (supp == null) { var prod = db.Needs.FirstOrDefault(n => n.Id == supply.ProductId); if (prod != null) { supp = new Supply { SlackId = supply.SlackId, Product = prod, Quantity = supply.Quantity }; var newSupp = db.Supplies.Add(supp); db.SaveChanges(); return(Ok(newSupp)); } else { return(StatusCode(HttpStatusCode.NotFound)); } } return(StatusCode(HttpStatusCode.Found)); }