From 7d742e7970b84a572c9d7abbee5ffba477d16be7 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Tue, 16 Apr 2024 10:37:18 +0100 Subject: [PATCH] add button to create new class without images closes #14 --- logic/models/data.go | 39 +++++ webpage/src/lib/KeepPageSize.svelte | 11 ++ webpage/src/lib/requests.svelte.ts | 16 ++ webpage/src/routes/models/edit/+page.svelte | 8 +- .../src/routes/models/edit/ModelTable.svelte | 147 ++++++++++-------- .../routes/models/edit/ZipStructure.svelte | 26 ++++ 6 files changed, 180 insertions(+), 67 deletions(-) create mode 100644 webpage/src/lib/KeepPageSize.svelte create mode 100644 webpage/src/routes/models/edit/ZipStructure.svelte diff --git a/logic/models/data.go b/logic/models/data.go index 0cf7729..09aa640 100644 --- a/logic/models/data.go +++ b/logic/models/data.go @@ -459,6 +459,45 @@ func handleDataUpload(handle *Handle) { return c.SendJSON(model.Id) }) + // Create New class + type CreateNewEmptyClass struct { + Id string `json:"id" validate:"required"` + Name string `json:"name" validate:"required"` + } + PostAuthJson(handle, "/models/data/class/new", User_Normal, func(c *Context, obj *CreateNewEmptyClass) *Error { + model, err := GetBaseModel(c.Db, obj.Id) + if err == ModelNotFoundError { + return c.JsonBadRequest("Model not found") + } + + if model.ModelType != 2 && model.Status != CONFIRM_PRE_TRAINING || model.ModelType == 2 && model.Status != CONFIRM_PRE_TRAINING && model.Status != READY { + return c.JsonBadRequest("Model not in the correct status for adding new class") + } + + var baseOrder struct { + Order int `db:"class_order"` + } + err = GetDBOnce(c, &baseOrder, "model_classes where model_id=$1 order by class_order desc;", model.Id) + if err != nil { + return c.E500M("Could not create class", err) + } + + id, err := model_classes.CreateClass(c.Db, model.Id, baseOrder.Order+1, obj.Name) + if err == model_classes.ClassAlreadyExists { + return c.JsonBadRequest("Class Already exists") + } else if err != nil { + return c.E500M("Could not create class", err) + } + + var modelClass model_classes.ModelClass + err = GetDBOnce(c, &modelClass, "model_classes where id=$1;", id) + if err != nil { + return c.E500M("Failed to get class information but class was creted", err) + } + + return c.SendJSON(modelClass) + }) + // ------ // ------ CLASS DATA UPLOAD // ------ diff --git a/webpage/src/lib/KeepPageSize.svelte b/webpage/src/lib/KeepPageSize.svelte new file mode 100644 index 0000000..9260b21 --- /dev/null +++ b/webpage/src/lib/KeepPageSize.svelte @@ -0,0 +1,11 @@ + + + diff --git a/webpage/src/lib/requests.svelte.ts b/webpage/src/lib/requests.svelte.ts index 2f98462..5545f99 100644 --- a/webpage/src/lib/requests.svelte.ts +++ b/webpage/src/lib/requests.svelte.ts @@ -99,3 +99,19 @@ export async function postFormData(url: string, body: FormData) { return r.json(); } + +export async function showMessage( + e: any, + messages: any, + message = 'Could not complete request' +): Promise { + if (e == null) { + return false; + } else if (e instanceof Response) { + messages.display(await e.json()); + return true; + } else { + messages.display(message); + return true; + } +} diff --git a/webpage/src/routes/models/edit/+page.svelte b/webpage/src/routes/models/edit/+page.svelte index 19cf0a0..2e09629 100644 --- a/webpage/src/routes/models/edit/+page.svelte +++ b/webpage/src/routes/models/edit/+page.svelte @@ -42,6 +42,7 @@ import ModelDataPage from './ModelDataPage.svelte'; import 'src/styles/forms.css'; + import KeepPageSize from 'src/lib/KeepPageSize.svelte'; let model: Promise = $state(new Promise(() => {})); let _model: Model | undefined = $state(undefined); @@ -87,7 +88,6 @@ }); let resetMessages: MessageSimple; - async function resetModel() { resetMessages.display(''); @@ -125,11 +125,7 @@ {/await} - +
diff --git a/webpage/src/routes/models/edit/ModelTable.svelte b/webpage/src/routes/models/edit/ModelTable.svelte index cf6c316..daa76f7 100644 --- a/webpage/src/routes/models/edit/ModelTable.svelte +++ b/webpage/src/routes/models/edit/ModelTable.svelte @@ -10,17 +10,22 @@