diff --git a/area.go b/area.go index 8d95156..18e2b38 100644 --- a/area.go +++ b/area.go @@ -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 { diff --git a/main.go b/main.go index 9496441..69f80c8 100644 --- a/main.go +++ b/main.go @@ -122,9 +122,11 @@ func main() { // {{{ service.Register("/area/new/{name}", false, false, areaNew) service.Register("/area/rename/{id}/{name}", false, false, areaRename) + service.Register("/area/delete/{id}", false, false, areaDelete) service.Register("/section/new/{areaID}/{name}", false, false, sectionNew) service.Register("/section/rename/{id}/{name}", false, false, sectionRename) + service.Register("/section/delete/{id}", false, false, sectionDelete) service.Register("/problems", false, false, pageProblems) service.Register("/problem/acknowledge/{id}", false, false, pageProblemAcknowledge) @@ -399,6 +401,24 @@ func areaRename(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ w.WriteHeader(302) return } // }}} +func areaDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ + idStr := r.PathValue("id") + id, err := strconv.Atoi(idStr) + if err != nil { + httpError(w, werr.Wrap(err).WithData(idStr).Log()) + return + } + + err = AreaDelete(id) + if err != nil { + httpError(w, werr.Wrap(err).WithData(id).Log()) + return + } + + w.Header().Add("Location", "/configuration") + w.WriteHeader(302) + return +} // }}} func sectionNew(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ idStr := r.PathValue("areaID") @@ -438,6 +458,24 @@ func sectionRename(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{ w.WriteHeader(302) return } // }}} +func sectionDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ + idStr := r.PathValue("id") + id, err := strconv.Atoi(idStr) + if err != nil { + httpError(w, werr.Wrap(err).WithData(idStr).Log()) + return + } + + err = SectionDelete(id) + if err != nil { + httpError(w, werr.Wrap(err).WithData(id).Log()) + return + } + + w.Header().Add("Location", "/configuration") + w.WriteHeader(302) + return +} // }}} func pageProblems(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ page := Page{ diff --git a/section.go b/section.go index b86f5c0..82177d9 100644 --- a/section.go +++ b/section.go @@ -1,6 +1,9 @@ package main import ( + // External + werr "git.gibonuddevalla.se/go/wrappederror" + // Standard "sort" ) @@ -22,11 +25,23 @@ func (s *Section) SortedTriggers() []Trigger { return s.Triggers } -func SectionCreate(areaID int, name string) (err error) {// {{{ +func SectionCreate(areaID int, name string) (err error) { // {{{ _, err = service.Db.Conn.Exec(`INSERT INTO section(area_id, name) VALUES($1, $2)`, areaID, name) return -}// }}} -func SectionRename(id int, name string) (err error) {// {{{ +} // }}} +func SectionRename(id int, name string) (err error) { // {{{ _, err = service.Db.Conn.Exec(`UPDATE section SET name=$2 WHERE id=$1`, id, name) return -}// }}} +} // }}} +func SectionDelete(id int) (err error) { // {{{ + _, err = service.Db.Conn.Exec(`DELETE FROM public.trigger WHERE section_id = $1`, id) + if err != nil { + return werr.Wrap(err).WithData(id) + } + + _, err = service.Db.Conn.Exec(`DELETE FROM public.section WHERE id = $1`, id) + if err != nil { + return werr.Wrap(err).WithData(id) + } + return +} // }}} diff --git a/static/css/configuration.css b/static/css/configuration.css new file mode 100644 index 0000000..e013c47 --- /dev/null +++ b/static/css/configuration.css @@ -0,0 +1,130 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} +*:focus { + outline: none; +} +[onClick] { + cursor: pointer; +} +html, +body { + margin: 0; + padding: 0; +} +body { + background: #282828; + font-family: sans-serif; + font-weight: 300; + color: #d5c4a1; + font-size: 11pt; +} +h1, +h2 { + margin-bottom: 4px; +} +h1:first-child, +h2:first-child { + margin-top: 0px; +} +h1 { + font-size: 1.5em; + color: #fb4934; + font-weight: 800; +} +h2 { + font-size: 1.25em; + color: #b8bb26; + font-weight: 800; +} +a { + color: #3f9da1; + text-decoration: none; +} +a:hover { + text-decoration: underline; +} +b { + font-weight: 800; +} +input[type="text"], +textarea, +select { + font-family: monospace; + background: #202020; + color: #d5c4a1; + padding: 4px 8px; + border: none; + font-size: 1em; + line-height: 1.5em; +} +button { + background: #202020; + color: #d5c4a1; + padding: 8px 32px; + border: 1px solid #535353; + font-size: 1em; + height: 3em; +} +button:focus { + background: #333; +} +.line { + grid-column: 1 / -1; + border-bottom: 1px solid #4e4e4e; +} +span.date { + color: #d5c4a1; + font-weight: 800; +} +span.time { + font-size: 0.9em; + color: #d5c4a1; +} +span.seconds { + display: none; +} +label { + user-select: none; +} +.description { + border: 1px solid #737373; + color: #3f9da1; + background: #202020; + padding: 4px 8px; + margin-top: 8px; + white-space: nowrap; + width: min-content; + border-radius: 8px; +} +#areas .area > .name { + display: grid; + grid-template-columns: 1fr min-content; + grid-gap: 0px 16px; + align-items: center; + padding-left: 16px; + padding-right: 8px; +} +#areas .area > .name img { + margin-top: 3px; + margin-bottom: 4px; + height: 16px; +} +#areas .area .section.configuration { + display: grid; + grid-template-columns: 1fr min-content; + grid-gap: 0 16px; + margin-top: 8px; + margin-bottom: 8px; +} +#areas .area .section.configuration:last-child { + margin-bottom: 16px; +} +#areas .area .section.configuration img { + height: 16px; +} diff --git a/static/css/main.css b/static/css/main.css index a213f0d..c5de4d4 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -182,10 +182,6 @@ label { margin-top: 12px; margin-bottom: 20px; } -#areas .area .section.configuration { - margin-top: 8px; - margin-bottom: 8px; -} #areas .area .section:last-child { margin-bottom: 12px; } diff --git a/static/images/delete_white.svg b/static/images/delete_white.svg new file mode 100644 index 0000000..7ae4dab --- /dev/null +++ b/static/images/delete_white.svg @@ -0,0 +1,67 @@ + + + + + + + + + + image/svg+xml + + + + + + trash-can-outline + + + diff --git a/static/less/configuration.less b/static/less/configuration.less new file mode 100644 index 0000000..8c59755 --- /dev/null +++ b/static/less/configuration.less @@ -0,0 +1,38 @@ +@import 'theme.less'; + +#areas { + .area { + & > .name { + display: grid; + grid-template-columns: 1fr min-content; + grid-gap: 0px 16px; + align-items: center; + padding-left: 16px; + padding-right: 8px; + + img { + margin-top: 3px; + margin-bottom: 4px; + height: 16px; + } + } + + + .section.configuration { + display: grid; + grid-template-columns: 1fr min-content; + grid-gap: 0 16px; + + margin-top: 8px; + margin-bottom: 8px; + + &:last-child { + margin-bottom: 16px; + } + + img { + height: 16px; + } + } + } +} diff --git a/static/less/main.less b/static/less/main.less index de80c87..d04e244 100644 --- a/static/less/main.less +++ b/static/less/main.less @@ -94,11 +94,6 @@ margin-top: 12px; margin-bottom: 20px; - &.configuration { - margin-top: 8px; - margin-bottom: 8px; - } - &:last-child { margin-bottom: 12px; } diff --git a/views/pages/configuration.gotmpl b/views/pages/configuration.gotmpl index 0fdfd54..aecf759 100644 --- a/views/pages/configuration.gotmpl +++ b/views/pages/configuration.gotmpl @@ -1,4 +1,6 @@ {{ define "page" }} + {{ $version := .VERSION }} + {{ block "page_label" . }}{{end}} @@ -58,14 +72,21 @@
{{ range .Data.Areas }}
-
{{ .Name }}
+
+
{{ .Name }}
+ +
Create
{{ range .SortedSections }} + {{ if eq .ID 0 }} + {{ continue }} + {{ end }}
{{ .Name }}
+
{{ end }}
diff --git a/views/pages/triggers.gotmpl b/views/pages/triggers.gotmpl index a0a5bef..093a340 100644 --- a/views/pages/triggers.gotmpl +++ b/views/pages/triggers.gotmpl @@ -38,6 +38,9 @@
{{ .Name }}
{{ range .SortedSections }} + {{ if eq .ID 0 }} + {{ continue }} + {{ end }}
{{ .Name }}