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 users collection pagination #636

Merged
merged 12 commits into from
Feb 22, 2025
23 changes: 19 additions & 4 deletions src/principal/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,35 @@ export class PrincipalService {

}

async findAll(type: 'user'): Promise<User[]>;
async findAll(type: 'user', page: number): Promise<User[]>;
async findAll(type: 'group'): Promise<Group[]>;
async findAll(type: 'app'): Promise<App[]>;
async findAll(): Promise<Principal[]>;
async findAll(type?: PrincipalType): Promise<Principal[]> {
async findAll(type?: PrincipalType, page?: number): Promise<Principal[]> {

this.privileges.require('a12n:principals:list');
const filters: Record<string, any> = {};
if (type) {
filters.type = userTypeToInt(type);
}

const result = await db('principals')
.where(filters);
let result: PrincipalsRecord[] = [];

if(type && page !== undefined){

page = page < 1 ? 1 : page;
const pageSize = 100;
const offset = (page - 1) * pageSize;

result = await db('principals')
.where(filters)
.limit(pageSize)
.offset(offset);

} else {
result = await db('principals')
.where(filters);
}

const principals: Principal[] = [];
for (const principal of result) {
Expand Down
9 changes: 7 additions & 2 deletions src/user/controller/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class UserCollectionController extends Controller {
async get(ctx: Context) {

const principalService = new services.principal.PrincipalService(ctx.privileges);
const users = await principalService.findAll('user');

const pageInt = parseInt(ctx.request.query.page);
const page = isNaN(pageInt) ? 1 : pageInt;

const users = await principalService.findAll('user', page);
const embed = ctx.request.prefer('transclude').toString().includes('item') || ctx.query.embed?.includes('item');

const embeddedUsers: HalResource[] = [];
Expand All @@ -36,7 +40,8 @@ class UserCollectionController extends Controller {
}
}

ctx.response.body = hal.collection(users, embeddedUsers);
const pageSize = 100;
ctx.response.body = hal.collection(users, embeddedUsers, page, pageSize);

}

Expand Down
28 changes: 24 additions & 4 deletions src/user/formats/hal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ import { HalResource } from 'hal-types';
import { LazyPrivilegeBox } from '../../privilege/service.ts';
import { UserNewResult } from '../../api-types.ts';

export function collection(users: User[], embeddedUsers: HalResource[]): HalResource {
export function collection(users: User[], embeddedUsers: HalResource[], currentPage: number, pageSize: number): HalResource {

const totalUsers = users.length;
const totalPages = Math.ceil(totalUsers / pageSize);
const nextPage = currentPage < totalPages ? currentPage + 1 : null;
const prevPage = currentPage > 1 ? currentPage - 1 : null;

const startIdx = (currentPage - 1) * pageSize;
const endIdx = Math.min(startIdx + pageSize, totalUsers);

const paginatedUsers = users.slice(startIdx, endIdx);

const hal: HalResource = {
_links: {
'self': { href: '/user' },
'item': users.map( user => ({
'self': { href: `/user?page=${currentPage}` },
'item': paginatedUsers.map( user => ({
href: user.href,
title: user.nickname,
})),
Expand All @@ -20,9 +30,19 @@ export function collection(users: User[], embeddedUsers: HalResource[]): HalReso
templated: true,
},
},
total: users.length,
total: totalUsers,
currentPage,
totalPages,
};

if(nextPage){
hal._links['next'] = { href: `/user?page=${nextPage}` };
}

if(prevPage){
hal._links['previous'] = { href: `/user?page=${prevPage}` };
}

if (embeddedUsers.length) {
hal._embedded = {
item: embeddedUsers
Expand Down
Loading