Initial commit
This commit is contained in:
71
cpone-dashboard/menu/projects/handler.go
Normal file
71
cpone-dashboard/menu/projects/handler.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"cpone-dashboard/menu/auth"
|
||||
"embed"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var tmplFS *embed.FS
|
||||
var basePath string
|
||||
|
||||
func SetTemplateFS(fs *embed.FS) { tmplFS = fs }
|
||||
func SetBasePath(p string) { basePath = p }
|
||||
|
||||
type pageData struct {
|
||||
Username string
|
||||
Projects []ProjectItem
|
||||
CurrentProject *ProjectItem
|
||||
}
|
||||
|
||||
func Index(w http.ResponseWriter, r *http.Request) {
|
||||
username := auth.Username(r)
|
||||
selectedID := auth.SelectedProjectID(r)
|
||||
|
||||
items, err := GetUserProjects(username)
|
||||
if err != nil {
|
||||
log.Printf("[projects] GetUserProjects error: %v", err)
|
||||
http.Error(w, "query error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
b := func(path string) string { return basePath + path }
|
||||
t := template.Must(template.New("").Funcs(template.FuncMap{"b": b}).ParseFS(tmplFS, "templates/projects/index.html"))
|
||||
var current *ProjectItem
|
||||
if selectedID > 0 {
|
||||
if item, ok, _ := GetUserProject(username, selectedID); ok {
|
||||
current = &item
|
||||
}
|
||||
}
|
||||
if err := t.ExecuteTemplate(w, "projects", pageData{
|
||||
Username: username,
|
||||
Projects: items,
|
||||
CurrentProject: current,
|
||||
}); err != nil {
|
||||
log.Printf("[projects] template error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func Select(w http.ResponseWriter, r *http.Request) {
|
||||
username := auth.Username(r)
|
||||
mcuID, _ := strconv.Atoi(r.URL.Query().Get("mcu_id"))
|
||||
if mcuID <= 0 {
|
||||
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok, err := GetUserProject(username, mcuID); err != nil {
|
||||
log.Printf("[projects] GetUserProject error: %v", err)
|
||||
http.Error(w, "query error", http.StatusInternalServerError)
|
||||
return
|
||||
} else if !ok {
|
||||
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
auth.SetSelectedProject(w, mcuID)
|
||||
http.Redirect(w, r, basePath+"/dashboard", http.StatusSeeOther)
|
||||
}
|
||||
Reference in New Issue
Block a user