Skip to content

Commit

Permalink
Add sort parameter (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamessalvatore authored Oct 15, 2023
1 parent 161cc05 commit b143c3a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 10 deletions.
7 changes: 7 additions & 0 deletions docs/api_docs/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,13 @@ paths:
type: integer
format: int64
description: return snapshots given the offset, it should usually set together with limit
- in: query
name: sort
type: string
enum:
- ASC
- DESC
description: sort order
responses:
'200':
description: returns the flag snapshots
Expand Down
13 changes: 12 additions & 1 deletion pkg/handler/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/go-openapi/runtime/middleware"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

// CRUD is the CRUD interface
Expand Down Expand Up @@ -180,8 +181,18 @@ func (c *crud) GetFlagSnapshots(params flag.GetFlagSnapshotsParams) middleware.R
tx = tx.Offset(int(*params.Offset))
}

descending := true
if params.Sort != nil && *params.Sort == "ASC" {
descending = false
}

if err := tx.
Order("created_at desc").
Order(clause.OrderByColumn{
Column: clause.Column{
Name: "created_at",
},
Desc: descending,
}).
Where(entity.FlagSnapshot{FlagID: util.SafeUint(params.FlagID)}).
Find(&fs).Error; err != nil {
return flag.NewGetFlagSnapshotsDefault(500).WithPayload(
Expand Down
45 changes: 36 additions & 9 deletions pkg/handler/crud_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -476,19 +477,25 @@ func TestGetFlagSnapshots(t *testing.T) {
defer tmpDB.Close()
defer gostub.StubFunc(&getDB, db).Reset()

c.CreateFlag(flag.CreateFlagParams{
Body: &models.CreateFlagRequest{
Description: util.StringPtr("funny flag"),
},
})
f := entity.Flag{}
err := db.Create(&f).Error
assert.NoError(t, err)

fb, err := json.Marshal(f)
assert.NoError(t, err)

for i := 0; i < numOfSnapshots; i++ {
entity.SaveFlagSnapshot(db, 1, "[email protected]")
snapshot := entity.FlagSnapshot{
FlagID: 1,
Flag: fb,
}
err := db.Create(&snapshot).Error
assert.NoError(t, err)
}

t.Run("GetFlagSnapshots - got all the results", func(t *testing.T) {
res = c.GetFlagSnapshots(flag.GetFlagSnapshotsParams{})
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, numOfSnapshots+1)
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, numOfSnapshots)
})

t.Run("GetFlagSnapshots (with limit)", func(t *testing.T) {
Expand All @@ -504,8 +511,28 @@ func TestGetFlagSnapshots(t *testing.T) {
Offset: util.Int64Ptr(int64(2)),
})
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, 2)
assert.Equal(t, int64(9), res.(*flag.GetFlagSnapshotsOK).Payload[0].ID)
assert.Equal(t, int64(8), res.(*flag.GetFlagSnapshotsOK).Payload[1].ID)
assert.Equal(t, int64(8), res.(*flag.GetFlagSnapshotsOK).Payload[0].ID)
assert.Equal(t, int64(7), res.(*flag.GetFlagSnapshotsOK).Payload[1].ID)
})

t.Run("GetFlagSnapshots (sort ascending)", func(t *testing.T) {
res = c.GetFlagSnapshots(flag.GetFlagSnapshotsParams{
Limit: util.Int64Ptr(int64(2)),
Sort: util.StringPtr("ASC"),
})
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, 2)
assert.Equal(t, int64(1), res.(*flag.GetFlagSnapshotsOK).Payload[0].ID)
assert.Equal(t, int64(2), res.(*flag.GetFlagSnapshotsOK).Payload[1].ID)
})

t.Run("GetFlagSnapshots (sort descending)", func(t *testing.T) {
res = c.GetFlagSnapshots(flag.GetFlagSnapshotsParams{
Limit: util.Int64Ptr(int64(2)),
Sort: util.StringPtr("DESC"),
})
assert.Len(t, res.(*flag.GetFlagSnapshotsOK).Payload, 2)
assert.Equal(t, int64(10), res.(*flag.GetFlagSnapshotsOK).Payload[0].ID)
assert.Equal(t, int64(9), res.(*flag.GetFlagSnapshotsOK).Payload[1].ID)
})
}

Expand Down
7 changes: 7 additions & 0 deletions swagger/flag_snapshots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ get:
type: integer
format: int64
description: return snapshots given the offset, it should usually set together with limit
- in: query
name: sort
type: string
enum:
- ASC
- DESC
description: sort order
responses:
200:
description: returns the flag snapshots
Expand Down
20 changes: 20 additions & 0 deletions swagger_gen/restapi/embedded_spec.go

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

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

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

0 comments on commit b143c3a

Please sign in to comment.