Skip to content

Commit

Permalink
feature/si-2182-use-pagination-helper-in-other-places (#64)
Browse files Browse the repository at this point in the history
* use ValidateFirstLast helper for ADs and vehicles

* dont need to redefine limit
  • Loading branch information
Allyson-English authored Nov 20, 2023
1 parent 851584d commit e558bb6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 51 deletions.
27 changes: 3 additions & 24 deletions internal/repositories/aftermarket_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,9 @@ import (
// @Param last [*int] "the number of devices to return from previous pages"
// @Param before [*string] "base64 string representing a device tokenID. Pointer to where we start fetching devices from previous pages"
func (r *Repository) GetAftermarketDevices(ctx context.Context, first *int, after *string, last *int, before *string, filterBy *gmodel.AftermarketDevicesFilter) (*gmodel.AftermarketDeviceConnection, error) {
var limit int

if first != nil {
if last != nil {
return nil, gqlerror.Errorf("Pass `first` or `last`, but not both.")
}
if *first < 0 {
return nil, gqlerror.Errorf("The value for `first` cannot be negative.")
}
if *first > maxPageSize {
return nil, gqlerror.Errorf("The value %d for `first` exceeds the limit %d.", *last, maxPageSize)
}
limit = *first
} else {
if last == nil {
return nil, gqlerror.Errorf("Provide `first` or `last`.")
}
if *last < 0 {
return nil, gqlerror.Errorf("The value for `last` cannot be negative.")
}
if *last > maxPageSize {
return nil, gqlerror.Errorf("The value %d for `last` exceeds the limit %d.", *last, maxPageSize)
}
limit = *last
limit, err := helpers.ValidateFirstLast(first, last, maxPageSize)
if err != nil {
return nil, err
}

where := []qm.QueryMod{}
Expand Down
1 change: 0 additions & 1 deletion internal/repositories/dcn.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (r *Repository) GetDCNByName(ctx context.Context, name string) (*gmodel.Dcn
var dcnCursorColumnsTuple = "(" + models.DCNColumns.MintedAt + ", " + models.DCNColumns.Node + ")"

func (r *Repository) GetDCNs(ctx context.Context, first *int, after *string, last *int, before *string, filterBy *gmodel.DCNFilter) (*gmodel.DCNConnection, error) {
var limit int
limit, err := helpers.ValidateFirstLast(first, last, maxPageSize)
if err != nil {
return nil, err
Expand Down
29 changes: 3 additions & 26 deletions internal/repositories/vehicles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/DIMO-Network/identity-api/models"
"github.com/DIMO-Network/shared/db"
"github.com/ethereum/go-ethereum/common"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/vmihailenco/msgpack/v5"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -139,33 +138,11 @@ const maxPageSize = 100
// @Param last [*int] "the number of devices to return from previous pages"
// @Param before [*string] "base64 string representing a device tokenID. Pointer to where we start fetching devices from previous pages"
func (v *Repository) GetVehicles(ctx context.Context, first *int, after *string, last *int, before *string, filterBy *gmodel.VehiclesFilter) (*gmodel.VehicleConnection, error) {
var limit int

if first != nil {
if last != nil {
return nil, gqlerror.Errorf("Pass `first` or `last`, but not both.")
}
if *first < 0 {
return nil, gqlerror.Errorf("The value for `first` cannot be negative.")
}
if *first > maxPageSize {
return nil, gqlerror.Errorf("The value %d for `first` exceeds the limit %d.", *last, maxPageSize)
}
limit = *first
} else {
if last == nil {
return nil, gqlerror.Errorf("Provide `first` or `last`.")
}
if *last < 0 {
return nil, gqlerror.Errorf("The value for `last` cannot be negative.")
}
if *last > maxPageSize {
return nil, gqlerror.Errorf("The value %d for `last` exceeds the limit %d.", *last, maxPageSize)
}
limit = *last
limit, err := helpers.ValidateFirstLast(first, last, maxPageSize)
if err != nil {
return nil, err
}

var err error
var totalCount int64
var queryMods []qm.QueryMod
if filterBy != nil && filterBy.Privileged != nil {
Expand Down

0 comments on commit e558bb6

Please sign in to comment.