예제 #1
0
//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
}
예제 #2
0
// CreatePerson ... create new person
func (c PersonController) CreatePerson(res http.ResponseWriter, req *http.Request) {
	r := render.New(render.Options{})
	person := new(models.Person)
	errs := binding.Bind(req, person)
	if errs.Handle(res) {
		r.JSON(res, 422, errs.Error())
		return
	}
	bindingErr := person.Validate(req, errs)

	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}
	// save to database
	p := models.Person{
		BaseModel:            c.BaseModel,
		FirstName:            person.FirstName,
		MiddleName:           person.MiddleName,
		LastName:             person.LastName,
		Title:                person.Title,
		Gender:               person.Gender,
		Occupation:           person.Occupation,
		DateOfBirth:          person.DateOfBirth,
		Addresses:            person.Addresses,
		Contacts:             person.Contacts,
		PersonIDType:         person.PersonIDType,
		CountryOfOriginID:    person.CountryOfOriginID,
		CountryOfResidenceID: person.CountryOfResidenceID}

	err := c.DataStore.SaveDatabaseObject(&p)
	if err != nil {
		panic(err)
	}

	// render response
	r.JSON(res, 200, p)
}