示例#1
0
// CreatePersonIDType ... add new contact to a person
func (c PersonController) CreatePersonIDType(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	personIDType := new(models.PersonIDType)
	errs := binding.Bind(req, personIDType)
	idTypes := models.IDType{}
	if errs.Handle(res) {
		r.JSON(res, 422, errs.Error())
		return
	}
	bindingErr := personIDType.Validate(req, errs)
	if bindingErr != nil {
		r.JSON(res, 422, bindingErr.Error())
		return
	}
	p := models.PersonIDType{
		BaseModel:      c.BaseModel,
		PersonID:       personIDType.PersonID,
		IDType:         personIDType.IDType,
		IDNumber:       personIDType.IDNumber,
		DateIssued:     personIDType.DateIssued,
		ExpiryDate:     personIDType.ExpiryDate,
		ScannedPicture: personIDType.ScannedPicture,
		IDTypes:        idTypes}
	err := c.DataStore.SaveDatabaseObject(&p)
	if err != nil {
		panic(err)
	}
	r.JSON(res, 200, p)
}
示例#2
0
// FetchPersonIDs ... Fetch all the regions/state of a country
func (c PersonController) FetchPersonIDs(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	r := render.New(render.Options{})
	vars := mux.Vars(req)
	personID := vars["person_id"]

	// find in the database
	ids := models.PersonIDType{}
	idTypes := models.IDType{}
	//query := c.Db.Where("person_id = ?", personID).Find(&ids)
	//c.Db.Model(&ids).Related(&idTypes, "IDType")
	qryparam := map[string]interface{}{"person_id": personID}
	err := c.DataStore.FetchRelatedObject(qryparam, &ids, &idTypes, "IDType")
	ids.IDTypes = idTypes

	if err != nil && err.ErrNo == 1001 {
		r.JSON(res, 404, err.Error())
	} else if err == nil {
		//User exist in database, confirm his password
		r.JSON(res, 200, ids)
	} else {
		panic(err)
	}
}