Files
cpone_dashboard/cpone-dashboard/menu/result/handler.go
2026-04-30 14:27:01 +07:00

75 lines
1.8 KiB
Go

package result
import (
"cpone-dashboard/menu/auth"
"cpone-dashboard/menu/projects"
"html/template"
"net/http"
)
var tmpl *template.Template
var pdfBaseURL string
var basePath string
func SetTemplates(t *template.Template) { tmpl = t }
func SetPDFBaseURL(u string) { pdfBaseURL = u }
func SetBasePath(p string) { basePath = p }
type pageData struct {
Username string
CurrentProject projects.ProjectItem
Search string
Filter string
Rows []ResultRow
FilteredRows []ResultRow
Summary ResultSummary
PDFBaseURL string
}
func Index(w http.ResponseWriter, r *http.Request) {
username := auth.Username(r)
mcuID := auth.SelectedProjectID(r)
if mcuID == 0 {
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
return
}
project, ok, err := projects.GetUserProject(username, mcuID)
if err != nil {
http.Error(w, "query error", http.StatusInternalServerError)
return
}
if !ok {
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
return
}
rows, err := GetResultRows(mcuID)
if err != nil {
http.Error(w, "query error", http.StatusInternalServerError)
return
}
summary := BuildResultSummary(rows)
search := r.URL.Query().Get("search")
filter := r.URL.Query().Get("filter")
filteredRows := FilterResultRows(rows, search, filter)
t := tmpl
if t == nil {
http.Error(w, "template not ready", http.StatusInternalServerError)
return
}
if err := t.ExecuteTemplate(w, "base", pageData{
Username: username,
CurrentProject: project,
Search: search,
Filter: filter,
Rows: rows,
FilteredRows: filteredRows,
Summary: summary,
PDFBaseURL: pdfBaseURL,
}); err != nil {
http.Error(w, "template error", http.StatusInternalServerError)
}
}