Skip to content

Commit

Permalink
wip: refactor pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jun 6, 2024
1 parent ef77874 commit 64446cd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 92 deletions.
12 changes: 6 additions & 6 deletions internal/web/adapters/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func (t *datastoreTx) CreateAccount(ctx context.Context, account *models.Account
}

// ListAccounts ...
func (t *datastoreTx) ListAccounts(ctx context.Context, pagination *models.Pagination[models.Account]) error {
return t.tx.Scopes(models.Paginate(&pagination.Rows, pagination, t.tx)).
func (t *datastoreTx) ListAccounts(ctx context.Context, pagination *tables.Results[models.Account]) error {
return t.tx.Scopes(tables.PaginatedResults(&pagination.Rows, pagination, t.tx)).
Preload("SigningKeyGroups").
Preload("SigningKeyGroups.Key").
Preload("Key").
Expand Down Expand Up @@ -202,8 +202,8 @@ func (t *datastoreTx) GetUser(ctx context.Context, user *models.User) error {
}

// ListUsers ...
func (t *datastoreTx) ListUsers(ctx context.Context, pagination *models.Pagination[models.User]) error {
return t.tx.Scopes(models.Paginate(&pagination.Rows, pagination, t.tx)).Find(&pagination.Rows).Error
func (t *datastoreTx) ListUsers(ctx context.Context, pagination *tables.Results[models.User]) error {
return t.tx.Scopes(tables.PaginatedResults(&pagination.Rows, pagination, t.tx)).Find(&pagination.Rows).Error
}

// CreateUser ...
Expand Down Expand Up @@ -237,8 +237,8 @@ func (t *datastoreTx) GetSystem(ctx context.Context, system *models.System) erro
}

// ListSystems is a method that returns a list of systems
func (t *datastoreTx) ListSystems(ctx context.Context, pagination *models.Pagination[models.System]) error {
return t.tx.Scopes(models.Paginate(&pagination.Rows, pagination, t.tx)).Preload("Tags").Find(&pagination.Rows).Error
func (t *datastoreTx) ListSystems(ctx context.Context, pagination *tables.Results[models.System]) error {
return t.tx.Scopes(tables.PaginatedResults(&pagination.Rows, pagination, t.tx)).Preload("Tags").Find(&pagination.Rows).Error
}

// DeleteSystem is a method that deletes a system
Expand Down
17 changes: 9 additions & 8 deletions internal/web/controllers/accounts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/tables"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/web/components"
"github.com/zeiss/typhoon/internal/web/components/accounts"
Expand All @@ -15,7 +16,7 @@ var _ = htmx.Controller(&ListAccountsController{})

// ListAccountsController ...
type ListAccountsController struct {
Pagination models.Pagination[models.Account]
Results tables.Results[models.Account]

store ports.Datastore
htmx.DefaultController
Expand All @@ -24,21 +25,21 @@ type ListAccountsController struct {
// NewListAccountsController ...
func NewListAccountsController(store ports.Datastore) *ListAccountsController {
return &ListAccountsController{
Pagination: models.Pagination[models.Account]{Limit: 10},
Results: tables.Results[models.Account]{Limit: 10},
DefaultController: htmx.DefaultController{},
store: store,
}
}

// Prepare ...
func (l *ListAccountsController) Prepare() error {
err := l.BindQuery(&l.Pagination)
err := l.BindQuery(&l.Results)
if err != nil {
return err
}

return l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListAccounts(ctx, &l.Pagination)
return tx.ListAccounts(ctx, &l.Results)
})
}

Expand All @@ -59,10 +60,10 @@ func (l *ListAccountsController) Get() error {
cards.BodyProps{},
accounts.AccountsTable(
accounts.AccountsTableProps{
Accounts: l.Pagination.GetRows(),
Offset: l.Pagination.GetOffset(),
Limit: l.Pagination.GetLimit(),
Total: l.Pagination.GetTotalRows(),
Accounts: l.Results.GetRows(),
Offset: l.Results.GetOffset(),
Limit: l.Results.GetLimit(),
Total: l.Results.GetLen(),
},
),
),
Expand Down
2 changes: 1 addition & 1 deletion internal/web/controllers/operators/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (l *ListOperatorsController) Get() error {
Operators: l.Results.GetRows(),
Offset: l.Results.GetOffset(),
Limit: l.Results.GetLimit(),
Total: l.Results.GetTotalRows(),
Total: l.Results.GetLen(),
},
),
),
Expand Down
32 changes: 10 additions & 22 deletions internal/web/controllers/systems/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@ import (

htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/tables"
)

var _ = htmx.Controller(&ListSystemsController{})

// ListSystemsControllerParams ...
type ListSystemsControllerParams struct {
Offset int `json:"offset" form:"offset" params:"offset"`
Limit int `json:"limit" form:"limit" params:"limit"`
Search string `json:"search" form:"search" params:"search"`
Sort string `json:"sort" form:"sort" params:"sort"`
}

// ListSystemsController ...
type ListSystemsController struct {
Pagination models.Pagination[models.System]
Results tables.Results[models.System]

store ports.Datastore
htmx.DefaultController
Expand All @@ -33,27 +26,22 @@ type ListSystemsController struct {
// NewListSystemsController ...
func NewListSystemsController(store ports.Datastore) *ListSystemsController {
return &ListSystemsController{
Pagination: models.Pagination[models.System]{Limit: 10},
Results: tables.Results[models.System]{Limit: 10},
DefaultController: htmx.DefaultController{},
store: store,
}
}

// Prepare ...
func (l *ListSystemsController) Prepare() error {
err := l.BindQuery(&l.Pagination)
err := l.BindQuery(&l.Results)
if err != nil {
return err
}

err = l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListSystems(ctx, &l.Pagination)
return l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListSystems(ctx, &l.Results)
})
if err != nil {
return err
}

return nil
}

// Get ...
Expand All @@ -74,10 +62,10 @@ func (l *ListSystemsController) Get() error {
cards.BodyProps{},
systems.SystemsTable(
systems.SystemsTableProps{
Limit: l.Pagination.GetLimit(),
Offset: l.Pagination.GetOffset(),
Total: l.Pagination.GetTotalRows(),
Systems: l.Pagination.GetRows(),
Limit: l.Results.GetLimit(),
Offset: l.Results.GetOffset(),
Total: l.Results.GetLen(),
Systems: l.Results.GetRows(),
},
),
),
Expand Down
45 changes: 13 additions & 32 deletions internal/web/controllers/users/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/tables"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/web/components"
"github.com/zeiss/typhoon/internal/web/components/users"
Expand All @@ -15,10 +16,7 @@ var _ = htmx.Controller(&ListUsersController{})

// ListUsersController ...
type ListUsersController struct {
Offset int `json:"offset" form:"offset"`
Limit int `json:"limit" form:"limit"`
Search string `json:"search" form:"search"`
Sort string `json:"sort" form:"sort"`
Results tables.Results[models.User]

store ports.Datastore
htmx.DefaultController
Expand All @@ -27,44 +25,27 @@ type ListUsersController struct {
// NewListUsersController ...
func NewListUsersController(store ports.Datastore) *ListUsersController {
return &ListUsersController{
store: store,
Results: tables.Results[models.User]{Limit: 10},
DefaultController: htmx.DefaultController{},
store: store,
}
}

// Prepare ...
func (l *ListUsersController) Prepare() error {
err := l.BindParams(l)
err := l.BindQuery(&l.Results)
if err != nil {
return err
}

return nil
return l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListUsers(ctx, &l.Results)
})
}

// Prepare ...
func (l *ListUsersController) Get() error {
pagination := models.Pagination[models.User]{
Offset: l.Offset,
Limit: l.Limit,
Sort: l.Sort,
Search: l.Search,
}

err := l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListUsers(ctx, &pagination)
})
if err != nil {
return err
}

