dim/zone_list.go

51 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"dim/query"
"dim/types"
)
type (
ZoneListOptions struct {
Attributes types.FieldList `json:"attributes"`
}
)
var (
zoneListMap = map[string]string{
"name": "z.name",
"modified_by": "z.modified_by",
"modified_at": "z.modified_at",
"created_by": "z.modified_by",
"created_at": "z.modified_at",
}
)
func zoneList(c *Context, req Request, res *Response) error {
options := ZoneListOptions{
Attributes: types.NewFieldList("name"),
}
if err := req.ParseAtLeast(0, &options); err != nil {
res.AddMessage(LevelError, "could not parse options: %s", err)
return nil
}
selClause := query.FieldListToSelect(options.Attributes, zoneListMap)
rows, err := c.tx.Query(fmt.Sprintf(`select %s from zones z`, selClause))
if err != nil {
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not get zone list: %s", err)
}
defer rows.Close()
res.Result["zones"], err = query.RowsToMap(rows)
if err != nil {
delete(res.Result, "zones")
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not parse zone list: %v", err)
}
return nil
}