dim/layer3domain_create.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

29 lines
778 B
Go

package main
import (
"fmt"
)
type (
Layer3DomainCreateOptions string
)
// Layer3DomainCreate creates a new layer3domain.
func layer3DomainCreate(c *Context, req Request, res *Response) error {
name := ""
options := Layer3DomainCreateOptions("{}")
if err := req.ParseAtLeast(1, &name, &options); err != nil {
res.AddMessage(LevelError, "could not parse parameter: %s", err)
return nil
}
_, err := c.tx.Exec(`insert into layer3domains(name, attributes, created_by, modified_by)
values ($1, $2::jsonb, $3, $3)`, name, options, c.username)
// TODO handle unique constraint violation
if err != nil {
res.AddMessage(LevelError, "could not create layer3domain '%s'", name)
return fmt.Errorf("could not insert layer3domain '%s': %s", name, err)
}
return nil
}