Beispiel #1
0
// CreateContact ... add new contact to a person
func (c PersonController) CreateContact(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	contact := new(models.Contacts)
	errs := binding.Bind(req, contact)
	if errs.Handle(res) {
		r.JSON(res, 422, errs.Error())
		return
	}
	bindingErr := contact.Validate(req, errs)
	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}

	p := models.Contacts{
		BaseModel:         c.BaseModel,
		PhoneNo:           contact.PhoneNo,
		PhoneNo2:          contact.PhoneNo2,
		PhoneNo3:          contact.PhoneNo3,
		Email:             contact.Email,
		Website:           contact.Website,
		FacebookID:        contact.FacebookID,
		PersonID:          contact.PersonID,
		CompanyEntitiesID: contact.CompanyEntitiesID}

	err := c.DataStore.SaveDatabaseObject(&p)
	if err != nil {
		panic(err)
	}
	// render response
	r.JSON(res, 200, p)
}