diff --git a/logic/db_types/user.go b/logic/db_types/user.go index 44ed861..89a4785 100644 --- a/logic/db_types/user.go +++ b/logic/db_types/user.go @@ -15,22 +15,22 @@ type User struct { var ErrUserNotFound = errors.New("User Not found") func UserFromToken(db *sql.DB, token string) (*User, error) { - row, err := db.Query("select users.id, users.username, users.email, users.user_type from users inner join tokens on tokens.user_id = users.id where tokens.token = $1;", token) - + rows, err := db.Query("select users.id, users.username, users.email, users.user_type from users inner join tokens on tokens.user_id = users.id where tokens.token = $1;", token) if err != nil { return nil, err } + defer rows.Close() var id string var username string var email string var user_type int - if !row.Next() { + if !rows.Next() { return nil, ErrUserNotFound } - err = row.Scan(&id, &username, &email, &user_type) + err = rows.Scan(&id, &username, &email, &user_type) if err != nil { return nil, err } diff --git a/logic/models/classes/list.go b/logic/models/classes/list.go new file mode 100644 index 0000000..7dd2082 --- /dev/null +++ b/logic/models/classes/list.go @@ -0,0 +1,101 @@ +package model_classes + +import ( + "fmt" + "net/http" + "strconv" + + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" +) + +func HandleList(handle *Handle) { + handle.Get("/models/data/list", func(w http.ResponseWriter, r *http.Request, c *Context) *Error { + if !CheckAuthLevel(1, w, r, c) { + return nil + } + if c.Mode == JSON { + panic("TODO JSON on /models/data/list") + } + + id, err := GetIdFromUrl(r, "id") + if err != nil { + return ErrorCode(err, 400, c.AddMap(nil)) + } + + page := 0 + if r.URL.Query().Has("page") { + page_url := r.URL.Query().Get("page") + page_url_number, err := strconv.Atoi(page_url) + if err != nil { + return ErrorCode(err, http.StatusBadRequest, c.AddMap(nil)) + } + page = page_url_number + } + + class_rows, err := handle.Db.Query("select name, model_id from model_classes where id=$1;", id) + if err != nil { + return Error500(err) + } + defer class_rows.Close() + + if !class_rows.Next() { + return ErrorCode(nil, 404, c.AddMap(nil)) + } + + name := "" + model_id := "" + if err = class_rows.Scan(&name, &model_id); err != nil { + return Error500(nil) + } + + rows, err := handle.Db.Query("select id, file_path, model_mode from model_data_point where class_id=$1 limit 10 offset $2;", id, page * 10) + if err != nil { + return Error500(err) + } + defer rows.Close() + + type baserow struct { + Id string + FilePath string + Mode int + } + + got := []baserow{} + + for rows.Next() { + nrow := baserow{} + err = rows.Scan(&nrow.Id, &nrow.FilePath, &nrow.Mode) + if err != nil { + return Error500(err) + } + got = append(got, nrow) + } + + rows_count, err := handle.Db.Query("select count(*) from model_data_point where class_id=$1;", id) + if err != nil { + return Error500(err) + } + defer rows_count.Close() + + if !rows_count.Next() { + fmt.Printf("select count(*) from model_data_point where class_id='%s';\n", id) + return Error500(nil) + } + + count := 0 + err = rows_count.Scan(&count) + if err != nil { + return Error500(err) + } + + LoadDefineTemplate(w, "/models/edit.html", "data-model-create-class-table-table", c.AddMap(AnyMap{ + "List": got, + "Count": count, + "Page": page, + "Id": id, + "Name": name, + "ModelId": model_id, + })) + return nil + }) +} diff --git a/logic/models/data.go b/logic/models/data.go index eee71d4..b7c963a 100644 --- a/logic/models/data.go +++ b/logic/models/data.go @@ -122,6 +122,8 @@ func processZipFile(handle *Handle, id string) { file_data, err := io.ReadAll(data) f.Write(file_data) + // TODO check if the file is a valid photo that matched the defined photo on the database + parts := strings.Split(file.Name, "/") mode := model_classes.DATA_POINT_MODE_TRAINING diff --git a/logic/models/delete.go b/logic/models/delete.go index 4d72673..3e19d46 100644 --- a/logic/models/delete.go +++ b/logic/models/delete.go @@ -92,7 +92,7 @@ func handleDelete(handle *Handle) { name := f.Get("name") if name != model.Name { - LoadError(w, "/models/edit.html", "delete-model-card", c.AddMap(AnyMap{ + LoadDefineTemplate(w, "/models/edit.html", "delete-model-card", c.AddMap(AnyMap{ "NameDoesNotMatch": true, "Model": model, })) diff --git a/logic/models/index.go b/logic/models/index.go index 728a626..f62542f 100644 --- a/logic/models/index.go +++ b/logic/models/index.go @@ -1,7 +1,8 @@ package models import ( - . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" + model_classes "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/classes" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" ) func HandleModels (handle *Handle) { @@ -12,5 +13,7 @@ func HandleModels (handle *Handle) { // Data endpoints handleDataUpload(handle) + + model_classes.HandleList(handle) } diff --git a/logic/utils/handler.go b/logic/utils/handler.go index 2e0bd75..411bfa9 100644 --- a/logic/utils/handler.go +++ b/logic/utils/handler.go @@ -16,8 +16,22 @@ import ( dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" ) +func Mul (n1 int, n2 int) int { + return n1 * n2 +} + +func Add (n1 int, n2 int) int { + return n1 + n2 +} + func baseLoadTemplate(base string, path string) (*template.Template, any) { - return template.New(base).ParseFiles( + funcs := map[string]any { + "startsWith": strings.HasPrefix, + "replace": strings.Replace, + "mul": Mul, + "add": Add, + } + return template.New(base).Funcs(funcs).ParseFiles( "./views/"+base, "./views/"+path, "./views/partials/header.html", @@ -82,7 +96,7 @@ func LoadHtml(writer http.ResponseWriter, path string, data interface{}) { } } -func LoadError(writer http.ResponseWriter, path string, base string, data AnyMap) { +func LoadDefineTemplate(writer http.ResponseWriter, path string, base string, data AnyMap) { if data == nil { data = map[string]interface{} { "Error": true, @@ -91,7 +105,14 @@ func LoadError(writer http.ResponseWriter, path string, base string, data AnyMap data["Error"] = true } - tmpl, err := template.New("").Parse("{{template \"" + base + "\" . }}") + funcs := map[string]any { + "startsWith": strings.HasPrefix, + "mul": Mul, + "replace": strings.Replace, + "add": Add, + } + + tmpl, err := template.New("").Funcs(funcs).Parse("{{template \"" + base + "\" . }}") if err != nil { panic("Lol") } diff --git a/views/models/edit.html b/views/models/edit.html index 836fbb7..a792ef8 100644 --- a/views/models/edit.html +++ b/views/models/edit.html @@ -22,7 +22,7 @@ {{ end }} {{ define "delete-model-card" }} -
{{ end }} +{{/* Is called from a diffrent endpoint so that it does not matter where this is from :) which means that . can mean what ever I want */}} +{{ define "data-model-create-class-table-table" }} ++ File Path + | ++ Mode + | ++ + | +
---|---|---|
+ {{.FilePath}} + | ++ {{ if (eq .Mode 2) }} + Testing + {{ else }} + Training + {{ end }} + | ++ {{ if startsWith .FilePath "file://" }} + + {{ else }} + TODO + img {{ .FilePath }} + {{ end }} + | +