mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-05-21 20:39:22 -05:00
Add paginate backups functionality
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
package backups
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
)
|
||||
|
||||
func (s *Service) ListBackups(
|
||||
ctx context.Context,
|
||||
) ([]dbgen.Backup, error) {
|
||||
return s.dbgen.BackupsServiceListBackups(ctx)
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
-- name: BackupsServiceListBackups :many
|
||||
SELECT * FROM backups;
|
||||
@@ -0,0 +1,44 @@
|
||||
package backups
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
"github.com/eduardolat/pgbackweb/internal/util/paginateutil"
|
||||
)
|
||||
|
||||
type PaginateBackupsParams struct {
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
func (s *Service) PaginateBackups(
|
||||
ctx context.Context, params PaginateBackupsParams,
|
||||
) (paginateutil.PaginateResponse, []dbgen.Backup, error) {
|
||||
page := max(params.Page, 1)
|
||||
limit := max(params.Limit, 100)
|
||||
|
||||
count, err := s.dbgen.BackupsServicePaginateBackupsCount(ctx)
|
||||
if err != nil {
|
||||
return paginateutil.PaginateResponse{}, nil, err
|
||||
}
|
||||
|
||||
paginateParams := paginateutil.PaginateParams{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
}
|
||||
offset := paginateutil.CreateOffsetFromParams(paginateParams)
|
||||
paginateResponse := paginateutil.CreatePaginateResponse(paginateParams, int(count))
|
||||
|
||||
backups, err := s.dbgen.BackupsServicePaginateBackups(
|
||||
ctx, dbgen.BackupsServicePaginateBackupsParams{
|
||||
Limit: int32(params.Limit),
|
||||
Offset: int32(offset),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return paginateutil.PaginateResponse{}, nil, err
|
||||
}
|
||||
|
||||
return paginateResponse, backups, nil
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
-- name: BackupsServicePaginateBackupsCount :one
|
||||
SELECT COUNT(*) FROM backups;
|
||||
|
||||
-- name: BackupsServicePaginateBackups :many
|
||||
SELECT *
|
||||
FROM backups
|
||||
ORDER BY created_at DESC
|
||||
LIMIT sqlc.arg('limit') OFFSET sqlc.arg('offset');
|
||||
Reference in New Issue
Block a user