Skip to content

Commit

Permalink
wip: create team
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jun 17, 2024
1 parent 54148fb commit 90e5473
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 488 deletions.
1 change: 1 addition & 0 deletions cmd/server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
site := app.Group("/site")
site.Get("/teams", handlers.ListTeams())
site.Get("/teams/new", handlers.NewTeam())
site.Post("/teams/new", handlers.CreateTeam())
site.Get("/teams/:id", handlers.ShowTeam())

// Team ...
Expand Down
7 changes: 7 additions & 0 deletions internal/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,10 @@ func (a *handlers) ShowTeam() fiber.Handler {
return teams.NewTeamShowController(a.store)
})
}

// CreateTeam ...
func (a *handlers) CreateTeam() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return teams.NewCreateTeamController(a.store)
})
}
1 change: 1 addition & 0 deletions internal/components/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type LayoutProps struct {
Children []htmx.Node
User adapters.GothUser
Path string
Team string
}

// WrapProps ...
Expand Down
8 changes: 5 additions & 3 deletions internal/components/main-menu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package components

import (
"fmt"
"strings"

htmx "github.com/zeiss/fiber-htmx"
Expand All @@ -11,6 +12,7 @@ import (
type MainMenuProps struct {
ClassNames htmx.ClassNames
Path string
Team string
}

// MainMenu ...
Expand Down Expand Up @@ -132,7 +134,7 @@ func MainMenu(p MainMenuProps, children ...htmx.Node) htmx.Node {
},
menus.MenuCollapsible(
menus.MenuCollapsibleProps{
Open: strings.HasPrefix(p.Path, "/profiles"),
Open: strings.HasPrefix(p.Path, fmt.Sprintf("/teams/%s/profiles", p.Team)),
},
menus.MenuCollapsibleSummary(
menus.MenuCollapsibleSummaryProps{},
Expand All @@ -146,8 +148,8 @@ func MainMenu(p MainMenuProps, children ...htmx.Node) htmx.Node {
},
menus.MenuLink(
menus.MenuLinkProps{
Href: "/profiles/new",
Active: p.Path == "/profiles/new",
Href: fmt.Sprintf("/teams/%s/profiles/new", p.Team),
Active: p.Path == fmt.Sprintf("/teams/%s/profiles/new", p.Team),
},
htmx.Text("New Profile"),
),
Expand Down
10 changes: 8 additions & 2 deletions internal/components/teams/teams-table.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package teams

import (
"fmt"

htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/forms"
Expand All @@ -9,6 +11,10 @@ import (
"github.com/zeiss/service-lens/internal/models"
)

const (
showTeamURL = "/site/teams/%s"
)

// TeamsTableProps ...
type TeamsTableProps struct {
Teams []*models.Team
Expand Down Expand Up @@ -87,7 +93,7 @@ func TeamsTable(props TeamsTableProps, children ...htmx.Node) htmx.Node {
),
),
htmx.A(
htmx.Href("/environments/new"),
htmx.Href("/site/teams/new"),
buttons.Outline(
buttons.ButtonProps{
ClassNames: htmx.ClassNames{
Expand Down Expand Up @@ -122,7 +128,7 @@ func TeamsTable(props TeamsTableProps, children ...htmx.Node) htmx.Node {
return htmx.Td(
links.Link(
links.LinkProps{
Href: "/teams/" + row.ID.String(),
Href: fmt.Sprintf(showTeamURL, row.ID),
},
htmx.Text(row.Name),
),
Expand Down
62 changes: 62 additions & 0 deletions internal/controllers/teams/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package teams

import (
"context"
"fmt"

"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/ports"

"github.com/go-playground/validator/v10"
htmx "github.com/zeiss/fiber-htmx"
)

const (
listTeamURL = "/site/teams"
)

var validate *validator.Validate

// CreateTeamControllerImpl ...
type CreateTeamControllerImpl struct {
team models.Team
store ports.Datastore
htmx.DefaultController
}

// NewCreateTeamController ...
func NewCreateTeamController(store ports.Datastore) *CreateTeamControllerImpl {
return &CreateTeamControllerImpl{
store: store,
}
}

// Error ...
func (l *CreateTeamControllerImpl) Error(err error) error {
fmt.Println()
return err
}

// Prepare ...
func (l *CreateTeamControllerImpl) Prepare() error {
validate = validator.New()

err := l.BindBody(&l.team)
if err != nil {
return err
}

err = validate.Struct(&l.team)
if err != nil {
return err
}

return l.store.ReadWriteTx(l.Context(), func(ctx context.Context, tx ports.ReadWriteTx) error {
return tx.CreateTeam(ctx, &l.team)
})
}

// Post ...
func (l *CreateTeamControllerImpl) Post() error {
return l.Redirect(listTeamURL)
}
Loading

0 comments on commit 90e5473

Please sign in to comment.