aboutsummaryrefslogtreecommitdiff
path: root/internal/webui/stop.go
blob: 68d592e2ad0e25d14e6a4b6527ff3184dcfede88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package webui

import (
	"fmt"
	"html/template"
	"net/http"

	"git.adyxax.org/adyxax/trains/pkg/model"
)

var stopTemplate = template.Must(template.New("stop").Funcs(funcMap).ParseFS(templatesFS, "html/base.html", "html/stop.html"))

// The page template variable
type StopPage struct {
	User  *model.User
	Stops []model.Stop
}

// The stop handler of the webui
func stopHandler(e *env, w http.ResponseWriter, r *http.Request) error {
	if r.URL.Path == "/stop" {
		user, err := tryAndResumeSession(e, r)
		if err != nil {
			http.Redirect(w, r, "/login", http.StatusFound)
			return nil
		}
		switch r.Method {
		case http.MethodGet:
			stops, err := e.dbEnv.GetStops()
			if err != nil {
				return newStatusError(http.StatusInternalServerError, fmt.Errorf("Could not get train stops"))
			} else {
				w.Header().Set("Cache-Control", "no-store, no-cache")
			}
			p := StopPage{
				User:  user,
				Stops: stops,
			}
			err = stopTemplate.ExecuteTemplate(w, "stop.html", p)
			if err != nil {
				return newStatusError(http.StatusInternalServerError, err)
			}
			return nil
		default:
			return newStatusError(http.StatusMethodNotAllowed, fmt.Errorf(http.StatusText(http.StatusMethodNotAllowed)))
		}
	} else {
		return newStatusError(http.StatusNotFound, fmt.Errorf("Invalid path in stopHandler"))
	}
}