Skip to content

Commit

Permalink
wip: teams controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jun 17, 2024
1 parent a3aac3b commit bd5a082
Show file tree
Hide file tree
Showing 13 changed files with 1,265 additions and 20 deletions.
16 changes: 12 additions & 4 deletions cmd/server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
requestid "github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/katallaxie/pkg/server"
"github.com/spf13/cobra"
authz "github.com/zeiss/fiber-authz"
goth "github.com/zeiss/fiber-goth"
adapter "github.com/zeiss/fiber-goth/adapters/gorm"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
Expand Down Expand Up @@ -88,10 +88,13 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
return err
}

tbac := authz.NewTBAC(conn)
gorm, err := adapter.New(conn)
if err != nil {
return err
}

gothConfig := goth.Config{
Adapter: tbac,
Adapter: gorm,
Secret: goth.GenerateKey(),
CookieHTTPOnly: true,
ResponseFilter: func(c *fiber.Ctx) error {
Expand All @@ -106,14 +109,19 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
app.Use(logger.New())

app.Use(goth.NewProtectMiddleware(gothConfig))
app.Use(authz.SetAuthzHandler(authz.NewNoopObjectResolver(), authz.NewNoopActionResolver(), authz.NewGothAuthzPrincipalResolver()))

app.Get("/", handlers.Dashboard())
app.Get("/login", handlers.Login())
app.Get("/login/:provider", goth.NewBeginAuthHandler(gothConfig))
app.Get("/auth/:provider/callback", goth.NewCompleteAuthHandler(gothConfig))
app.Get("/logout", goth.NewLogoutHandler(gothConfig))

// Site ...
site := app.Group("/site")
site.Get("/teams", handlers.ListTeams())
site.Get("/teams/new", handlers.NewTeam())
site.Get("/teams/:id", handlers.ShowTeam())

// Me ...
app.Get("/me", handlers.Me())

Expand Down
36 changes: 36 additions & 0 deletions internal/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/zeiss/service-lens/internal/controllers/me"
"github.com/zeiss/service-lens/internal/controllers/profiles"
"github.com/zeiss/service-lens/internal/controllers/settings"
"github.com/zeiss/service-lens/internal/controllers/teams"
"github.com/zeiss/service-lens/internal/controllers/workloads"
"github.com/zeiss/service-lens/internal/controllers/workloads/partials"
"github.com/zeiss/service-lens/internal/controllers/workloads/questions"
Expand Down Expand Up @@ -286,3 +287,38 @@ func (a *handlers) UpdateWorkloadAnswer() fiber.Handler {
return questions.NewWorkloadUpdateAnswerController(a.store)
})
}

// ListTeams ...
func (a *handlers) ListTeams() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return teams.NewTeamListController(a.store)
})
}

// NewTeam ...
func (a *handlers) NewTeam() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return teams.NewTeamController(a.store)
})
}

// DeleteTeam ...
func (a *handlers) DeleteTeam() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return teams.NewTeamDeleteController(a.store)
})
}

// EditTeam ...
func (a *handlers) EditTeam() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return teams.NewTeamEditController(a.store)
})
}

// ShowTeam ...
func (a *handlers) ShowTeam() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return teams.NewTeamShowController(a.store)
})
}
2 changes: 0 additions & 2 deletions internal/components/layout.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package components

import (
authz "github.com/zeiss/fiber-authz"
"github.com/zeiss/fiber-goth/adapters"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/dividers"
Expand All @@ -13,7 +12,6 @@ import (
// LayoutProps is the properties for the Layout component.
type LayoutProps struct {
Children []htmx.Node
Team *authz.Team
User adapters.GothUser
Path string
}
Expand Down
1 change: 0 additions & 1 deletion internal/components/main-menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func MainMenu(p MainMenuProps, children ...htmx.Node) htmx.Node {
),
),
),

