func TestGetOffsetForPage(t *testing.T) { var offset int // First page offset should be zero offset = pagination.GetOffsetForPage( 10, // count 1, // page 2, // limit ) assert.Equal(t, 0, offset) // Second page offset should be 2 offset = pagination.GetOffsetForPage( 10, // count 2, // page 2, // limit ) assert.Equal(t, 2, offset) // Last page offset should be 8 offset = pagination.GetOffsetForPage( 10, // count 5, // page 2, // limit ) assert.Equal(t, 8, offset) }
// Handles calls to list user accounts // GET /v1/users func (s *Service) listUsersHandler(w http.ResponseWriter, r *http.Request) { // Get the authenticated user from the request context authenticatedUser, err := GetAuthenticatedUser(r) if err != nil { response.UnauthorizedError(w, err.Error()) return } // Check permissions if err = checkListUsersPermissions(authenticatedUser); err != nil { response.Error(w, err.Error(), http.StatusForbidden) return } // Get pagination params page, limit, sorts, err := pagination.GetParams(r, []string{"id"}) if err != nil { response.Error(w, err.Error(), http.StatusBadRequest) return } // Count total number of results count, err := s.PaginatedUsersCount() if err != nil { response.Error(w, err.Error(), http.StatusInternalServerError) return } // Get pagination links first, last, previous, next, err := pagination.GetLinks(r.URL, count, page, limit) if err != nil { response.Error(w, err.Error(), http.StatusBadRequest) return } // Get paginated results users, err := s.FindPaginatedUsers( pagination.GetOffsetForPage(count, page, limit), limit, sorts, ) if err != nil { response.Error(w, err.Error(), http.StatusInternalServerError) return } // Create response self := util.GetCurrentURL(r) listUsersResponse, err := NewListUsersResponse( count, page, self, first, last, previous, next, users, ) if err != nil { response.Error(w, err.Error(), http.StatusInternalServerError) return } // Write JSON response response.WriteJSON(w, listUsersResponse, http.StatusOK) }