2021-04-28 21:09:38 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:39:41 +02:00
|
|
|
selClause := query.FieldListToSelect("z", options.Attributes, zoneListMap)
|
|
|
|
from := "zones z"
|
|
|
|
queryStr := fmt.Sprintf(`select %s from %s`, selClause, from)
|
|
|
|
rows, err := c.tx.Query(queryStr)
|
2021-04-28 21:09:38 +02:00
|
|
|
if err != nil {
|
|
|
|
res.AddMessage(LevelError, "could not return result")
|
2021-05-03 19:39:41 +02:00
|
|
|
return fmt.Errorf("could not get zone list: %s - query: %s", err, queryStr)
|
2021-04-28 21:09:38 +02:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
2021-05-03 19:39:41 +02:00
|
|
|
res.Result, err = query.RowsToMap(rows)
|
2021-04-28 21:09:38 +02:00
|
|
|
if err != nil {
|
2021-05-03 19:39:41 +02:00
|
|
|
res.Result = nil
|
2021-04-28 21:09:38 +02:00
|
|
|
res.AddMessage(LevelError, "could not return result")
|
|
|
|
return fmt.Errorf("could not parse zone list: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|