Skip to content

Commit

Permalink
wip: operators controller
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed May 22, 2024
1 parent 0a307dc commit 4b3417c
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 44 deletions.
3 changes: 3 additions & 0 deletions cmd/web/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/web/adapters/db/operators.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions internal/web/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
}
52 changes: 8 additions & 44 deletions internal/web/components/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...,
),
),
),
Expand Down Expand Up @@ -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"),
),
),
Expand All @@ -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"),
),
),
Expand Down
41 changes: 41 additions & 0 deletions internal/web/controllers/operators/list_operators_controller.go
Original file line number Diff line number Diff line change
@@ -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"),
),
),
)
}
2 changes: 2 additions & 0 deletions internal/web/ports/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ type Handlers interface {
Dashboard() fiber.Handler
// Me ...
Me() fiber.Handler
// ListOperators ...
ListOperators() fiber.Handler
}
13 changes: 13 additions & 0 deletions internal/web/ports/operators.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions internal/web/ports/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package ports
// Repository ...
type Repository interface {
Users
Operators
}
42 changes: 42 additions & 0 deletions pkg/resolvers/operators.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 4b3417c

Please sign in to comment.