Implemented datapoint delete

This commit is contained in:
Magnus Åhall 2024-05-02 08:59:55 +02:00
parent 14ed83d227
commit 37f15c9f2a
6 changed files with 99 additions and 5 deletions

View File

@ -233,3 +233,10 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
return return
} // }}} } // }}}
func DatapointDelete(id int) (err error) {// {{{
_, err = service.Db.Conn.Exec(`DELETE FROM datapoint WHERE id=$1`, id)
if err != nil {
err = we.Wrap(err).WithData(id)
}
return
}// }}}

18
main.go
View File

@ -111,6 +111,7 @@ func main() { // {{{
service.Register("/datapoints", false, false, pageDatapoints) service.Register("/datapoints", false, false, pageDatapoints)
service.Register("/datapoint/edit/{id}", false, false, pageDatapointEdit) service.Register("/datapoint/edit/{id}", false, false, pageDatapointEdit)
service.Register("/datapoint/update/{id}", false, false, pageDatapointUpdate) service.Register("/datapoint/update/{id}", false, false, pageDatapointUpdate)
service.Register("/datapoint/delete/{id}", false, false, pageDatapointDelete)
service.Register("/triggers", false, false, pageTriggers) service.Register("/triggers", false, false, pageTriggers)
service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit) service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit)
@ -491,6 +492,23 @@ func pageDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.Header().Add("Location", "/datapoints") w.Header().Add("Location", "/datapoints")
w.WriteHeader(302) w.WriteHeader(302)
} // }}} } // }}}
func pageDatapointDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
httpError(w, we.Wrap(err).Log())
return
}
err = DatapointDelete(id)
if err != nil {
httpError(w, we.Wrap(err).Log())
return
}
w.Header().Add("Location", "/datapoints")
w.WriteHeader(302)
} // }}}
func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
areas, err := TriggersRetrieve() areas, err := TriggersRetrieve()

View File

@ -99,7 +99,7 @@ label {
} }
#datapoints { #datapoints {
display: grid; display: grid;
grid-template-columns: repeat(4, min-content); grid-template-columns: repeat(5, min-content);
grid-gap: 8px 16px; grid-gap: 8px 16px;
margin-top: 16px; margin-top: 16px;
} }

67
static/images/delete.svg Normal file
View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="16.000025"
height="18"
viewBox="0 0 4.2333398 4.7625001"
version="1.1"
id="svg8"
inkscape:version="1.3.2 (1:1.3.2+202311252150+091e20ef0f)"
sodipodi:docname="delete.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.70710678"
inkscape:cx="-458.9123"
inkscape:cy="132.93608"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
units="px"
inkscape:window-width="2190"
inkscape:window-height="1404"
inkscape:window-x="1463"
inkscape:window-y="16"
inkscape:window-maximized="0"
inkscape:showpageshadow="true"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d6d6d6"
showborder="true" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-214.57708,-116.15208)">
<title
id="title1">trash-can-outline</title>
<path
d="m 215.9,116.15208 v 0.26459 h -1.32292 v 0.52916 h 0.26459 v 3.43959 a 0.52916667,0.52916667 0 0 0 0.52916,0.52916 h 2.64584 a 0.52916667,0.52916667 0 0 0 0.52916,-0.52916 v -3.43959 h 0.26459 v -0.52916 h -1.32292 v -0.26459 H 215.9 m -0.52917,0.79375 h 2.64584 v 3.43959 h -2.64584 v -3.43959 M 215.9,117.475 v 2.38125 h 0.52917 V 117.475 H 215.9 m 1.05833,0 v 2.38125 h 0.52917 V 117.475 Z"
id="path1"
style="fill:#fb4934;fill-opacity:1;stroke-width:0.264583" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -2,7 +2,7 @@
#datapoints { #datapoints {
display: grid; display: grid;
grid-template-columns: repeat(4, min-content); grid-template-columns: repeat(5, min-content);
grid-gap: 8px 16px; grid-gap: 8px 16px;
margin-top: 16px; margin-top: 16px;

View File

@ -1,5 +1,6 @@
{{ define "page" }} {{ define "page" }}
<link rel="stylesheet" type="text/css" href="/css/{{ .VERSION }}/datapoints.css"> {{ $version := .VERSION }}
<link rel="stylesheet" type="text/css" href="/css/{{ .VERSION }}/datapoints.css">
{{ block "page_label" . }}{{end}} {{ block "page_label" . }}{{end}}
@ -10,6 +11,7 @@
<div class="header">Datatype</div> <div class="header">Datatype</div>
<div class="header">Last value</div> <div class="header">Last value</div>
<div class="header">Value</div> <div class="header">Value</div>
<div class="header"></div>
{{ range .Data.Datapoints }} {{ range .Data.Datapoints }}
<div class="line"></div> <div class="line"></div>
@ -21,7 +23,7 @@
{{ else }} {{ else }}
<div class="value">{{ .LastDatapointValue.Value }}</div> <div class="value">{{ .LastDatapointValue.Value }}</div>
{{ end }} {{ end }}
<div class="delete"><a href="/datapoint/delete/{{ .ID }}" onclick="confirm(`Are you sure you want to delete '{{ .Name }}'?`)"><img src="/images/{{ $version }}/delete.svg"></a></div>
{{ end }} {{ end }}
</div> </div>
{{ end }} {{ end }}