Skip to content

Commit

Permalink
wip: get operator
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed May 23, 2024
1 parent 7c9cf47 commit f1d964f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/web/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
app.Get("/operators", handlers.ListOperators())
app.Get("/operators/new", handlers.NewOperator())
app.Post("/operators/new", handlers.CreateOperator())
app.Get("/operators/:id", handlers.ShowOperator())

err = app.Listen(s.cfg.Flags.Addr)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/web/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ func (h *handlers) NewOperator() fiber.Handler {
func (h *handlers) CreateOperator() fiber.Handler {
return htmx.NewHxControllerHandler(operators.NewOperatorController(h.db))
}

// ShowOperator ...
func (h *handlers) ShowOperator() fiber.Handler {
return htmx.NewHxControllerHandler(operators.NewShowOperatorController(h.db))
}
4 changes: 3 additions & 1 deletion internal/web/components/operators/operators_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func OperatorsTable(props OperatorsTableProps, children ...htmx.Node) htmx.Node
Cell: func(p tables.TableProps[*models.Operator], row *models.Operator) htmx.Node {
return htmx.Td(
links.Link(
links.LinkProps{},
links.LinkProps{
Href: "/operators/" + row.ID.String(),
},
htmx.Text(row.Name),
),
)
Expand Down
9 changes: 0 additions & 9 deletions internal/web/controllers/operators/new_operator_controller.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package operators

import (
"fmt"

"github.com/go-playground/validator/v10"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
Expand Down Expand Up @@ -65,13 +63,6 @@ func (l *NewOperatorControllerImpl) Post() error {
return nil
}

// Error ...
func (l *NewOperatorControllerImpl) Error(err error) error {
fmt.Println(err)

return nil
}

// Get ...
func (l *NewOperatorControllerImpl) Get() error {
return htmx.RenderComp(
Expand Down
116 changes: 116 additions & 0 deletions internal/web/controllers/operators/show_operators_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package operators

import (
"github.com/google/uuid"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/web/components"
"github.com/zeiss/typhoon/internal/web/ports"
)

// ShowOperatorControllerImpl ...
type ShowOperatorControllerImpl struct {
ID uuid.UUID `json:"name" form:"name" validate:"required:uuid"`

ports.Operators
htmx.DefaultController
}

// NewShowOperatorController ...
func NewShowOperatorController(db ports.Operators) *ShowOperatorControllerImpl {
return &ShowOperatorControllerImpl{
Operators: db,
DefaultController: htmx.DefaultController{},
}
}

// Get ...
func (l *ShowOperatorControllerImpl) Get() error {
err := l.BindParams(l)
if err != nil {
return err
}

op := models.Operator{ID: l.ID}

err = l.GetOperator(l.Context(), &op)
if err != nil {
return err
}

return htmx.RenderComp(
l.Ctx(),
components.Page(
components.PageProps{},
components.Layout(
components.LayoutProps{},
cards.CardBordered(
cards.CardProps{},
cards.Body(
cards.BodyProps{},
cards.Title(
cards.TitleProps{},
htmx.Text("Overview"),
),
htmx.Div(
htmx.H1(
htmx.Text(op.Name),
),
htmx.P(
htmx.Text(op.Description),
),
htmx.Div(
htmx.ClassNames{
"flex": true,
"flex-col": true,
"py-2": true,
},
htmx.H4(
htmx.ClassNames{
"text-gray-500": true,
},
htmx.Text("Created at"),
),
htmx.H3(
htmx.Text(
op.CreatedAt.Format("2006-01-02 15:04:05"),
),
),
),
htmx.Div(
htmx.ClassNames{
"flex": true,
"flex-col": true,
"py-2": true,
},
htmx.H4(
htmx.ClassNames{
"text-gray-500": true,
},
htmx.Text("Updated at"),
),
htmx.H3(
htmx.Text(
op.UpdatedAt.Format("2006-01-02 15:04:05"),
),
),
),
),
cards.Actions(
cards.ActionsProps{},
buttons.OutlinePrimary(
buttons.ButtonProps{},
htmx.HxDelete(""),
htmx.HxConfirm("Are you sure you want to delete this lens?"),
htmx.Text("Delete"),
),
),
),
),
),
),
)

}
2 changes: 2 additions & 0 deletions internal/web/ports/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ type Handlers interface {
NewOperator() fiber.Handler
// CreateOperator ...
CreateOperator() fiber.Handler
// ShowOperator ...
ShowOperator() fiber.Handler
}

0 comments on commit f1d964f

Please sign in to comment.