Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pagination data #26

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion model/updateModels.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ func (update UpdatePayload) Validate() error {
}

type Updates struct {
Updates []Update `json:"updates"`
Updates []Update `json:"updates"`
Pagination Pagination `json:"pagination"`
}

type Pagination struct {
TotalPages int `json:"totalPages"`
Page int `json:"page"`
Size int `json:"size"`
}

type Person struct {
Expand Down
19 changes: 13 additions & 6 deletions repository/updateRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type UpdateRepository interface {
StoreUpdate(ctx context.Context, update model.Update) error
GetUpdates(ctx context.Context, query bson.D, page int, size int, sortBy string, sortOrder int) ([]model.Update, error)
GetUpdates(ctx context.Context, query bson.D, page int, size int, sortBy string, sortOrder int) ([]model.Update, int64, error)
GetUpdate(ctx context.Context, catalogId string, resourceId string, updateId string) (*model.Update, error)
}

Expand All @@ -37,7 +37,7 @@ func (r UpdateRepositoryImpl) StoreUpdate(ctx context.Context, update model.Upda
return err
}

func (r UpdateRepositoryImpl) GetUpdates(ctx context.Context, query bson.D, page int, size int, sortBy string, sortOrder int) ([]model.Update, error) {
func (r UpdateRepositoryImpl) GetUpdates(ctx context.Context, query bson.D, page int, size int, sortBy string, sortOrder int) ([]model.Update, int64, error) {
skip := (page - 1) * size

opts := options.Find().SetSkip(int64(skip)).SetLimit(int64(size))
Expand All @@ -51,7 +51,7 @@ func (r UpdateRepositoryImpl) GetUpdates(ctx context.Context, query bson.D, page
current, err := r.collection.Find(ctx, query, opts)
if err != nil {
logging.LogAndPrintError(err)
return updates, err
return updates, 0, err
}
defer func(current *mongo.Cursor, ctx context.Context) {
err := current.Close(ctx)
Expand All @@ -65,15 +65,22 @@ func (r UpdateRepositoryImpl) GetUpdates(ctx context.Context, query bson.D, page
err := bson.Unmarshal(current.Current, &update)
if err != nil {
logging.LogAndPrintError(err)
return updates, err
return updates, 0, err
}
updates = append(updates, update)
}
if err := current.Err(); err != nil {
logging.LogAndPrintError(err)
return updates, err
return updates, 0, err
}
return updates, nil

count, err := r.collection.CountDocuments(ctx, query, options.Count())
if err != nil {
logging.LogAndPrintError(err)
return nil, 0, err
}

return updates, count, nil
}

func (r UpdateRepositoryImpl) GetUpdate(ctx context.Context, catalogId string, resourceId string, updateId string) (*model.Update, error) {
Expand Down
10 changes: 7 additions & 3 deletions service/updateService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"encoding/json"
"math"
"net/http"
"time"

Expand Down Expand Up @@ -83,7 +84,7 @@ func (service UpdateServiceImpl) GetUpdates(ctx context.Context, catalogId strin
sortOrderInt = 1
}

databaseUpdates, err := service.UpdateRepository.GetUpdates(ctx, query, page, size, sortByCol, sortOrderInt)
databaseUpdates, count, err := service.UpdateRepository.GetUpdates(ctx, query, page, size, sortByCol, sortOrderInt)
if err != nil {
logrus.Error("Get updates failed")
logging.LogAndPrintError(err)
Expand All @@ -93,11 +94,14 @@ func (service UpdateServiceImpl) GetUpdates(ctx context.Context, catalogId strin
if databaseUpdates == nil {
logrus.Error("No updates found")
logging.LogAndPrintError(err)
return model.Updates{Updates: []model.Update{}}, http.StatusOK
pagination := model.Pagination{ TotalPages: 0, Page: page, Size: size }
return model.Updates{Updates: []model.Update{}, Pagination: pagination}, http.StatusOK
} else {
logrus.Info("Returning updates")
logging.LogAndPrintError(err)
return model.Updates{Updates: databaseUpdates}, http.StatusOK
totalPages := int(math.Ceil(float64(count) / float64(size)))
pagination := model.Pagination{ TotalPages: totalPages, Page: page, Size: size }
return model.Updates{Updates: databaseUpdates, Pagination: pagination}, http.StatusOK
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/get_updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func TestGetUpdates(t *testing.T) {

assert.Nil(t, err)
assert.True(t, len(actualResponse.Updates) > 0)
assert.Equal(t, 1, actualResponse.Pagination.TotalPages)
assert.Equal(t, 1, actualResponse.Pagination.Page)
assert.Equal(t, 10, actualResponse.Pagination.Size)
}

func TestGetUpdatesWithPagination(t *testing.T) {
Expand All @@ -45,6 +48,9 @@ func TestGetUpdatesWithPagination(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, 2, len(actualResponse.Updates))
assert.Equal(t, 3, actualResponse.Pagination.TotalPages)
assert.Equal(t, 1, actualResponse.Pagination.Page)
assert.Equal(t, 2, actualResponse.Pagination.Size)
}

func TestGetUpdatesUnauthorizedWhenMissingAuthHeader(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions test/sorting_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func TestPaginationAndSorting(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, 2, len(actualResponse.Updates))
assert.Equal(t, 3, actualResponse.Pagination.TotalPages)
assert.Equal(t, 1, actualResponse.Pagination.Page)
assert.Equal(t, 2, actualResponse.Pagination.Size)

// Check that updates are returned in descending order by date
assert.True(t, actualResponse.Updates[0].DateTime.After(actualResponse.Updates[1].DateTime))
Expand All @@ -49,6 +52,9 @@ func TestPaginationAndSortingTwo(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, 2, len(actualResponse.Updates))
assert.Equal(t, 3, actualResponse.Pagination.TotalPages)
assert.Equal(t, 1, actualResponse.Pagination.Page)
assert.Equal(t, 2, actualResponse.Pagination.Size)

// Check that updates are returned in ascending order by person name
assert.True(t, actualResponse.Updates[0].Person.Name <= actualResponse.Updates[1].Person.Name)
Expand Down
Loading