// POST api/BillofMaterial public HttpResponseMessage PostBillofMaterial(BillofMaterial billofmaterial) { if (ModelState.IsValid) { string CustomCode = "BOM-" + DateTime.Now.ToString("yyyyMMdd"); int? MaxCode = Convert.ToInt32((db.BillofMaterials.Where(r => r.BillofMaterialCode.StartsWith(CustomCode)).Select(r => r.BillofMaterialCode.Substring(CustomCode.Length, 4)).ToList()).Max()); string BOMCode = CustomCode + ((MaxCode + 1).ToString()).PadLeft(4, '0'); billofmaterial.BillofMaterialCode = BOMCode; billofmaterial.InsertBy = loginUser.UserID; db.BillofMaterials.Add(billofmaterial); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, billofmaterial); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = billofmaterial.BillofMaterialID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/BillofMaterial/5 public HttpResponseMessage PutBillofMaterial(long id, BillofMaterial billofmaterial) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } billofmaterial.Collaborator = null; billofmaterial.ProjectSetup = null; billofmaterial.ProcesStatus = null; if (id != billofmaterial.BillofMaterialID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } billofmaterial.UpdateBy = loginUser.UserID; db.Entry(billofmaterial).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }