Пример #1
0
func getPeople(rend render.Render, req *http.Request, dbh *db.DBHandle) {
	err := req.ParseForm()
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	var people_json []*personWithRelations
	param_ids := req.Form["ids[]"]
	if len(param_ids) > 0 {
		people_ids, err := parseParamIds(param_ids)
		if err != nil {
			rend.JSON(500, err.Error())
			return
		}
		people_json = make([]*personWithRelations, len(people_ids))
		for i, pid := range people_ids {
			person, err := dbh.GetPersonById(pid)
			if err != nil {
				if err == orm.ErrNoRows {
					rend.JSON(404, fmt.Sprintf("Person with ID %d doesn't exist", pid))
					return
				} else {
					rend.JSON(500, err.Error())
					return
				}
			}
			pn, err := newPersonWithRelations(person, dbh)
			if err != nil {
				rend.JSON(500, err)
				return
			}

			people_json[i] = &pn
		}
	} else {
		people, err := dbh.GetPeopleById([]int64{})
		if err != nil {
			rend.JSON(500, err)
			return
		}

		people_json = make([]*personWithRelations, len(people))
		for i, p := range people {
			pn, err := newPersonWithRelations(p, dbh)
			if err != nil {
				rend.JSON(500, err)
				return
			}
			people_json[i] = &pn
		}
	}
	rend.JSON(200, people_json)
}
Пример #2
0
func getPerson(rend render.Render, params martini.Params, dbh *db.DBHandle) {
	id, err := strconv.ParseInt(params["id"], 10, 64)
	if err != nil {
		rend.JSON(500, "Invalid id: "+err.Error())
		return
	}
	p, err := dbh.GetPersonById(id)
	if err != nil {
		if err == orm.ErrNoRows {
			rend.JSON(404, fmt.Sprintf("No Person with id %d found.", id))
			return
		} else {
			rend.JSON(500, err.Error())
			return
		}
	}

	pn, err := newPersonWithRelations(p, dbh)
	if err != nil {
		rend.JSON(500, err)
		return
	}
	rend.JSON(200, pn)
}