dim/layer3domain_list.go
Gibheer 01b87332d9 add a way to set attributes
For this to work, I have added a new function that takes a list of
key looking things and converts them into json.
At the same time, it also can convert json looking payloads and
prepare it for the database (that last part was not intended, but
works).

With the many columns where setting attributes is possible, this
functionality should help quite a bit.
2021-05-07 10:17:05 +02:00

53 lines
1.3 KiB
Go

package main
import (
"fmt"
"dim/query"
"dim/types"
)
type (
Layer3DomainListOptions struct {
Attributes types.FieldList `json:"attributes"`
}
)
var (
layer3DomainListMap = map[string]string{
"name": "l.name",
"modified_by": "l.modified_by",
"modified_at": "l.modified_at",
"created_by": "l.created_by",
"created_at": "l.created_at",
}
)
// Layer3DomainList lists all registered layer3domains.
func layer3DomainList(c *Context, req Request, res *Response) error {
options := Layer3DomainListOptions{
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("l", options.Attributes, layer3DomainListMap)
from := "layer3domains l"
queryStr := fmt.Sprintf(`select %s from %s`, selClause, from)
rows, err := c.tx.Query(queryStr)
if err != nil {
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not get layer3domain list: %s - query %s", err, queryStr)
}
defer rows.Close()
res.Result, err = query.RowsToMap(rows)
if err != nil {
res.Result = nil
res.AddMessage(LevelError, "could not return result")
return fmt.Errorf("could not parse layer3domain list: %#v", err)
}
return nil
}