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 GetcatCustomer(int id) { catCustomer catCustomer = db.catCustomers.Find(id); if (catCustomer == null) { return(NotFound()); } return(Ok(ToViewModel(catCustomer))); }
public IHttpActionResult DeletecatCustomer(int id) { catCustomer catCustomer = db.catCustomers.Find(id); if (catCustomer == null) { return(NotFound()); } db.catCustomers.Remove(catCustomer); db.SaveChanges(); return(Ok(ToViewModel(catCustomer))); }
private CustomerViewModel ToViewModel(Customer c) { catCustomer catCustomer = c.catCustomer; if (catCustomer == null) { catCustomer = db.catCustomers.Find(c.Cat); } catPay catPay = c.catPay; if (catPay == null) { catPay = db.catPays.Find(c.Pay); } catCurrency catCurrency = c.catCurrency; if (catCurrency == null) { catCurrency = db.catCurrencies.Find(c.Currency); } return(new CustomerViewModel { Id = c.Id, Cat = c.Cat, CatName = catCustomer == null ? "" : catCustomer.Name, PayName = catPay == null ? "" : catPay.Name, CurrencyName = catCurrency == null ? "" : catCurrency.Name, Currency = c.Currency, Pay = c.Pay, NO = c.NO, Name = c.Name, Sname = c.Sname, Unid = c.Unid, Contact1 = c.Contact1, Contact2 = c.Contact2, Email1 = c.Email1, Email2 = c.Email2, Email3 = c.Email3, Telephone1 = c.Telephone1, Telephone2 = c.Telephone2, Telephone3 = c.Telephone3, Website = c.Website, Fax = c.Fax, Address = c.Address, Shipaddr = c.Shipaddr, Invoiceaddr = c.Invoiceaddr, Lasttrade = c.Lasttrade, Note = c.Note, TimestampString = Convert.ToBase64String(c.Timestamp) }); }
private CatCustomerViewModel ToViewModel(catCustomer c) { return(new CatCustomerViewModel { Id = c.Id, No = c.No, Name = c.Name, TimestampString = Convert.ToBase64String(c.Timestamp) }); }