From e5c0df6f6f090ffb872d6d7fa0e2b289e0818aa6 Mon Sep 17 00:00:00 2001 From: structix Date: Sun, 11 Oct 2020 22:45:08 +0200 Subject: [PATCH] Add admin page --- database/admin.go | 51 ++++++++++++++++++++++++++++++++ handler/admin.go | 14 +++++++++ handler/mainpage.go | 2 +- html/admin/adminindex.html | 56 ++++++++++++++++++++++++++++++++++++ html/static/navbar.html | 1 + html/static/navbaradmin.html | 10 +++++++ routes/routes.go | 7 +++-- 7 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 database/admin.go create mode 100644 handler/admin.go create mode 100644 html/admin/adminindex.html create mode 100644 html/static/navbaradmin.html diff --git a/database/admin.go b/database/admin.go new file mode 100644 index 0000000..19bc533 --- /dev/null +++ b/database/admin.go @@ -0,0 +1,51 @@ +package database + +import ( + "context" + "github.com/gin-gonic/gin" + "github.com/jackc/pgx/v4/pgxpool" + "log" + "trikotwaschliste/models" +) + +func DbAdminMainpage(pool *pgxpool.Pool) gin.H { + var items []models.Washlist + var persons []models.Person + + witems, err := pool.Query(context.Background(), "SELECT wa.id, pe.name, ga.created_at, wa.washed \nFROM washlist wa\nINNER JOIN persons pe ON wa.personid = pe.id\nINNER JOIN gamedata ga ON wa.gamedataid = ga.id ORDER BY ga.created_at ASC") + if err != nil { + log.Println(err.Error()) + } + + var item models.Washlist + for witems.Next() { + err = witems.Scan(&item.Id, &item.Person, &item.Date, &item.Washed) + if err != nil { + log.Println(err.Error()) + } + item.DateString = item.Date.Format(layoutDE) + items = append(items, item) + } + + // get persons + wperson, err := pool.Query(context.Background(), "SELECT id, name FROM persons ORDER BY name ASC") + if err != nil { + log.Println(err.Error()) + } + + persons = append(persons, models.Person{ + Id: -1, + Name: "-- Auswählen --", + }) + var person models.Person + for wperson.Next() { + err = wperson.Scan(&person.Id, &person.Name) + if err != nil { + log.Println(err.Error()) + } + persons = append(persons, person) + } + + + return gin.H{"items": items, "persons": persons} +} diff --git a/handler/admin.go b/handler/admin.go new file mode 100644 index 0000000..91730ae --- /dev/null +++ b/handler/admin.go @@ -0,0 +1,14 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "github.com/jackc/pgx/v4/pgxpool" + "net/http" + "trikotwaschliste/database" +) + +func AdminMainPage(pool *pgxpool.Pool) gin.HandlerFunc { + return func(c *gin.Context) { + c.HTML(http.StatusAccepted, "adminindex.html", database.DbAdminMainpage(pool)) + } +} diff --git a/handler/mainpage.go b/handler/mainpage.go index 6fbd8ff..a28d5cd 100644 --- a/handler/mainpage.go +++ b/handler/mainpage.go @@ -25,6 +25,6 @@ func UploadName(pool *pgxpool.Pool) gin.HandlerFunc { func Credits() gin.HandlerFunc { return func(c *gin.Context) { - c.HTML(http.StatusAccepted, "credits.html", gin.H{"version":"1.0"}) + c.HTML(http.StatusAccepted, "credits.html", gin.H{"version":"1.1"}) } } diff --git a/html/admin/adminindex.html b/html/admin/adminindex.html new file mode 100644 index 0000000..a26d5c5 --- /dev/null +++ b/html/admin/adminindex.html @@ -0,0 +1,56 @@ + + + + + Trikotwaschliste - Admin + + + + + +{{template "navbaradmin"}} +
+

Trikotwaschliste Admin

+ + + + + + + + + + {{ $items := .items}} + {{ $persons := .persons}} + {{range $item := $items}} + + + + + + {{end}} + +
PersonDatumWaschperson
{{$item.Person}}{{$item.DateString}} +
+
+ + + +
+
+ +
+ +
+ + + \ No newline at end of file diff --git a/html/static/navbar.html b/html/static/navbar.html index 27a5dc7..850ebb3 100644 --- a/html/static/navbar.html +++ b/html/static/navbar.html @@ -5,6 +5,7 @@ {{end}} \ No newline at end of file diff --git a/html/static/navbaradmin.html b/html/static/navbaradmin.html new file mode 100644 index 0000000..dc51231 --- /dev/null +++ b/html/static/navbaradmin.html @@ -0,0 +1,10 @@ +{{define "navbaradmin"}} + +{{end}} \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index 33e11cc..3ec4578 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -15,10 +15,11 @@ func RoutesInit(router *gin.Engine, pool *pgxpool.Pool, config *models.Config) { router.GET("/persons", handler.ShowPersonsList(pool)) // Basic auth accounts - //authorized := router.Group("/admin", gin.BasicAuth(gin.Accounts{ - //})) + authorized := router.Group("/admin", gin.BasicAuth(gin.Accounts{ + config.ADMIN_NAME: config.ADMIN_PASSWORD, + })) // Basic auth routes - + authorized.GET("/", handler.AdminMainPage(pool)) }