Skip to content

Commit

Permalink
add and delete layer
Browse files Browse the repository at this point in the history
  • Loading branch information
snadrus committed Apr 30, 2024
1 parent f4763e3 commit 5b11bd6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions curiosrc/web/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,26 @@ func Routes(r *mux.Router, deps *deps.Deps) {
// At edit.html:
r.Methods("GET").Path("/schema").HandlerFunc(getSch)
r.Methods("GET").Path("/layers/{layer}").HandlerFunc(c.getLayer)
r.Methods("POST").Path("/addlayer").HandlerFunc(c.addLayer)
r.Methods("POST").Path("/layers/{layer}").HandlerFunc(c.setLayer)
r.Methods("GET").Path("/default").HandlerFunc(c.def)
}

func (c *cfg) addLayer(w http.ResponseWriter, r *http.Request) {
var layer struct {
Name string
}
apihelper.OrHTTPFail(w, json.NewDecoder(r.Body).Decode(&layer))
ct, err := c.DB.Exec(context.Background(), `INSERT INTO harmony_config (title, config) VALUES ($1, $2)`, layer.Name, "")
apihelper.OrHTTPFail(w, err)
if ct != 1 {
w.WriteHeader(http.StatusConflict)
w.Write([]byte("Layer already exists"))

Check failure on line 47 in curiosrc/web/api/config/config.go

View workflow job for this annotation

GitHub Actions / Check (lint-all)

Error return value of `w.Write` is not checked (errcheck)
return
}
w.WriteHeader(200)
}

func getSch(w http.ResponseWriter, r *http.Request) {
ref := jsonschema.Reflector{
Mapper: func(i reflect.Type) *jsonschema.Schema {
Expand Down
28 changes: 28 additions & 0 deletions curiosrc/web/static/config/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
this.loadData();
this.message = this.readCookie('message');
this.eraseCookie('message');
this.addName = '';
}
static get styles() {
return [css`
Expand All @@ -46,6 +47,13 @@
</tr>
`)}
</table>
<input autofocus type="text" id="layername" placeholder="Layer Name" @change=${this.updateName}>
<button class="button" @click=${this.addLayer}>Add Layer</button>
<hr>
<span>
To delete a layer, use ysqlsh to issue the following command:<br>
<code lang=sql>DELETE FROM curio.harmony_config WHERE title = 'my_layer_name';</code>
</span>
`;
}
loadData() {
Expand Down Expand Up @@ -77,6 +85,26 @@
eraseCookie(name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}

updateName(e) {
this.addName = e.target.value;
}
addLayer() {
// get a name
var v = this.addName;
if (v === '') {
alert('Error: Layer name cannot be empty');
return;
}

axios.post('/api/config/addlayer', { name: v })
.then(response => {
window.location.href = '/config/edit.html?layer=' + v;
})
.catch(error => {
alert('Error adding layer:', error);
});
}
});
</script>
<curio-ux>
Expand Down

0 comments on commit 5b11bd6

Please sign in to comment.