Skip to content

Commit

Permalink
Change owned_vehicles to accessible_vehicles, return both user vehicl…
Browse files Browse the repository at this point in the history
…es and vehicles with privilege and write tests
  • Loading branch information
0xdev22 committed Jul 26, 2023
1 parent 274f419 commit 374d875
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 58 deletions.
38 changes: 19 additions & 19 deletions graph/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Privilege {
}

type Query {
ownedVehicles(
accessibleVehicles(
address: Address!
first: Int
after: String
Expand Down
6 changes: 3 additions & 3 deletions graph/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,7 @@ func (v *VehiclesRepo) createVehiclesResponse(totalCount int64, vehicles models.
return res
}

func (v *VehiclesRepo) GetOwnedVehicles(ctx context.Context, addr common.Address, first *int, after *string) (*gmodel.VehicleConnection, error) {
totalCount, err := models.Vehicles(
models.VehicleWhere.OwnerAddress.EQ(addr.Bytes()),
).Count(ctx, v.pdb.DBS().Reader)
if err != nil {
return nil, err
}

if totalCount == 0 {
return &gmodel.VehicleConnection{
TotalCount: 0,
Edges: []*gmodel.VehicleEdge{},
}, nil
}

func (v *VehiclesRepo) GetAccessibleVehicles(ctx context.Context, addr common.Address, first *int, after *string) (*gmodel.VehicleConnection, error) {
limit := defaultPageSize
if first != nil {
limit = *first
Expand All @@ -100,12 +86,16 @@ func (v *VehiclesRepo) GetOwnedVehicles(ctx context.Context, addr common.Address
}
}

vAlias := "identity_api." + models.TableNames.Vehicles
pAlias := "identity_api." + models.TableNames.Privileges
queryMods := []qm.QueryMod{
models.VehicleWhere.OwnerAddress.EQ(addr.Bytes()),
qm.Select(vAlias + ".*"),
qm.LeftOuterJoin(
"identity_api." + models.TableNames.Privileges + " ON " + vAlias + ".id = " + pAlias + "." + models.PrivilegeColumns.TokenID,
),
qm.Or2(models.VehicleWhere.OwnerAddress.EQ(addr.Bytes())),
qm.Or2(models.PrivilegeWhere.GrantedToAddress.EQ(addr.Bytes())),
// Use limit + 1 here to check if there's a next page.
qm.Limit(limit + 1),
qm.OrderBy(models.VehicleColumns.ID + " DESC"),
qm.Load(models.VehicleRels.TokenPrivileges),
}

if after != nil {
Expand All @@ -121,6 +111,23 @@ func (v *VehiclesRepo) GetOwnedVehicles(ctx context.Context, addr common.Address
queryMods = append(queryMods, models.VehicleWhere.ID.LT(lastCursorVal))
}

totalCount, err := models.Vehicles(queryMods...).Count(ctx, v.pdb.DBS().Reader)
if err != nil {
return nil, err
}

if totalCount == 0 {
return &gmodel.VehicleConnection{
TotalCount: 0,
Edges: []*gmodel.VehicleEdge{},
}, nil
}

queryMods = append(queryMods,
qm.Limit(limit+1),
qm.OrderBy(models.VehicleColumns.ID+" DESC"),
qm.Load(models.VehicleRels.TokenPrivileges))

all, err := models.Vehicles(queryMods...).All(ctx, v.pdb.DBS().Reader)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 374d875

Please sign in to comment.