Initial commit
This commit is contained in:
commit
89f483171a
43 changed files with 2245 additions and 0 deletions
62
area.go
Normal file
62
area.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
// External
|
||||
re "git.gibonuddevalla.se/go/wrappederror"
|
||||
|
||||
// Standard
|
||||
"encoding/json"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Area struct {
|
||||
ID int
|
||||
Name string
|
||||
|
||||
Sections []Section
|
||||
}
|
||||
|
||||
func AreaRetrieve() (areas []Area, err error) {// {{{
|
||||
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
|
||||
INNER JOIN section s ON s.area_id = a.id
|
||||
GROUP BY
|
||||
a.id, a.name
|
||||
) jsonsections`,
|
||||
)
|
||||
|
||||
var jsonData []byte
|
||||
err = row.Scan(&jsonData)
|
||||
if err != nil {
|
||||
err = re.Wrap(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonData, &areas)
|
||||
if err != nil {
|
||||
err = re.Wrap(err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}// }}}
|
||||
|
||||
func (a Area) SortedSections() []Section {// {{{
|
||||
sort.SliceStable(a.Sections, func (i, j int) bool {
|
||||
return a.Sections[i].Name < a.Sections[j].Name
|
||||
})
|
||||
return a.Sections
|
||||
}// }}}
|
||||
Loading…
Add table
Add a link
Reference in a new issue