menus.MenuItem(
menus.MenuItemProps{
ClassNames: htmx.ClassNames{
Expand Down
161 changes: 161 additions & 0 deletions internal/components/teams/teams-table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package teams

import (
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/links"
"github.com/zeiss/fiber-htmx/components/tables"
"github.com/zeiss/service-lens/internal/models"
)

// TeamsTableProps ...
type TeamsTableProps struct {
Teams []*models.Team
Offset int
Limit int
Total int
}

// TeamsTableProps ...
func TeamsTable(props TeamsTableProps, children ...htmx.Node) htmx.Node {
return htmx.Div(
htmx.ClassNames{},
tables.Table(
tables.TableProps{
ID: "teams-tables",
Pagination: tables.TablePagination(
tables.TablePaginationProps{
Pagination: tables.Pagination(
tables.PaginationProps{
Offset: props.Offset,
Limit: props.Limit,
Total: props.Total,
},
tables.Prev(
tables.PaginationProps{
Total: props.Total,
Offset: props.Offset,
Limit: props.Limit,
URL: "/teams",
},
),

tables.Select(
tables.SelectProps{
Total: props.Total,
Offset: props.Offset,
Limit: props.Limit,
Limits: tables.DefaultLimits,
URL: "/teams",
},
),
tables.Next(
tables.PaginationProps{
Total: props.Total,
Offset: props.Offset,
Limit: props.Limit,
URL: "/teams",
},
),
),
},
),
Toolbar: tables.TableToolbar(
tables.TableToolbarProps{
ClassNames: htmx.ClassNames{
"flex": true,
"items-center": true,
"justify-between": true,
"px-5": true,
"pt-5": true,
},
},
htmx.Div(
htmx.ClassNames{
"inline-flex": true,
"items-center": true,
"gap-3": true,
},
forms.TextInputBordered(
forms.TextInputProps{
ClassNames: htmx.ClassNames{
"input-sm": true,
},
Placeholder: "Search ...",
},
),
),
htmx.A(
htmx.Href("/environments/new"),
buttons.Outline(
buttons.ButtonProps{
ClassNames: htmx.ClassNames{
"btn-sm": true,
},
},
htmx.Text("Create Team"),
),
),
),
},
[]tables.ColumnDef[*models.Team]{
{
ID: "id",
AccessorKey: "id",
Header: func(p tables.TableProps) htmx.Node {
return htmx.Th(htmx.Text("ID"))
},
Cell: func(p tables.TableProps, row *models.Team) htmx.Node {
return htmx.Td(
htmx.Text(row.ID.String()),
)
},
},
{
ID: "name",
AccessorKey: "name",
Header: func(p tables.TableProps) htmx.Node {
return htmx.Th(htmx.Text("Name"))
},
Cell: func(p tables.TableProps, row *models.Team) htmx.Node {
return htmx.Td(
links.Link(
links.LinkProps{
Href: "/teams/" + row.ID.String(),
},
htmx.Text(row.Name),
),
)
},
},
{
Header: func(p tables.TableProps) htmx.Node {
return nil
},
Cell: func(p tables.TableProps, row *models.Team) htmx.Node {
return htmx.Td(
buttons.Button(
buttons.ButtonProps{
ClassNames: htmx.ClassNames{
"btn-square": true,
},
},
),
)
},
},
},
props.Teams,
// Pagination: ProfileListTablePaginationComponent(
// ProfileListTablePaginationProps{
// Limit: props.Limit,
// Offset: props.Offset,
// Total: props.Total,
// Target: "profiles-tables",
// Team: props.Team,
// },
// ),
),
)
}
14 changes: 1 addition & 13 deletions internal/components/user-menu.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
package components

import (
authz "github.com/zeiss/fiber-authz"
"github.com/zeiss/fiber-htmx/components/links"

htmx "github.com/zeiss/fiber-htmx"
)

// UserNavProps ...
type UserNavProps struct {
// User ...
User *authz.User
}

// UserNav ...
func UserNav(p UserNavProps) htmx.Node {
if p.User == nil {
return nil
}

users := []htmx.Node{}
for _, u := range *p.User.Teams {
users = append(users, htmx.Li(htmx.Text(u.Name)))
}

return htmx.Div(
htmx.ClassNames{"dropdown": true, "dropdown-end": true},
htmx.Div(
Expand Down Expand Up @@ -59,7 +47,7 @@ func UserNav(p UserNavProps) htmx.Node {
"rounded-box": true,
"w-52": true,
},
htmx.Group(users...),
// htmx.Group(users...),
htmx.Li(
htmx.A(
htmx.ClassNames{"justify-between": true},
Expand Down
41 changes: 41 additions & 0 deletions internal/controllers/teams/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package teams

import (
"context"

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

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

// NewTeamDeleteController ...
func NewTeamDeleteController(store ports.Datastore) *TeamDeleteControllerImpl {
return &TeamDeleteControllerImpl{
team: models.Team{},
store: store,
}
}

// Prepare ...
func (p *TeamDeleteControllerImpl) Prepare() error {
err := p.BindParams(&p.team)
if err != nil {
return err
}

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

// Delete ...
func (p *TeamDeleteControllerImpl) Delete() error {
return p.Redirect("/site/teams")
}
Loading

0 comments on commit bd5a082

Please sign in to comment.