public IHttpActionResult PostCustomer(CustomerViewModel customer_View_Model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } Customer customer_db; try { customer_db = new Customer { Id = customer_View_Model.Id, Cat = customer_View_Model.Cat, NO = customer_View_Model.NO, Name = customer_View_Model.Name, Sname = customer_View_Model.Sname, Unid = customer_View_Model.Unid, Contact1 = customer_View_Model.Contact1, Contact2 = customer_View_Model.Contact2, Email1 = customer_View_Model.Email1, Email2 = customer_View_Model.Email2, Email3 = customer_View_Model.Email3, Telephone1 = customer_View_Model.Telephone1, Telephone2 = customer_View_Model.Telephone2, Telephone3 = customer_View_Model.Telephone3, Website = customer_View_Model.Website, Fax = customer_View_Model.Fax, Address = customer_View_Model.Address, Shipaddr = customer_View_Model.Shipaddr, Invoiceaddr = customer_View_Model.Invoiceaddr, Pay = customer_View_Model.Pay, Currency = customer_View_Model.Currency, Lasttrade = customer_View_Model.Lasttrade, Note = customer_View_Model.Note }; db.Customers.Add(customer_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 = customer_db.Id }, ToViewModel(customer_db)); }
public IHttpActionResult PutCustomer(int id, CustomerViewModel customer_View_Model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != customer_View_Model.Id) { return BadRequest(); } //把資料庫中的那筆資料讀出來 var customer_db = db.Customers.Find(id); if (customer_db == null) { return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!")); } else { try { //customer_db.catCustomer.Name = customer_View_Model.CatName; customer_db.Cat = customer_View_Model.Cat; customer_db.NO = customer_View_Model.NO; customer_db.Name = customer_View_Model.Name; customer_db.Sname = customer_View_Model.Sname; customer_db.Unid = customer_View_Model.Unid; customer_db.Contact1 = customer_View_Model.Contact1; customer_db.Contact2 = customer_View_Model.Contact2; customer_db.Email1 = customer_View_Model.Email1; customer_db.Email2 = customer_View_Model.Email2; customer_db.Email3 = customer_View_Model.Email3; customer_db.Telephone1 = customer_View_Model.Telephone1; customer_db.Telephone2 = customer_View_Model.Telephone2; customer_db.Telephone3 = customer_View_Model.Telephone3; customer_db.Website = customer_View_Model.Website; customer_db.Fax = customer_View_Model.Fax; customer_db.Address = customer_View_Model.Address; customer_db.Shipaddr = customer_View_Model.Shipaddr; customer_db.Invoiceaddr = customer_View_Model.Invoiceaddr; customer_db.Pay = customer_View_Model.Pay; //customer_db.catPay.Name = customer_View_Model.PayName; customer_db.Currency = customer_View_Model.Currency; //customer_db.catCurrency.Name = customer_View_Model.CurrencyName; customer_db.Lasttrade = customer_View_Model.Lasttrade; customer_db.Note = customer_View_Model.Note; db.Entry(customer_db).OriginalValues["Timestamp"] = Convert.FromBase64String(customer_View_Model.TimestampString); db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!")); else throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, "這筆資料已被其他人修改!"));// "" } } return Ok(ToViewModel(customer_db)); }