Exemplo n.º 1
0
// CreateCustomerType ... Add new customer
func (cust CustomerController) CreateCustomerType(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	custType := new(models.CustomerType)
	errs := binding.Bind(req, custType)
	if errs.Handle(res) {
		r.JSON(res, 422, errs.Error())
		return
	}
	bindingErr := custType.Validate(req, errs)
	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}
	p := models.CustomerType{cust.BaseModel, custType.Name, custType.Code, custType.Description}
	//p := models.User{acct.BaseModel, user.Realm, user.Username, user.Password, user.Credential, user.Challenges, user.Email, user.Emailverified, user.Verificationtoken,
	//	user.LogInCummulative, user.FailedAttemptedLogin, uuid.New(), user.PersonID, user.PhoneNum, user.VerifiedPhoneNum}

	tx := cust.Db.Begin()

	if err := tx.Create(&p).Error; err != nil {
		tx.Rollback()
		panic(err)
	}
	tx.Commit()
	// render response
	r.JSON(res, 200, p)
}
Exemplo n.º 2
0
// CreateCustomerType ... Add new customer
func (acct AccountController) CreateCustomerType(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	custType := new(models.CustomerType)
	errs := binding.Bind(req, custType)
	if errs.Handle(res) {
		r.JSON(res, 422, errs.Error())
		return
	}
	bindingErr := custType.Validate(req, errs)
	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}
	p := models.CustomerType{acct.BaseModel, custType.Name, custType.Code, custType.Description}
	err := acct.DataStore.SaveDatabaseObject(&p)
	if err != nil {
		panic(err)
	}
	// render response
	r.JSON(res, 200, p)
}