fyp/logic/models/list.go

67 lines
1.4 KiB
Go
Raw Normal View History

package models
import (
"net/http"
"git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
func handleList(handle *Handle) {
handle.Get("/models", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
if !CheckAuthLevel(1, w, r, c) {
return nil
}
// 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)
}
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
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
})
}