Added area deletion

This commit is contained in:
Magnus Åhall 2024-06-02 09:17:50 +02:00
parent 72f23b9c4d
commit b83adad7c8
10 changed files with 379 additions and 21 deletions

69
area.go
View file

@ -2,9 +2,10 @@ package main
import (
// External
re "git.gibonuddevalla.se/go/wrappederror"
werr "git.gibonuddevalla.se/go/wrappederror"
// Standard
"database/sql"
"encoding/json"
"sort"
)
@ -41,7 +42,7 @@ func AreaRetrieve() (areas []Area, err error) { // {{{
var jsonData []byte
err = row.Scan(&jsonData)
if err != nil {
err = re.Wrap(err)
err = werr.Wrap(err)
return
}
@ -51,20 +52,74 @@ func AreaRetrieve() (areas []Area, err error) { // {{{
err = json.Unmarshal(jsonData, &areas)
if err != nil {
err = re.Wrap(err)
err = werr.Wrap(err)
return
}
return
} // }}}
func AreaCreate(name string) (err error) {// {{{
func AreaCreate(name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`INSERT INTO area(name) VALUES($1)`, name)
return
}// }}}
func AreaRename(id int, name string) (err error) {// {{{
} // }}}
func AreaRename(id int, name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`UPDATE area SET name=$2 WHERE id=$1`, id, name)
return
}// }}}
} // }}}
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
} // }}}
func (a Area) SortedSections() []Section { // {{{
sort.SliceStable(a.Sections, func(i, j int) bool {