public IHttpActionResult PosttypeFactory(TypeFactoryViewModel Type_FactoryView_Model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } typeFactory typeFactory_db; try { typeFactory_db = new typeFactory { Id = Type_FactoryView_Model.Id, No = Type_FactoryView_Model.No, Name = Type_FactoryView_Model.Name }; db.typeFactories.Add(typeFactory_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 = typeFactory_db.Id }, ToViewModel(typeFactory_db))); }
public IHttpActionResult PuttypeFactory(int id, TypeFactoryViewModel Type_FactoryView_Model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != Type_FactoryView_Model.Id) { return(BadRequest()); } //把資料庫中的那筆資料讀出來 var typeFactory_db = db.typeFactories.Find(id); if (typeFactory_db == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!"))); } else { try { typeFactory_db.Id = Type_FactoryView_Model.Id; typeFactory_db.No = Type_FactoryView_Model.No; typeFactory_db.Name = Type_FactoryView_Model.Name; db.Entry(typeFactory_db).OriginalValues["Timestamp"] = Convert.FromBase64String(Type_FactoryView_Model.TimestampString); db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!typeFactoryExists(id)) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!")); } else { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, "這筆資料已被其他人修改!"));// "" } } } return(Ok(ToViewModel(typeFactory_db))); }