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