smon/section.go
2026-08-19 17:53:52 +02:00

54 lines
1.2 KiB
Go

package main
import (
// External
werr "git.ahall.se/go/wrappederror"
// Standard
"context"
"sort"
)
type Section struct {
ID int
Name string
AreaID int `db:"area_id"`
Area Area
Triggers []Trigger
}
func (s *Section) SortedTriggers() []Trigger {
sort.SliceStable(s.Triggers, func(i, j int) bool {
return s.Triggers[i].Name < s.Triggers[j].Name
})
return s.Triggers
}
func SectionCreate(areaID int, name string) (err error) { // {{{
_, err = db.Exec(context.Background(), `INSERT INTO section(area_id, name) VALUES($1, $2)`, areaID, name)
if err != nil {
err = werr.Wrap(err)
}
return
} // }}}
func SectionRename(id int, name string) (err error) { // {{{
_, err = db.Exec(context.Background(), `UPDATE section SET name=$2 WHERE id=$1`, id, name)
if err != nil {
err = werr.Wrap(err)
}
return
} // }}}
func SectionDelete(id int) (err error) { // {{{
_, err = db.Exec(context.Background(), `DELETE FROM public.trigger WHERE section_id = $1`, id)
if err != nil {
return werr.Wrap(err).WithData(id)
}
_, err = db.Exec(context.Background(), `DELETE FROM public.section WHERE id = $1`, id)
if err != nil {
return werr.Wrap(err).WithData(id)
}
return
} // }}}