cml04-falcon-ui/handlers/index.go

70 lines
2.0 KiB
Go
Raw Normal View History

2024-10-04 20:02:51 +02:00
package handlers
import (
"net/http"
"text/template"
2024-10-09 16:02:06 +02:00
"time"
2024-10-04 20:02:51 +02:00
2024-10-09 16:02:06 +02:00
"git.espin.casa/albert/cml04-falcon-ui/helper"
2024-10-06 18:33:18 +02:00
"git.espin.casa/albert/cml04-falcon-ui/storage"
2024-10-09 16:02:06 +02:00
"git.espin.casa/albert/cml04-falcon-ui/types"
2024-10-04 20:02:51 +02:00
"github.com/julienschmidt/httprouter"
)
2024-10-06 18:33:18 +02:00
func IndexHandler(storage storage.Storager) httprouter.Handle {
2024-10-09 16:02:06 +02:00
type PageView struct {
2024-10-23 10:06:35 +02:00
Stats *types.Stats
BundlesATA12 []types.BundleData
BundlesATA345 []types.BundleData
BarcodesATA12 []types.Barcode
BarcodesATA345 []types.Barcode
2024-10-09 16:02:06 +02:00
}
2024-10-04 20:02:51 +02:00
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
2024-10-09 16:02:06 +02:00
// get current shift dates
dates, err := helper.GetShiftDates(time.Now())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-10-23 10:06:35 +02:00
// get ata12 shift bundles
2024-10-10 12:14:21 +02:00
bundles12, err := storage.ShiftListBundle(r.Context(), dates[0], dates[1], types.ATA12)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-10-23 10:06:35 +02:00
// get ata345 shift bundles
2024-10-10 12:14:21 +02:00
bundles345, err := storage.ShiftListBundle(r.Context(), dates[0], dates[1], types.ATA345)
2024-10-09 16:02:06 +02:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-10-23 10:06:35 +02:00
// get ata12 shift barcodes
barcodesATA12, err := storage.ShiftListBarcode(r.Context(), dates[0], dates[1], types.ATA12)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// get ata345 shift barcodes
barcodesATA345, err := storage.ShiftListBarcode(r.Context(), dates[0], dates[1], types.ATA345)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-10-09 16:02:06 +02:00
// create view
view := &PageView{
2024-10-23 10:06:35 +02:00
Stats: &types.Stats{},
BundlesATA12: bundles12,
BundlesATA345: bundles345,
BarcodesATA12: barcodesATA12,
BarcodesATA345: barcodesATA345,
2024-10-09 16:02:06 +02:00
}
2024-10-05 18:43:25 +02:00
t, _ := template.ParseFiles("templates/base.html", "templates/index.html")
2024-10-09 16:02:06 +02:00
err = t.Execute(w, view)
2024-10-04 20:02:51 +02:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}