예제 #1
0
func (datastore *DataStore) FindAllPersons() ([]model.Person, error) {
	persons := []model.Person{}
	personDocuments := []model.PersonDocument{}

	err := datastore.session.DB(DBNAME).C(TABLE).Find(nil).All(&personDocuments)
	if err != nil {
		return persons, err
	}

	for _, personDocument := range personDocuments {
		person := model.Person{}
		person.Name = personDocument.Name
		person.Gender = personDocument.Gender
		person.Age = personDocument.Age
		persons = append(persons, person)
	}

	return persons, nil
}
예제 #2
0
func (datastore *DataStore) FindPerson(id string) (model.Person, error) {
	person := model.Person{}
	personDocument := &model.PersonDocument{}

	if !bson.IsObjectIdHex(id) {
		return person, errors.New("Invalid Id")
	}

	personId := bson.ObjectIdHex(id)
	err := datastore.session.DB(DBNAME).C(TABLE).FindId(personId).One(personDocument)
	if err != nil {
		return person, err
	}

	person.Name = personDocument.Name
	person.Gender = personDocument.Gender
	person.Age = personDocument.Age
	return person, nil
}