smon/area.go

130 lines
2.3 KiB
Go
Raw Normal View History

2024-04-29 08:36:13 +02:00
package main
import (
// External
2024-06-02 09:17:50 +02:00
werr "git.gibonuddevalla.se/go/wrappederror"
2024-04-29 08:36:13 +02:00
// Standard
2024-06-02 09:17:50 +02:00
"database/sql"
2024-04-29 08:36:13 +02:00
"encoding/json"
"sort"
)
type Area struct {
ID int
2024-04-29 08:36:13 +02:00
Name string
Sections []Section
}
func AreaRetrieve() (areas []Area, err error) { // {{{
2024-04-29 08:36:13 +02:00
areas = []Area{}
row := service.Db.Conn.QueryRow(`
SELECT
jsonb_agg(jsonsections)
FROM (
SELECT
a.id,
a.name,
jsonb_agg(
jsonb_build_object(
'id', s.id,
'name', s.name
)
) AS sections
FROM area a
LEFT JOIN section s ON s.area_id = a.id
2024-04-29 08:36:13 +02:00
GROUP BY
a.id, a.name
) jsonsections`,
)
var jsonData []byte
err = row.Scan(&jsonData)
if err != nil {
2024-06-02 09:17:50 +02:00
err = werr.Wrap(err)
2024-04-29 08:36:13 +02:00
return
}
if jsonData == nil {
return
}
2024-04-29 08:36:13 +02:00
err = json.Unmarshal(jsonData, &areas)
if err != nil {
2024-06-02 09:17:50 +02:00
err = werr.Wrap(err)
2024-04-29 08:36:13 +02:00
return
}
return
} // }}}
2024-06-02 09:17:50 +02:00
func AreaCreate(name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`INSERT INTO area(name) VALUES($1)`, name)
return
2024-06-02 09:17:50 +02:00
} // }}}
func AreaRename(id int, name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`UPDATE area SET name=$2 WHERE id=$1`, id, name)
2024-04-29 08:36:13 +02:00
return
2024-06-02 09:17:50 +02:00
} // }}}
func AreaDelete(id int) (err error) { // {{{
var trx *sql.Tx
trx, err = service.Db.Conn.Begin()
if err != nil {
err = werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`
DELETE
FROM trigger t
USING section s
WHERE
t.section_id = s.id AND
s.area_id = $1
`,
id,
)
if err != nil {
err2 := trx.Rollback()
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`DELETE FROM public.section WHERE area_id = $1`, id)
if err != nil {
err2 := trx.Rollback()
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`DELETE FROM public.area WHERE id = $1`, id)
if err != nil {
err2 := trx.Rollback()
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
err = trx.Commit()
if err != nil {
err2 := trx.Rollback()
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
return nil
} // }}}
2024-04-29 08:36:13 +02:00
func (a Area) SortedSections() []Section { // {{{
sort.SliceStable(a.Sections, func(i, j int) bool {
2024-04-29 08:36:13 +02:00
return a.Sections[i].Name < a.Sections[j].Name
})
return a.Sections
} // }}}