public IHttpActionResult PostcatFactory(CatFactoryViewModel Cat_FactoryView_Model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } catFactory catFactory_db; try { catFactory_db = new catFactory { Id = Cat_FactoryView_Model.Id, No = Cat_FactoryView_Model.No, Name = Cat_FactoryView_Model.Name }; db.catFactories.Add(catFactory_db); db.SaveChanges(); } catch (DbEntityValidationException ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, "DbEntityValidationException:" + ex.Message)); } catch (Exception ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message)); } return CreatedAtRoute("DefaultApi", new { id = catFactory_db.Id }, ToViewModel(catFactory_db)); }
public IHttpActionResult PutcatFactory(int id, CatFactoryViewModel Cat_FactoryView_Model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != Cat_FactoryView_Model.Id) { return BadRequest(); } //把資料庫中的那筆資料讀出來 var catFactory_db = db.catFactories.Find(id); if (catFactory_db == null) { return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!")); } else { try { catFactory_db.Id = Cat_FactoryView_Model.Id; catFactory_db.No = Cat_FactoryView_Model.No; catFactory_db.Name = Cat_FactoryView_Model.Name; db.Entry(catFactory_db).OriginalValues["Timestamp"] = Convert.FromBase64String(Cat_FactoryView_Model.TimestampString); db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!catFactoryExists(id)) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!")); else throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, "這筆資料已被其他人修改!"));// "" } } return Ok(ToViewModel(catFactory_db)); }