79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package models
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
|
|
. "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.E500M("Failed to delete models", 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.E500M("Failed to remove data", err)
|
|
}
|
|
|
|
return c.SendJSON(id)
|
|
}
|
|
|
|
func handleDelete(handle *Handle) {
|
|
type DeleteModel struct {
|
|
Id string `json:"id" validate:"required"`
|
|
Name *string `json:"name,omitempty"`
|
|
}
|
|
DeleteAuthJson(handle, "/models/delete", User_Normal, func(c *Context, dat *DeleteModel) *Error {
|
|
var model struct {
|
|
Id string
|
|
Name string
|
|
Status int
|
|
}
|
|
err := GetDBOnce(c, &model, "models where id=$1 and user_id=$2;", dat.Id, c.User.Id)
|
|
if err == NotFoundError {
|
|
return c.SendJSONStatus(http.StatusNotFound, "Model not found!")
|
|
} else if err != nil {
|
|
return c.E500M("Faield to get model", err)
|
|
}
|
|
|
|
switch model.Status {
|
|
case FAILED_TRAINING:
|
|
fallthrough
|
|
case FAILED_PREPARING_ZIP_FILE:
|
|
fallthrough
|
|
case FAILED_PREPARING_TRAINING:
|
|
fallthrough
|
|
case FAILED_PREPARING:
|
|
return deleteModelJSON(c, dat.Id)
|
|
|
|
case READY:
|
|
fallthrough
|
|
case READY_RETRAIN_FAILED:
|
|
fallthrough
|
|
case READY_ALTERATION_FAILED:
|
|
fallthrough
|
|
case CONFIRM_PRE_TRAINING:
|
|
if dat.Name == nil {
|
|
return c.JsonBadRequest("Provided name does not match the model name")
|
|
}
|
|
|
|
if *dat.Name != model.Name {
|
|
return c.JsonBadRequest("Provided name does not match the model name")
|
|
}
|
|
|
|
return deleteModelJSON(c, dat.Id)
|
|
default:
|
|
c.Logger.Warn("Do not know how to handle model in status", "status", model.Status)
|
|
return c.JsonBadRequest("Model in invalid status")
|
|
}
|
|
})
|
|
}
|