Add Task Index Backend
This commit is contained in:
parent
fe0c712447
commit
8ae77f802d
1 changed files with 27 additions and 1 deletions
|
@ -7,12 +7,14 @@ import (
|
|||
|
||||
"git.lastassault.de/speatzle/morffix/constants"
|
||||
"git.lastassault.de/speatzle/morffix/types"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type IndexData struct {
|
||||
Counter []int
|
||||
|
||||
Workers []IndexWorker
|
||||
Tasks []Task
|
||||
}
|
||||
|
||||
type IndexWorker struct {
|
||||
|
@ -22,6 +24,15 @@ type IndexWorker struct {
|
|||
|
||||
var count = 123456789
|
||||
|
||||
type Task struct {
|
||||
ID int `db:"id"`
|
||||
Library int `db:"library"`
|
||||
Worker *string `db:"worker"`
|
||||
Type int `db:"type"`
|
||||
Status int `db:"status"`
|
||||
File string `db:"file"`
|
||||
}
|
||||
|
||||
func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
data := IndexData{
|
||||
|
@ -54,8 +65,23 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
}()
|
||||
|
||||
rows, err := db.Query(r.Context(), "SELECT t.id AS id, l.id AS library, t.worker_id AS worker, t.type AS type, t.status AS status, f.path AS file FROM tasks t INNER JOIN files f ON f.id = t.file_id INNER JOIN libraries l ON l.id = f.library_id")
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Query Tasks", "err", err)
|
||||
http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tasks, err := pgx.CollectRows[Task](rows, pgx.RowToStructByName[Task])
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Executing index Template", "err", err)
|
||||
http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Tasks = tasks
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
err := templates.ExecuteTemplate(&buf, constants.INDEX_TEMPLATE_NAME, data)
|
||||
err = templates.ExecuteTemplate(&buf, constants.INDEX_TEMPLATE_NAME, data)
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Executing index Template", "err", err)
|
||||
http.Error(w, "Error Executing Template: "+err.Error(), http.StatusInternalServerError)
|
||||
|
|
Loading…
Add table
Reference in a new issue