2023-09-21 16:43:11 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2023-09-22 19:22:36 +01:00
|
|
|
model_classes "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/classes"
|
2023-09-26 20:15:28 +01:00
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
|
2023-09-21 16:43:11 +01:00
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleEdit(handle *Handle) {
|
|
|
|
handle.GetHTML("/models/edit", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
|
|
|
if !CheckAuthLevel(1, w, r, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := GetIdFromUrl(r, "id")
|
|
|
|
if err != nil {
|
|
|
|
return ErrorCode(nil, http.StatusNotFound, AnyMap{
|
|
|
|
"NotFoundMessage": "Model not found",
|
|
|
|
"GoBackLink": "/models",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO handle admin users
|
|
|
|
rows, err := handle.Db.Query("select name, status, width, height, color_mode from models where id=$1 and user_id=$2;", id, c.User.Id)
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
2023-09-22 19:22:36 +01:00
|
|
|
defer rows.Close()
|
2023-09-21 16:43:11 +01:00
|
|
|
|
|
|
|
if !rows.Next() {
|
|
|
|
return ErrorCode(nil, http.StatusNotFound, AnyMap{
|
|
|
|
"NotFoundMessage": "Model not found",
|
|
|
|
"GoBackLink": "/models",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type rowmodel struct {
|
|
|
|
Name string
|
|
|
|
Status int
|
|
|
|
Id string
|
|
|
|
Width *int
|
|
|
|
Height *int
|
|
|
|
Color_mode *string
|
|
|
|
}
|
|
|
|
|
|
|
|
var model rowmodel = rowmodel{}
|
|
|
|
model.Id = id
|
|
|
|
|
|
|
|
err = rows.Scan(&model.Name, &model.Status, &model.Width, &model.Height, &model.Color_mode)
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle errors
|
|
|
|
// All errors will be negative
|
|
|
|
if model.Status < 0 {
|
|
|
|
LoadBasedOnAnswer(c.Mode, w, "/models/edit.html", c.AddMap(AnyMap{
|
|
|
|
"Model": model,
|
|
|
|
}))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch model.Status {
|
|
|
|
case PREPARING:
|
|
|
|
LoadBasedOnAnswer(c.Mode, w, "/models/edit.html", c.AddMap(AnyMap{
|
|
|
|
"Model": model,
|
|
|
|
}))
|
|
|
|
case CONFIRM_PRE_TRAINING:
|
2023-09-22 19:22:36 +01:00
|
|
|
|
|
|
|
cls, err := model_classes.ListClasses(handle.Db, id)
|
2023-09-26 20:15:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
has_data, err := model_classes.ModelHasDataPoints(handle.Db, id)
|
2023-09-22 19:22:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
LoadBasedOnAnswer(c.Mode, w, "/models/edit.html", c.AddMap(AnyMap{
|
|
|
|
"Model": model,
|
2023-09-22 19:22:36 +01:00
|
|
|
"Classes": cls,
|
2023-09-26 20:15:28 +01:00
|
|
|
"HasData": has_data,
|
2023-09-21 16:43:11 +01:00
|
|
|
}))
|
2023-09-26 20:15:28 +01:00
|
|
|
case TRAINING:
|
|
|
|
fallthrough
|
2023-09-22 19:22:36 +01:00
|
|
|
case PREPARING_ZIP_FILE:
|
|
|
|
LoadBasedOnAnswer(c.Mode, w, "/models/edit.html", c.AddMap(AnyMap{
|
|
|
|
"Model": model,
|
|
|
|
}))
|
2023-09-21 16:43:11 +01:00
|
|
|
default:
|
|
|
|
fmt.Printf("Unkown Status: %d\n", model.Status)
|
|
|
|
return Error500(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|