diff --git a/model/updateModels.go b/model/updateModels.go index 1fc2ecc..de2b0ba 100644 --- a/model/updateModels.go +++ b/model/updateModels.go @@ -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 { diff --git a/repository/updateRepository.go b/repository/updateRepository.go index f6a73ae..25613d4 100644 --- a/repository/updateRepository.go +++ b/repository/updateRepository.go @@ -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) } @@ -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)) @@ -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) @@ -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) { diff --git a/service/updateService.go b/service/updateService.go index b9d2890..2654839 100644 --- a/service/updateService.go +++ b/service/updateService.go @@ -3,6 +3,7 @@ package service import ( "context" "encoding/json" + "math" "net/http" "time" @@ -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) @@ -91,13 +92,16 @@ func (service UpdateServiceImpl) GetUpdates(ctx context.Context, catalogId strin } if databaseUpdates == nil { - logrus.Error("No updates found") + logrus.Error("No concept 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") + logrus.Info("Returning concept 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 } } diff --git a/test/get_updates_test.go b/test/get_updates_test.go index 542fa7b..a9d5bc2 100644 --- a/test/get_updates_test.go +++ b/test/get_updates_test.go @@ -27,6 +27,9 @@ func TestGetUpdates(t *testing.T) { assert.Nil(t, err) assert.True(t, len(actualResponse.Updates) > 0) + assert.True(t, actualResponse.Pagination.TotalPages == 1) + assert.True(t, actualResponse.Pagination.Page == 1) + assert.True(t, actualResponse.Pagination.Size == 10) } func TestGetUpdatesWithPagination(t *testing.T) { @@ -45,6 +48,9 @@ func TestGetUpdatesWithPagination(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 2, len(actualResponse.Updates)) + assert.True(t, actualResponse.Pagination.TotalPages == 2) + assert.True(t, actualResponse.Pagination.Page == 1) + assert.True(t, actualResponse.Pagination.Size == 2) } func TestGetUpdatesUnauthorizedWhenMissingAuthHeader(t *testing.T) {