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 GetcatPay(int id)
        {
            catPay catPay = db.catPays.Find(id);

            if (catPay == null)
            {
                return(NotFound());
            }

            return(Ok(ToViewModel(catPay)));
        }
        public IHttpActionResult DeletecatPay(int id)
        {
            catPay catPay = db.catPays.Find(id);

            if (catPay == null)
            {
                return(NotFound());
            }

            db.catPays.Remove(catPay);
            db.SaveChanges();

            return(Ok(ToViewModel(catPay)));
        }
        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 CatPayViewModel ToViewModel(catPay c)
 {
     return(new CatPayViewModel {
         Id = c.Id, No = c.No, Name = c.Name, TimestampString = Convert.ToBase64String(c.Timestamp)
     });
 }