fyp/logic/models/delete.go

89 lines
2.0 KiB
Go
Raw Normal View History

package models
import (
"net/http"
"os"
"path"
2024-04-14 14:51:16 +01:00
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
func deleteModelJSON(c *Context, id string) *Error {
c.Logger.Warnf("Removing model with id: %s", id)
_, err := c.Db.Exec("delete from models where id=$1;", id)
if err != nil {
return c.Error500(err)
}
model_path := path.Join("./savedData", id)
c.Logger.Warnf("Removing folder of model with id: %s at %s", id, model_path)
err = os.RemoveAll(model_path)
if err != nil {
return c.Error500(err)
}
return c.SendJSON(id)
}
func handleDelete(handle *Handle) {
2024-03-09 10:52:08 +00:00
handle.Delete("/models/delete", func(c *Context) *Error {
2024-04-08 14:17:13 +01:00
if !c.CheckAuthLevel(1) {
2024-03-09 10:52:08 +00:00
return nil
}
2024-03-09 10:52:08 +00:00
var dat struct {
Id string `json:"id" validate:"required"`
Name *string `json:"name,omitempty"`
}
2024-03-09 10:52:08 +00:00
if err_ := c.ToJSON(&dat); err_ != nil {
return err_
}
2024-03-09 10:52:08 +00:00
var model struct {
Id string
Name string
Status int
}
2024-04-14 14:51:16 +01:00
err := GetDBOnce(c, &model, "models where id=$1 and user_id=$2;", dat.Id, c.User.Id)
2024-03-09 10:52:08 +00:00
if err == NotFoundError {
return c.SendJSONStatus(http.StatusNotFound, "Model not found!")
} else if err != nil {
2023-10-06 10:46:45 +01:00
return c.Error500(err)
}
switch model.Status {
case FAILED_TRAINING:
fallthrough
case FAILED_PREPARING_ZIP_FILE:
fallthrough
case FAILED_PREPARING_TRAINING:
fallthrough
case FAILED_PREPARING:
2024-03-09 10:52:08 +00:00
return deleteModelJSON(c, dat.Id)
case READY:
fallthrough
2024-04-08 14:17:13 +01:00
case READY_RETRAIN_FAILED:
fallthrough
case READY_ALTERATION_FAILED:
fallthrough
case CONFIRM_PRE_TRAINING:
2024-03-09 10:52:08 +00:00
if dat.Name == nil {
return c.JsonBadRequest("Provided name does not match the model name")
}
2024-03-09 10:52:08 +00:00
if *dat.Name != model.Name {
return c.JsonBadRequest("Provided name does not match the model name")
}
2024-03-09 10:52:08 +00:00
return deleteModelJSON(c, dat.Id)
default:
2024-03-09 10:52:08 +00:00
c.Logger.Warn("Do not know how to handle model in status", "status", model.Status)
return c.JsonBadRequest("Model in invalid status")
}
})
}