Compare commits

..

2 Commits

Author SHA1 Message Date
Magnus Åhall
a120100305 Bumped to v16 2024-05-30 15:06:57 +02:00
Magnus Åhall
37bf692984 Better trigger create 2024-05-30 15:06:41 +02:00
3 changed files with 86 additions and 3 deletions

35
main.go
View File

@ -27,7 +27,7 @@ import (
"time"
)
const VERSION = "v15"
const VERSION = "v16"
var (
logger *slog.Logger
@ -137,6 +137,7 @@ func main() { // {{{
service.Register("/datapoint/values/{id}", false, false, pageDatapointValues)
service.Register("/triggers", false, false, pageTriggers)
service.Register("/trigger/create/{sectionID}/{name}", false, false, triggerCreate)
service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit)
service.Register("/trigger/edit/{id}/{sectionID}", false, false, pageTriggerEdit)
service.Register("/trigger/addDatapoint/{id}/{datapointName}", false, false, pageTriggerDatapointAdd)
@ -672,6 +673,38 @@ func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
page.Render(w)
} // }}}
func triggerCreate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
name := r.PathValue("name")
sectionIDStr := r.PathValue("sectionID")
sectionID, err := strconv.Atoi(sectionIDStr)
if err != nil {
httpError(w, werr.Wrap(err).WithData(sectionIDStr).Log())
return
}
t, err := TriggerCreate(sectionID, name)
if err != nil {
httpError(w, werr.Wrap(err).WithData(struct {
SectionID int
Name string
}{
sectionID,
name,
}).Log())
return
}
resp := struct {
OK bool
Trigger Trigger
}{
true,
t,
}
j, _ := json.Marshal(resp)
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)

View File

@ -7,6 +7,7 @@ import (
"github.com/lib/pq"
// Standard
"database/sql"
"encoding/json"
"fmt"
"strings"
@ -20,6 +21,13 @@ type Trigger struct {
Datapoints []string
}
func TriggerCreate(sectionID int, name string) (t Trigger, err error) { // {{{
t.SectionID = sectionID
t.Name = name
t.Expression = "false"
err = t.Update()
return
} // }}}
func TriggersRetrieve() (areas []Area, err error) { // {{{
areas = []Area{}
@ -137,15 +145,34 @@ func (t *Trigger) Update() (err error) { // {{{
}
jsonDatapoints, _ := json.Marshal(t.Datapoints)
if t.ID == 0 {
_, err = service.Db.Conn.Exec(`
var row *sql.Row
row = service.Db.Conn.QueryRow(`
INSERT INTO "trigger"(name, section_id, expression, datapoints)
VALUES($1, $2, $3, $4)
RETURNING id
`,
t.Name,
t.SectionID,
t.Expression,
jsonDatapoints,
)
err = row.Scan(&t.ID)
if err != nil {
err = we.Wrap(err).WithData(
struct {
SectionID int
Name string
Expression string
JsonDataPoints []byte
}{
t.SectionID,
t.Name,
t.Expression,
jsonDatapoints,
},
)
return
}
} else {
_, err = service.Db.Conn.Exec(`
UPDATE "trigger"

View File

@ -3,6 +3,29 @@
{{ block "page_label" . }}{{end}}
{{ $version := .VERSION }}
<script type="text/javascript">
function createTrigger(sectionID) {
let name = prompt("Trigger name")
if (name === null)
return
if (name.trim() == "") {
alert('A name is required')
return
}
fetch(`/trigger/create/${sectionID}/${encodeURIComponent(name)}`)
.then(data=>data.json())
.then(json=>{
if (!json.OK) {
alert(json.Error)
return
}
location.href = `/trigger/edit/${json.Trigger.ID}`
})
}
</script>
<div id="areas">
{{ range .Data.Areas }}
<div class="area">
@ -11,7 +34,7 @@
<div class="section">
<div class="create">
<div class="name">{{ .Name }}</div>
<div class="new"><a href="/trigger/edit/0/{{ .ID }}">+</a></div>
<div class="new"><a onclick="createTrigger({{ .ID }})">+</a></div>
</div>
<div class="triggers">