Ejemplo n.º 1
0
//Index returns a list of italian cities, filtered by the prefix header and the maximum
//number returned controlled by the max parameter.
func (STATELESS *ItalianCitiesResource) Index(headers map[string]string,
	qp map[string]string) (string, *seven5.Error) {

	result := []*ItalianCity{}
	prefix, hasPrefix := headers["Prefix"] //note the capital is always there on headers
	maxStr, hasMax := qp["max"]
	var max int
	var err error

	if hasMax {
		if max, err = strconv.Atoi(maxStr); err != nil {
			return seven5.BadRequest(fmt.Sprintf("can't undestand max parameter %s", maxStr))
		}
	}
	for _, v := range cityData {
		if hasPrefix && !strings.HasPrefix(string(v.Name), prefix) {
			continue
		}
		result = append(result, v)
		if hasMax && len(result) == max {
			break
		}
	}
	return seven5.JsonResult(result, true)
}
Ejemplo n.º 2
0
//given an id, find the object it referencs and return JSON for it. This ignores
//the query parameters but understands the header 'Round' for rounding pop figures to
//100K boundaries.
func (STATELESS *ItalianCityResource) Find(id seven5.Id, hdrs map[string]string,
	query map[string]string) (string, *seven5.Error) {

	r, hasRound := hdrs["Round"] //note the capital is always there on headers
	n := int64(id)
	if n < 0 || n >= int64(len(cityData)) {
		return seven5.BadRequest(fmt.Sprintf("id must be from 0 to %d", len(cityData)-1))
	}
	pop := cityData[id].Population
	if hasRound && strings.ToLower(r) == "true" {
		excess := cityData[id].Population % 100000
		pop -= excess
		if excess >= 50000 {
			pop += 100000
		}
	}
	data := cityData[id]
	forClient := &ItalianCity{data.Id, data.Name, pop, data.Province, data.Location}
	return seven5.JsonResult(forClient, true)
}