// 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) }
//RegisterUser ... Register a new user by creating his record as a Person and also as a user. //If Contact is not attached to a person, extract it from user detail and create a contact object func (usrlogic *UserLogic) RegisterUser(person models.Person, user models.User) (interface{}, interface{}, error) { var err error restfulds := dataaccess.RESTFul{} contact := models.Contacts{} if len(person.Contacts) == 0 { contact.Email = user.Email contact.PhoneNo = user.PhoneNum } person.Contacts = append(person.Contacts, contact) restfulds.ServiceURI = usrlogic.ServiceList["utility"] restfulds.Logger = usrlogic.Logger savedEntity, statusCode, err := restfulds.SaveObject(person, "person") if statusCode != 0 { return nil, nil, err } savedPerson := savedEntity.(map[string]interface{}) restfulds.ServiceURI = usrlogic.ServiceList["auth"] user.PersonID = int(savedPerson["ID"].(float64)) user.Verificationtoken = utility.RandStr(6, "number") user.BaseModel.Status = "UNACTIVATED" user.Emailverified = true savedEntity, statusCode, err = restfulds.SaveObject(user, "user") if statusCode != 0 { //Revert the person saved usrlogic.Logger.Crit(err.Error()) restfulds.ServiceURI = usrlogic.ServiceList["utility"] _ = restfulds.DeleteObject("person/" + strconv.Itoa(user.PersonID)) return nil, nil, err } savedUser := savedEntity.(map[string]interface{}) return savedPerson, savedUser, nil }