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