accs := make([]*models.User, 0, len(pagination.Rows))
for _, row := range pagination.Rows {
accs = append(accs, &row)
}

return htmx.RenderComp(
l.Ctx(),
return l.Render(
components.Page(
components.PageProps{
Title: "Users",
Expand All @@ -79,10 +60,10 @@ func (l *ListUsersController) Get() error {
cards.BodyProps{},
users.UsersTable(
users.UsersTableProps{
Users: accs,
Offset: pagination.Offset,
Limit: pagination.Limit,
Total: pagination.TotalRows,
Users: l.Results.GetRows(),
Offset: l.Results.GetOffset(),
Limit: l.Results.GetLimit(),
Total: l.Results.GetLen(),
},
),
),
Expand Down
25 changes: 8 additions & 17 deletions internal/web/controllers/users/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/tables"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/web/components"
"github.com/zeiss/typhoon/internal/web/ports"
Expand All @@ -15,7 +16,7 @@ import (

// NewUserControllerImpl ...
type NewUserControllerImpl struct {
Accounts []*models.Account
Results tables.Results[models.Account]

store ports.Datastore
htmx.DefaultController
Expand All @@ -24,32 +25,22 @@ type NewUserControllerImpl struct {
// NewUserController ...
func NewUserController(store ports.Datastore) *NewUserControllerImpl {
return &NewUserControllerImpl{
store: store,
Results: tables.Results[models.Account]{Limit: 10},
DefaultController: htmx.DefaultController{},
store: store,
}
}

// Prepare ...
func (l *NewUserControllerImpl) Prepare() error {
pagination := models.Pagination[models.Account]{}

err := l.BindQuery(&pagination)
err := l.BindQuery(&l.Results)
if err != nil {
return err
}

err = l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListAccounts(ctx, &pagination)
return l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.ListAccounts(ctx, &l.Results)
})
if err != nil {
return err
}

for _, account := range pagination.Rows {
l.Accounts = append(l.Accounts, &account)
}

return nil
}

// Get ...
Expand Down Expand Up @@ -110,7 +101,7 @@ func (l *NewUserControllerImpl) Get() error {
),
htmx.Name("account_id"),
htmx.Group(
htmx.ForEach(l.Accounts, func(operator *models.Account) htmx.Node {
htmx.ForEach(l.Results.GetRows(), func(operator *models.Account) htmx.Node {
return forms.Option(
forms.OptionProps{
Value: operator.ID.String(),
Expand Down
12 changes: 6 additions & 6 deletions internal/web/ports/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,25 @@ type ReadTx interface {
// GetOperator is a method that returns an operator by ID
GetOperator(ctx context.Context, operator *models.Operator) error
// ListOperators is a method that returns a list of operators
ListOperators(ctx context.Context, pagination *tables.Results[models.Operator]) error
ListOperators(ctx context.Context, results *tables.Results[models.Operator]) error
// GetAccount ...
GetAccount(ctx context.Context, account *models.Account) error
// ListAccounts ...
ListAccounts(ctx context.Context, pagination *models.Pagination[models.Account]) error
ListAccounts(ctx context.Context, results *tables.Results[models.Account]) error
// GetUser is a method that returns a user by ID
GetUser(ctx context.Context, user *models.User) error
// ListUsers is a method that returns a list of users
ListUsers(ctx context.Context, pagination *models.Pagination[models.User]) error
ListUsers(ctx context.Context, results *tables.Results[models.User]) error
// GetProfile is a method that returns the profile of the current user
GetProfile(ctx context.Context, user *adapters.GothUser) error
// GetSystem is a method that returns a system by ID
GetSystem(ctx context.Context, system *models.System) error
// ListSystems is a method that returns a list of systems
ListSystems(ctx context.Context, pagination *models.Pagination[models.System]) error
ListSystems(ctx context.Context, results *tables.Results[models.System]) error
}

// ReadWriteTx provides methods for transactional read and write operations.
type ReadWriteTx interface {
ReadTx

// CreateOperator is a method that creates a new operator
CreateOperator(ctx context.Context, operator *models.Operator) error
// UpdateOperator is a method that updates an operator
Expand All @@ -77,4 +75,6 @@ type ReadWriteTx interface {
UpdateSystem(ctx context.Context, system *models.System) error
// DeleteSystem is a method that deletes a system
DeleteSystem(ctx context.Context, system *models.System) error

ReadTx
}

0 comments on commit 64446cd

Please sign in to comment.