Skip to content

Commit

Permalink
wip: profiles with team
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jun 17, 2024
1 parent bd5a082 commit 54148fb
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 50 deletions.
2 changes: 1 addition & 1 deletion cmd/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func run(ctx context.Context) error {
return err
}

seeder := seed.NewSeeder(seed.WithDatabase(conn))
seeder := seed.NewSeeder(conn)
err = seeder.Seed(ctx, seeds...)
if err != nil {
panic(err)
Expand Down
79 changes: 41 additions & 38 deletions cmd/server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/zeiss/fiber-goth/providers/github"
"github.com/zeiss/service-lens/internal/adapters/db"
"github.com/zeiss/service-lens/internal/adapters/handlers"
"github.com/zeiss/service-lens/internal/utils"

"github.com/gofiber/fiber/v2"
logger "github.com/gofiber/fiber/v2/middleware/logger"
Expand Down Expand Up @@ -122,52 +123,54 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
site.Get("/teams/new", handlers.NewTeam())
site.Get("/teams/:id", handlers.ShowTeam())

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

// Settings ...
app.Get("/settings", handlers.ShowSettings())
// Team ...
team := app.Group("/teams/:t_slug", utils.ResolveTeam(conn))

// Profiles ...
app.Get("/profiles", handlers.ListProfiles())
app.Get("/profiles/new", handlers.NewProfile())
app.Post("/profiles/new", handlers.CreateProfile())
app.Get("/profiles/:id", handlers.ShowProfile())
app.Put("/profiles/:id", handlers.EditProfile())
app.Delete("/profiles/:id", handlers.DeleteProfile())
team.Get("/profiles", handlers.ListProfiles())
team.Get("/profiles/new", handlers.NewProfile())
team.Post("/profiles/new", handlers.CreateProfile())
team.Get("/profiles/:id", handlers.ShowProfile())
team.Put("/profiles/:id", handlers.EditProfile())
team.Delete("/profiles/:id", handlers.DeleteProfile())

// Environments ...
app.Get("/environments", handlers.ListEnvironments())
app.Get("/environments/new", handlers.NewEnvironment())
app.Post("/environments/new", handlers.CreateEnvironment())
app.Get("/environments/:id", handlers.ShowEnvironment())
app.Get("/environments/:id/edit", handlers.EditEnvironment())
app.Put("/environments/:id", handlers.UpdateEnvironment())
app.Delete("/environments/:id", handlers.DeleteEnvironment())
team.Get("/environments", handlers.ListEnvironments())
team.Get("/environments/new", handlers.NewEnvironment())
team.Post("/environments/new", handlers.CreateEnvironment())
team.Get("/environments/:id", handlers.ShowEnvironment())
team.Get("/environments/:id/edit", handlers.EditEnvironment())
team.Put("/environments/:id", handlers.UpdateEnvironment())
team.Delete("/environments/:id", handlers.DeleteEnvironment())

// Lenses ...
app.Get("/lenses", handlers.ListLenses())
app.Get("/lenses/new", handlers.NewLens())
app.Post("/lenses/new", handlers.CreateLens())
app.Get("/lenses/:id", handlers.ShowLens())
app.Get("/lenses/:id/edit", handlers.EditLens())
app.Put("/lenses/:id", handlers.UpdateLens())
app.Delete("/lenses/:id", handlers.DeleteLens())
team.Get("/lenses", handlers.ListLenses())
team.Get("/lenses/new", handlers.NewLens())
team.Post("/lenses/new", handlers.CreateLens())
team.Get("/lenses/:id", handlers.ShowLens())
team.Get("/lenses/:id/edit", handlers.EditLens())
team.Put("/lenses/:id", handlers.UpdateLens())
team.Delete("/lenses/:id", handlers.DeleteLens())

// Workloads ...
app.Get("/workloads", handlers.ListWorkloads())
app.Get("/workloads/new", handlers.NewWorkload())
app.Post("/workloads/new", handlers.CreateWorkload())
app.Get("/workloads/:id", handlers.ShowWorkload())
app.Get("/workloads/:id/edit", handlers.EditWorkload())
team.Get("/workloads", handlers.ListWorkloads())
team.Get("/workloads/new", handlers.NewWorkload())
team.Post("/workloads/new", handlers.CreateWorkload())
team.Get("/workloads/:id", handlers.ShowWorkload())
team.Get("/workloads/:id/edit", handlers.EditWorkload())
// app.Put("/workloads/:id", handlers.UpdateWorkload())
app.Delete("/workloads/:id", handlers.DeleteWorkload())
app.Get("/workloads/partials/environments", handlers.ListEnvironmentsPartial())
app.Get("/workloads/partials/profiles", handlers.ListProfilesPartial())
app.Get("/workloads/:id/lenses/:lens", handlers.ShowWorkloadLens())
app.Get("/workloads/:id/lenses/:lens/edit", handlers.EditWorkloadLens())
app.Get("/workloads/:workload/lenses/:lens/question/:question", handlers.ShowLensQuestion())
app.Put("/workloads/:workload/lenses/:lens/question/:question", handlers.UpdateWorkloadAnswer())
team.Delete("/workloads/:id", handlers.DeleteWorkload())
team.Get("/workloads/partials/environments", handlers.ListEnvironmentsPartial())
team.Get("/workloads/partials/profiles", handlers.ListProfilesPartial())
team.Get("/workloads/:id/lenses/:lens", handlers.ShowWorkloadLens())
team.Get("/workloads/:id/lenses/:lens/edit", handlers.EditWorkloadLens())
team.Get("/workloads/:workload/lenses/:lens/question/:question", handlers.ShowLensQuestion())
team.Put("/workloads/:workload/lenses/:lens/question/:question", handlers.UpdateWorkloadAnswer())

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

// Settings ...
app.Get("/settings", handlers.ShowSettings())

err = app.Listen(s.cfg.Flags.Addr)
if err != nil {
Expand Down
13 changes: 10 additions & 3 deletions internal/adapters/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"

"github.com/google/uuid"
"github.com/zeiss/fiber-htmx/components/tables"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/ports"
Expand Down Expand Up @@ -119,13 +120,19 @@ func (t *datastoreTx) GetUser(ctx context.Context, user *adapters.GothUser) erro
}

// ListProfiles is a method that returns a list of profiles
func (t *datastoreTx) ListProfiles(ctx context.Context, pagination *tables.Results[models.Profile]) error {
return t.tx.Scopes(tables.PaginatedResults(&pagination.Rows, pagination, t.tx)).Find(&pagination.Rows).Error
func (t *datastoreTx) ListProfiles(ctx context.Context, team uuid.UUID, pagination *tables.Results[models.Profile]) error {
return t.tx.
Scopes(tables.PaginatedResults(&pagination.Rows, pagination, t.tx)).
Where("team_id = ?", team).
Find(&pagination.Rows).Error
}

// GetProfile is a method that returns a profile by ID
func (t *datastoreTx) GetProfile(ctx context.Context, profile *models.Profile) error {
return t.tx.Preload("Answers").First(profile).Error
return t.tx.
Preload("Answers").
Where(profile).
First(profile).Error
}

// CreateProfile is a method that creates a profile
Expand Down
12 changes: 10 additions & 2 deletions internal/components/profiles/profiles-table.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package profiles

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,9 +11,15 @@ import (
"github.com/zeiss/service-lens/internal/models"
)

const (
createProfileURL = "/teams/%s/profiles/new"
showProfileURL = "/teams/%s/profiles/%s"
)

// ProfilesTableProps ...
type ProfilesTableProps struct {
Profiles []*models.Profile
Team string
Offset int
Limit int
Total int
Expand Down Expand Up @@ -87,7 +95,7 @@ func ProfilesTable(props ProfilesTableProps, children ...htmx.Node) htmx.Node {
),
),
htmx.A(
htmx.Href("/profiles/new"),
htmx.Href(fmt.Sprintf(createProfileURL, props.Team)),
buttons.Outline(
buttons.ButtonProps{
ClassNames: htmx.ClassNames{
Expand Down Expand Up @@ -122,7 +130,7 @@ func ProfilesTable(props ProfilesTableProps, children ...htmx.Node) htmx.Node {
return htmx.Td(
links.Link(
links.LinkProps{
Href: "/profiles/" + row.ID.String(),
Href: fmt.Sprintf(showProfileURL, props.Team, row.ID.String()),
},
htmx.Text(row.Name),
),
Expand Down
16 changes: 15 additions & 1 deletion internal/controllers/profiles/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (

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

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

const (
listProfilesURL = "/teams/%s/profiles"
)

var validate *validator.Validate

// CreateProfileControllerImpl ...
Expand All @@ -29,6 +34,12 @@ func NewCreateProfileController(store ports.Datastore) *CreateProfileControllerI
}
}

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

// Prepare ...
func (l *CreateProfileControllerImpl) Prepare() error {
validate = validator.New()
Expand All @@ -38,6 +49,9 @@ func (l *CreateProfileControllerImpl) Prepare() error {
return err
}

team := utils.FromContextTeam(l.Ctx())
l.profile.TeamID = team.ID

err = validate.Struct(&l.profile)
if err != nil {
return err
Expand All @@ -50,5 +64,5 @@ func (l *CreateProfileControllerImpl) Prepare() error {

// Post ...
func (l *CreateProfileControllerImpl) Post() error {
return l.Redirect(fmt.Sprintf("/profiles/%s", l.profile.ID))
return l.Redirect(fmt.Sprintf(listProfilesURL, utils.FromContextTeam(l.Ctx()).Slug))
}
6 changes: 5 additions & 1 deletion internal/controllers/profiles/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/zeiss/service-lens/internal/components/profiles"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/ports"
"github.com/zeiss/service-lens/internal/utils"

htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/tables"
Expand All @@ -30,8 +31,10 @@ func (w *ProfileListControllerImpl) Prepare() error {
return err
}

team := utils.FromContextTeam(w.Ctx())

return w.store.ReadTx(w.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListProfiles(ctx, &w.profiles)
return tx.ListProfiles(ctx, team.ID, &w.profiles)
})
}

Expand All @@ -52,6 +55,7 @@ func (w *ProfileListControllerImpl) Get() error {
},
profiles.ProfilesTable(
profiles.ProfilesTableProps{
Team: utils.FromContextTeam(w.Ctx()).Slug,
Profiles: w.profiles.GetRows(),
Offset: w.profiles.GetOffset(),
Limit: w.profiles.GetLimit(),
Expand Down
3 changes: 3 additions & 0 deletions internal/controllers/profiles/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (p *ProfileShowControllerImpl) Prepare() error {
return err
}

team := utils.FromContextTeam(p.Ctx())
p.profile.TeamID = team.ID

err = p.store.ReadTx(p.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListProfileQuestions(ctx, &p.questions)
})
Expand Down
5 changes: 4 additions & 1 deletion internal/controllers/workloads/partials/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

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

htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/dropdowns"
Expand Down Expand Up @@ -32,8 +33,10 @@ func (w *ProfilePartialListControllerImpl) Prepare() error {
return err
}

team := utils.FromContextTeam(w.Ctx())

return w.store.ReadTx(w.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListProfiles(ctx, &w.profiles)
return tx.ListProfiles(ctx, team.ID, &w.profiles)
})
}

Expand Down
4 changes: 2 additions & 2 deletions internal/models/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ type Profile struct {
// Tags ...
Tags []*Tag `json:"tags" gorm:"polymorphic:Taggable;"`
// Team is the team that owns the environment
Team Team `json:"owner" gorm:"foreignKey:TeamID"`
Team Team `json:"owner" gorm:"foreignKey:TeamID" validate:"-"`
// TeamID is the foreign key of the owner
TeamID uuid.UUID `json:"owner_id" gorm:"type:uuid;index"`
TeamID uuid.UUID `json:"owner_id" gorm:"type:uuid;index" validate:"-"`
// CreatedAt ...
CreatedAt time.Time `json:"created_at"`
// UpdatedAt ...
Expand Down
3 changes: 2 additions & 1 deletion internal/ports/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io"

"github.com/google/uuid"
"github.com/zeiss/fiber-goth/adapters"
"github.com/zeiss/fiber-htmx/components/tables"
"github.com/zeiss/service-lens/internal/models"
Expand Down Expand Up @@ -31,7 +32,7 @@ type ReadTx interface {
// GetUser is a method that returns the profile of the current user
GetUser(ctx context.Context, user *adapters.GothUser) error
// ListProfiles is a method that returns a list of profiles
ListProfiles(ctx context.Context, profiles *tables.Results[models.Profile]) error
ListProfiles(ctx context.Context, team uuid.UUID, profiles *tables.Results[models.Profile]) error
// ListProfileQuestions is a method that returns a list of profile questions
ListProfileQuestions(ctx context.Context, questions *tables.Results[models.ProfileQuestion]) error
// GetProfile is a method that returns a profile by ID
Expand Down
34 changes: 34 additions & 0 deletions internal/utils/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package utils

import (
"github.com/gofiber/fiber/v2"
"github.com/zeiss/service-lens/internal/models"
"gorm.io/gorm"
)

type teamContextKey int

const teamKey teamContextKey = iota

// ResolveTeam ...
func ResolveTeam(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
team := models.Team{
Slug: c.Params("t_slug"),
}

err := db.WithContext(c.Context()).Where(&team).First(&team).Error
if err != nil {
return err
}

c.Locals(teamKey, team)

return c.Next()
}
}

// FromContextTeam ...
func FromContextTeam(c *fiber.Ctx) models.Team {
return c.Locals(teamKey).(models.Team)
}

0 comments on commit 54148fb

Please sign in to comment.