2024-04-24 00:34:57 +00:00
|
|
|
package httpserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
echo "github.com/labstack/echo/v4"
|
|
|
|
|
|
|
|
"git.janky.solutions/finn/lockserver/db"
|
|
|
|
"git.janky.solutions/finn/lockserver/frontend"
|
|
|
|
)
|
|
|
|
|
|
|
|
type indexTemplateData struct {
|
|
|
|
baseTemplateData
|
|
|
|
|
2024-11-23 03:50:18 +00:00
|
|
|
ActiveCodes []db.IssuedCode
|
|
|
|
Locks []db.Lock
|
2024-04-25 19:38:24 +00:00
|
|
|
}
|
2024-04-25 19:06:20 +00:00
|
|
|
|
2024-04-25 19:38:24 +00:00
|
|
|
func indexHandler(c echo.Context) error {
|
2024-04-24 00:34:57 +00:00
|
|
|
queries, dbc, err := db.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dbc.Close()
|
|
|
|
|
2024-11-23 03:50:18 +00:00
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
|
|
|
activeCodes, err := queries.GetActiveCodes(ctx)
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
locks, err := queries.GetLocks(ctx)
|
2024-04-24 00:34:57 +00:00
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-25 19:38:24 +00:00
|
|
|
return frontend.Templates.ExecuteTemplate(c.Response(), "index.html", indexTemplateData{
|
|
|
|
baseTemplateData: getBaseTemplateData(c.Request().Header),
|
2024-11-23 03:50:18 +00:00
|
|
|
ActiveCodes: activeCodes,
|
2024-04-25 19:38:24 +00:00
|
|
|
Locks: locks,
|
|
|
|
})
|
2024-04-24 00:34:57 +00:00
|
|
|
}
|