From 4b3417c450824c1094f7c3365aca6d7e10a6582d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Wed, 22 May 2024 11:02:29 +0000 Subject: [PATCH] wip: operators controller --- cmd/web/cmd/root.go | 3 ++ internal/web/adapters/db/operators.go | 12 +++++ internal/web/adapters/handlers/handlers.go | 6 +++ internal/web/components/layout.go | 52 +++---------------- .../operators/list_operators_controller.go | 41 +++++++++++++++ internal/web/ports/handlers.go | 2 + internal/web/ports/operators.go | 13 +++++ internal/web/ports/repo.go | 1 + pkg/resolvers/operators.go | 42 +++++++++++++++ 9 files changed, 128 insertions(+), 44 deletions(-) create mode 100644 internal/web/adapters/db/operators.go create mode 100644 internal/web/controllers/operators/list_operators_controller.go create mode 100644 internal/web/ports/operators.go create mode 100644 pkg/resolvers/operators.go diff --git a/cmd/web/cmd/root.go b/cmd/web/cmd/root.go index ee0a56b6..d7f20d06 100644 --- a/cmd/web/cmd/root.go +++ b/cmd/web/cmd/root.go @@ -113,6 +113,9 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R // Me handler app.Get("/me", handlers.Me()) + // Operators handler + app.Get("/operators", handlers.ListOperators()) + err = app.Listen(s.cfg.Flags.Addr) if err != nil { return err diff --git a/internal/web/adapters/db/operators.go b/internal/web/adapters/db/operators.go new file mode 100644 index 00000000..2eff6657 --- /dev/null +++ b/internal/web/adapters/db/operators.go @@ -0,0 +1,12 @@ +package db + +import ( + "context" + + "github.com/zeiss/typhoon/internal/api/models" +) + +// ListOperators ... +func (db *database) ListOperators(ctx context.Context, pagination *models.Pagination[models.Operator]) error { + return db.conn.WithContext(ctx).Scopes(models.Paginate(&pagination.Rows, pagination, db.conn)).Preload("SigningKeyGroups").Preload("SigningKeyGroups.Key").Preload("Key").Find(&pagination.Rows).Error +} diff --git a/internal/web/adapters/handlers/handlers.go b/internal/web/adapters/handlers/handlers.go index 5bf62934..f5ff9c7a 100644 --- a/internal/web/adapters/handlers/handlers.go +++ b/internal/web/adapters/handlers/handlers.go @@ -6,6 +6,7 @@ import ( "github.com/zeiss/typhoon/internal/web/controllers/dashboard" "github.com/zeiss/typhoon/internal/web/controllers/login" "github.com/zeiss/typhoon/internal/web/controllers/me" + "github.com/zeiss/typhoon/internal/web/controllers/operators" "github.com/zeiss/typhoon/internal/web/ports" "github.com/zeiss/typhoon/pkg/resolvers" ) @@ -35,3 +36,8 @@ func (h *handlers) Dashboard() fiber.Handler { func (h *handlers) Me() fiber.Handler { return htmx.NewHxControllerHandler(me.NewMeController(), htmx.Config{Resolvers: []htmx.ResolveFunc{resolvers.UserByID(h.db)}}) } + +// ListOperators ... +func (h *handlers) ListOperators() fiber.Handler { + return htmx.NewHxControllerHandler(operators.NewListOperatorsController(h.db)) +} diff --git a/internal/web/components/layout.go b/internal/web/components/layout.go index dea5ed30..cb3f4e34 100644 --- a/internal/web/components/layout.go +++ b/internal/web/components/layout.go @@ -5,7 +5,6 @@ import ( htmx "github.com/zeiss/fiber-htmx" "github.com/zeiss/fiber-htmx/components/avatars" "github.com/zeiss/fiber-htmx/components/buttons" - "github.com/zeiss/fiber-htmx/components/cards" "github.com/zeiss/fiber-htmx/components/dividers" "github.com/zeiss/fiber-htmx/components/drawers" "github.com/zeiss/fiber-htmx/components/dropdowns" @@ -165,47 +164,8 @@ func Layout(p LayoutProps, children ...htmx.Node) htmx.Node { ), ), ), - htmx.Div( - htmx.ClassNames{ - "p-8": true, - "grid": true, - "gap-6": true, - "xl:grid-cols-2": true, - }, - cards.CardBordered( - cards.CardProps{ - ClassNames: htmx.ClassNames{ - "shadow-xl": false, - "border": true, - "rounded": true, - }, - }, - cards.Body( - cards.BodyProps{}, - cards.Title( - cards.TitleProps{}, - htmx.Text("Hello, World!"), - ), - htmx.Text("This is a card body."), - ), - ), - cards.Card( - cards.CardProps{ - ClassNames: htmx.ClassNames{ - "shadow-xl": false, - "border": true, - "rounded": true, - }, - }, - cards.Body( - cards.BodyProps{}, - cards.Title( - cards.TitleProps{}, - htmx.Text("Hello, World!"), - ), - htmx.Text("This is a card body."), - ), - ), + htmx.Group( + children..., ), ), ), @@ -236,7 +196,9 @@ func Layout(p LayoutProps, children ...htmx.Node) htmx.Node { menus.MenuItem( menus.MenuItemProps{}, menus.MenuLink( - menus.MenuLinkProps{}, + menus.MenuLinkProps{ + Href: "/", + }, htmx.Text("Dashboard"), ), ), @@ -247,7 +209,9 @@ func Layout(p LayoutProps, children ...htmx.Node) htmx.Node { menus.MenuItem( menus.MenuItemProps{}, menus.MenuLink( - menus.MenuLinkProps{}, + menus.MenuLinkProps{ + Href: "/operators", + }, htmx.Text("Operators"), ), ), diff --git a/internal/web/controllers/operators/list_operators_controller.go b/internal/web/controllers/operators/list_operators_controller.go new file mode 100644 index 00000000..2348ff1a --- /dev/null +++ b/internal/web/controllers/operators/list_operators_controller.go @@ -0,0 +1,41 @@ +package operators + +import ( + "fmt" + + htmx "github.com/zeiss/fiber-htmx" + "github.com/zeiss/typhoon/internal/api/models" + "github.com/zeiss/typhoon/internal/web/components" + "github.com/zeiss/typhoon/internal/web/ports" + "github.com/zeiss/typhoon/pkg/resolvers" +) + +var _ = htmx.Controller(&ListOperatorsController{}) + +// ListOperatorsController ... +type ListOperatorsController struct { + htmx.DefaultController +} + +// NewListOperatorsController ... +func NewListOperatorsController(db ports.Operators) *ListOperatorsController { + return &ListOperatorsController{} +} + +// Prepare ... +func (l *ListOperatorsController) Get() error { + ops := htmx.Values[models.Pagination[models.Operator]](l.Ctx().UserContext(), resolvers.ValuesKeyOperators) + + fmt.Println(ops) + + return htmx.RenderComp( + l.Ctx(), + components.Page( + components.PageProps{}, + components.Layout( + components.LayoutProps{}, + htmx.Text("Operators"), + ), + ), + ) +} diff --git a/internal/web/ports/handlers.go b/internal/web/ports/handlers.go index aebb5774..9a697ec9 100644 --- a/internal/web/ports/handlers.go +++ b/internal/web/ports/handlers.go @@ -10,4 +10,6 @@ type Handlers interface { Dashboard() fiber.Handler // Me ... Me() fiber.Handler + // ListOperators ... + ListOperators() fiber.Handler } diff --git a/internal/web/ports/operators.go b/internal/web/ports/operators.go new file mode 100644 index 00000000..fe65cbe4 --- /dev/null +++ b/internal/web/ports/operators.go @@ -0,0 +1,13 @@ +package ports + +import ( + "context" + + "github.com/zeiss/typhoon/internal/api/models" +) + +// Operators is a port that defines the methods for operators +type Operators interface { + // ListOperators is a method that returns a list of operators + ListOperators(ctx context.Context, pagination *models.Pagination[models.Operator]) error +} diff --git a/internal/web/ports/repo.go b/internal/web/ports/repo.go index a0ebb80c..3302e9bd 100644 --- a/internal/web/ports/repo.go +++ b/internal/web/ports/repo.go @@ -3,4 +3,5 @@ package ports // Repository ... type Repository interface { Users + Operators } diff --git a/pkg/resolvers/operators.go b/pkg/resolvers/operators.go new file mode 100644 index 00000000..18d94b6d --- /dev/null +++ b/pkg/resolvers/operators.go @@ -0,0 +1,42 @@ +package resolvers + +import ( + "github.com/gofiber/fiber/v2" + htmx "github.com/zeiss/fiber-htmx" + "github.com/zeiss/typhoon/internal/api/models" + "github.com/zeiss/typhoon/internal/web/ports" +) + +const ( + // ValuesKeyOperators ... + ValuesKeyOperators = "user" +) + +// ListOperators ... +func ListOperators(db ports.Repository) htmx.ResolveFunc { + return func(ctx *fiber.Ctx) (interface{}, interface{}, error) { + query := struct { + Limit int `json:"limit" xml:"limit" form:"limit"` + Offset int `json:"offset" xml:"offset" form:"offset"` + Search string `json:"search" xml:"search" form:"search"` + }{} + + err := ctx.QueryParser(&query) + if err != nil { + return nil, nil, err + } + + ops := models.Pagination[models.Operator]{} + + ops.Limit = query.Limit + ops.Offset = query.Offset + ops.Search = query.Search + + err = db.ListOperators(ctx.Context(), &ops) + if err != nil { + return nil, nil, err + } + + return ValuesKeyOperators, ops, nil + } +}