private IQueryable<CustomerViewModel> ToViewModels(Customer[] customers) { return customers.Select(c => ToViewModel(c)).AsQueryable(); }
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)); }
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) }; }