From 712c650d76f4607e50f58721b0a7fc050971cb75 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 13 Oct 2023 15:39:45 +0300 Subject: [PATCH] feat: first partial, for showing all users in the admin view --- components/adminpartials.templ | 15 +++++++++ components/adminpartials_templ.go | 55 +++++++++++++++++++++++++++++++ components/manageadmin.templ | 2 ++ components/manageadmin_templ.go | 2 +- db/users.go | 24 ++++++++++++++ felu.go | 14 +++----- handlers/admin.go | 32 ++++++++++++++++++ handlers/adminpartials.go | 28 ++++++++++++++++ 8 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 components/adminpartials.templ create mode 100644 components/adminpartials_templ.go create mode 100644 handlers/admin.go create mode 100644 handlers/adminpartials.go diff --git a/components/adminpartials.templ b/components/adminpartials.templ new file mode 100644 index 0000000..4ae06d9 --- /dev/null +++ b/components/adminpartials.templ @@ -0,0 +1,15 @@ +package components + +import "git.src.quest/~skye/felu-ddns/db" + +templ AdminPartialsUsersList(users []db.User) { +
+ for _, user := range users { +
+
+ { user.Email } +
+
+ } +
+} diff --git a/components/adminpartials_templ.go b/components/adminpartials_templ.go new file mode 100644 index 0000000..0514235 --- /dev/null +++ b/components/adminpartials_templ.go @@ -0,0 +1,55 @@ +// Code generated by templ@(devel) DO NOT EDIT. + +package components + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import "context" +import "io" +import "bytes" + +import "git.src.quest/~skye/felu-ddns/db" + +func AdminPartialsUsersList(users []db.User) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) { + templBuffer, templIsBuffer := w.(*bytes.Buffer) + if !templIsBuffer { + templBuffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templBuffer) + } + ctx = templ.InitializeContext(ctx) + var_1 := templ.GetChildren(ctx) + if var_1 == nil { + var_1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, err = templBuffer.WriteString("
") + if err != nil { + return err + } + for _, user := range users { + _, err = templBuffer.WriteString("
") + if err != nil { + return err + } + var var_2 string = user.Email + _, err = templBuffer.WriteString(templ.EscapeString(var_2)) + if err != nil { + return err + } + _, err = templBuffer.WriteString("
") + if err != nil { + return err + } + } + _, err = templBuffer.WriteString("
") + if err != nil { + return err + } + if !templIsBuffer { + _, err = templBuffer.WriteTo(w) + } + return err + }) +} diff --git a/components/manageadmin.templ b/components/manageadmin.templ index f13d1d0..642cb36 100644 --- a/components/manageadmin.templ +++ b/components/manageadmin.templ @@ -14,6 +14,8 @@ templ ManageAdminUsers() {
list of users here, with also the ability to add new users
+
+
} } diff --git a/components/manageadmin_templ.go b/components/manageadmin_templ.go index cc0d95a..ecf26f7 100644 --- a/components/manageadmin_templ.go +++ b/components/manageadmin_templ.go @@ -94,7 +94,7 @@ func ManageAdminUsers() templ.Component { if err != nil { return err } - _, err = templBuffer.WriteString("") + _, err = templBuffer.WriteString("
") if err != nil { return err } diff --git a/db/users.go b/db/users.go index 4f0129b..a2ab3d9 100644 --- a/db/users.go +++ b/db/users.go @@ -92,3 +92,27 @@ func FetchUserWithUlid(ulid string) (*User, error) { return &user, nil } + +func FetchAllUsers() ([]User, error) { + rows, err := DBConn.Query(`SELECT ulid, email, is_admin FROM users`) + if err != nil { + return nil, err + } + defer rows.Close() + + var users []User + for rows.Next() { + var user User + err = rows.Scan(&user.Ulid, &user.Email, &user.IsAdmin) + if err != nil { + return nil, err + } + users = append(users, user) + } + err = rows.Err() + if err != nil { + return nil, err + } + + return users, nil +} diff --git a/felu.go b/felu.go index 82e55d5..4867e2a 100644 --- a/felu.go +++ b/felu.go @@ -93,15 +93,11 @@ func setupFrontendRouter() *gin.Engine { middlewares.AdminOnly(), ) { - manageAdmin.GET("/", func(c *gin.Context) { - c.HTML(http.StatusOK, "", components.ManageAdmin()) - }) - manageAdmin.GET("/users", func(c *gin.Context) { - c.HTML(http.StatusOK, "", components.ManageAdminUsers()) - }) - manageAdmin.GET("/domains", func(c *gin.Context) { - c.HTML(http.StatusOK, "", components.ManageAdminDomains()) - }) + manageAdmin.GET("/", handlers.ManageAdmin()) + manageAdmin.GET("/users", handlers.ManageAdminUsers()) + manageAdmin.GET("/domains", handlers.ManageAdminDomains()) + + manageAdmin.GET("/partials/users_list", handlers.AdminPartialsUsersList()) } return r diff --git a/handlers/admin.go b/handlers/admin.go new file mode 100644 index 0000000..6e70230 --- /dev/null +++ b/handlers/admin.go @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Jonni Liljamo + * + * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for + * more information. + */ +package handlers + +import ( + "net/http" + + "git.src.quest/~skye/felu-ddns/components" + "github.com/gin-gonic/gin" +) + +func ManageAdmin() gin.HandlerFunc { + return func(c *gin.Context) { + c.HTML(http.StatusOK, "", components.ManageAdmin()) + } +} + +func ManageAdminUsers() gin.HandlerFunc { + return func(c *gin.Context) { + c.HTML(http.StatusOK, "", components.ManageAdminUsers()) + } +} + +func ManageAdminDomains() gin.HandlerFunc { + return func(c *gin.Context) { + c.HTML(http.StatusOK, "", components.ManageAdminDomains()) + } +} diff --git a/handlers/adminpartials.go b/handlers/adminpartials.go new file mode 100644 index 0000000..ecbe62c --- /dev/null +++ b/handlers/adminpartials.go @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2023 Jonni Liljamo + * + * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for + * more information. + */ +package handlers + +import ( + "net/http" + + "git.src.quest/~skye/felu-ddns/components" + "git.src.quest/~skye/felu-ddns/db" + "github.com/gin-gonic/gin" +) + +func AdminPartialsUsersList() gin.HandlerFunc { + return func(c *gin.Context) { + users, err := db.FetchAllUsers() + if err != nil { + // TODO: Handle this better + c.String(http.StatusInternalServerError, "Something went wrong while fetching users") + c.Abort() + } else { + c.HTML(http.StatusOK, "", components.AdminPartialsUsersList(users)) + } + } +} -- 2.44.1