2023-09-21 16:43:11 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-03-01 23:03:25 +00:00
|
|
|
"git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
2023-09-21 16:43:11 +01:00
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleList(handle *Handle) {
|
2024-03-01 23:03:25 +00:00
|
|
|
handle.Get("/models", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
2023-09-21 16:43:11 +01:00
|
|
|
if !CheckAuthLevel(1, w, r, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-01 23:03:25 +00:00
|
|
|
// TODO handle admin
|
|
|
|
if c.Mode == JSON {
|
|
|
|
rows, err := handle.Db.Query("select id, name from models where user_id=$1;", c.User.Id)
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
type Row struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Id string `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := utils.GetDbMultitple[Row](c, "models where user_id=$1", c.User.Id);
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Warn("HERE 6")
|
|
|
|
return c.Error500(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendJSON(got)
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:43:11 +01:00
|
|
|
rows, err := handle.Db.Query("select id, name from models where user_id=$1;", c.User.Id)
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
2024-03-01 23:03:25 +00:00
|
|
|
defer rows.Close()
|
2023-09-21 16:43:11 +01:00
|
|
|
|
|
|
|
type row struct {
|
|
|
|
Name string
|
|
|
|
Id string
|
|
|
|
}
|
|
|
|
|
|
|
|
got := []row{}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var r row
|
|
|
|
err = rows.Scan(&r.Id, &r.Name)
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
got = append(got, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadBasedOnAnswer(c.Mode, w, "/models/list.html", c.AddMap(AnyMap{
|
|
|
|
"List": got,
|
|
|
|
}))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|