feat: add button to remove image from class

This commit is contained in:
Andre Henriques 2024-04-13 00:38:36 +01:00
parent eb20c1b0ac
commit 3f828ab855
4 changed files with 62 additions and 8 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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 }

View File

@ -10,7 +10,7 @@
<script lang="ts">
import Tabs from 'src/lib/Tabs.svelte';
import type { Class } from './ModelData.svelte';
import { get, postFormData } from 'src/lib/requests.svelte';
import { get, postFormData, rdelete } from 'src/lib/requests.svelte';
import type { Model } from './+page.svelte';
import FileUpload from 'src/lib/FileUpload.svelte';
import MessageSimple from 'src/lib/MessageSimple.svelte';
@ -88,6 +88,16 @@
uploading = Promise.resolve();
}
function deleteDataPoint(id: string) {
try {
rdelete('models/data/point', { id })
getList()
} catch (e) {
console.error('TODO notify user', e);
}
}
</script>
{#if classes.length == 0}
@ -219,6 +229,9 @@
<th>
<!-- Status -->
</th>
<th>
<!-- Remove -->
</th>
</tr>
</thead>
<tbody>
@ -258,6 +271,11 @@
<span class="bi bi-exclamation-triangle-fill" style="color: red"></span>
{/if}
</td>
<td style="width: 3ch">
<button class="danger" on:click={() => deleteDataPoint(image.id)}>
<span class="bi bi-trash"></span>
</button>
</td>
</tr>
{/each}
</tbody>