From 3f828ab85581dbe94403340f46b5e610e6ab181c Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sat, 13 Apr 2024 00:38:36 +0100 Subject: [PATCH] feat: add button to remove image from class --- logic/models/data.go | 38 ++++++++++++++++--- logic/utils/handler.go | 10 +++++ logic/utils/utils.go | 2 +- .../src/routes/models/edit/ModelTable.svelte | 20 +++++++++- 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/logic/models/data.go b/logic/models/data.go index a55c305..13610ad 100644 --- a/logic/models/data.go +++ b/logic/models/data.go @@ -376,6 +376,35 @@ func processZipFileExpand(c *Context, model *BaseModel) { ModelUpdateStatus(c, model.Id, READY) } + +func handleRemoveDataPoint(c *Context) *Error { + var dat JustId + if err := c.ToJSON(&dat); err != nil { + return err + } + + var GetModelId struct { + Value string `db:"m.id"` + Format string `db:"m.format"` + } + + err := GetDBOnce(c, &GetModelId, "model_data_point as mdp inner join model_classes as mc on mdp.class_id=mc.id inner join models as m on m.id=mc.model_id where mdp.id=$1;", dat.Id) + if err == NotFoundError { + return c.SendJSONStatus(404, "Data point not found") + } else if err != nil { + return c.E500M("Failed to find data point", err) + } + + os.Remove(path.Join("savedData", GetModelId.Value, "data", dat.Id+"."+GetModelId.Format)) + + _, err = c.Db.Exec("delete from model_data_point where id=$1;", dat.Id) + if err != nil { + return c.E500M("Failed to remove datapoint from database", err) + } + + return c.SendJSON("ok") +} + func handleDataUpload(handle *Handle) { handle.Post("/models/data/upload", func(c *Context) *Error { if !c.CheckAuthLevel(1) { @@ -501,17 +530,14 @@ func handleDataUpload(handle *Handle) { return c.SendJSON(model.Id) }) + handle.Delete("/models/data/point", handleRemoveDataPoint) + handle.Delete("/models/data/delete-zip-file", func(c *Context) *Error { if !c.CheckAuthLevel(1) { return nil } - type ModelData struct { - Id string `json:"id"` - } - - var dat ModelData - + var dat JustId if err := c.ToJSON(&dat); err != nil { return err } diff --git a/logic/utils/handler.go b/logic/utils/handler.go index 1713dd7..a10a719 100644 --- a/logic/utils/handler.go +++ b/logic/utils/handler.go @@ -95,6 +95,16 @@ func (x *Handle) Delete(path string, fn func(c *Context) *Error) { x.deletes = append(x.deletes, HandleFunc{path, fn}) } +func (x *Handle) DeleteAuth(path string, authLevel int, fn func(c *Context) *Error) { + inner_fn := func(c *Context) *Error { + if !c.CheckAuthLevel(authLevel) { + return nil + } + return fn(c) + } + x.posts = append(x.posts, HandleFunc{path, inner_fn}) +} + func (x *Handle) handleGets(context *Context) { defer func() { if r := recover(); r != nil { diff --git a/logic/utils/utils.go b/logic/utils/utils.go index 83ba440..7ced1b7 100644 --- a/logic/utils/utils.go +++ b/logic/utils/utils.go @@ -204,7 +204,7 @@ func MyParseForm(r *http.Request) (vs url.Values, err error) { return } -type JustId struct{ Id string } +type JustId struct{ Id string `json:"id" validate:"required"` } type Generic struct{ reflect.Type } diff --git a/webpage/src/routes/models/edit/ModelTable.svelte b/webpage/src/routes/models/edit/ModelTable.svelte index e831dde..2ad0fdd 100644 --- a/webpage/src/routes/models/edit/ModelTable.svelte +++ b/webpage/src/routes/models/edit/ModelTable.svelte @@ -10,7 +10,7 @@ {#if classes.length == 0} @@ -219,6 +229,9 @@ + + + @@ -258,6 +271,11 @@ {/if} + + + {/each}