From 04bab0c97ecf498cee785c968165105da7cacd6a Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Mon, 16 Sep 2024 18:00:00 +0200 Subject: [PATCH] member pagination --- src/controller/admin/memberController.ts | 11 +++++++-- src/service/memberService.ts | 30 +++++++++--------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/controller/admin/memberController.ts b/src/controller/admin/memberController.ts index efc5011..0f1ea37 100644 --- a/src/controller/admin/memberController.ts +++ b/src/controller/admin/memberController.ts @@ -19,9 +19,16 @@ import CommunicationFactory from "../../factory/admin/communication"; * @returns {Promise<*>} */ export async function getAllMembers(req: Request, res: Response): Promise { - let members = await MemberService.getAll(); + let offset = parseInt((req.query.offset as string) ?? "0"); + let count = parseInt((req.query.count as string) ?? "25"); + let [members, total] = await MemberService.getAll(offset, count); - res.json(MemberFactory.mapToBase(members)); + res.json({ + members: MemberFactory.mapToBase(members), + total: total, + offset: offset, + count: count, + }); } /** diff --git a/src/service/memberService.ts b/src/service/memberService.ts index 1d41059..7d6d8e7 100644 --- a/src/service/memberService.ts +++ b/src/service/memberService.ts @@ -6,9 +6,9 @@ import InternalException from "../exceptions/internalException"; export default abstract class MemberService { /** * @description get all members - * @returns {Promise>} + * @returns {Promise<[Array, number]>} */ - static async getAll(): Promise> { + static async getAll(offset: number = 0, count: number = 25): Promise<[Array, number]> { return await dataSource .getRepository(member) .createQueryBuilder("member") @@ -24,7 +24,9 @@ export default abstract class MemberService { "membership_last", "membership_last.memberId = member.id AND membership_last.start = (SELECT MAX(m.start) FROM membership m WHERE m.memberId = member.id)" ) - .getMany() + .offset(offset) + .limit(count) + .getManyAndCount() .then((res) => { return res; }) @@ -44,25 +46,15 @@ export default abstract class MemberService { .createQueryBuilder("member") .leftJoinAndMapOne( "member.firstMembershipEntry", - (subQuery) => { - return subQuery - .from(membership, "membership") - .where("membership.memberId = member.id") - .orderBy("membership.date", "ASC") - .limit(1); - }, - "membership_first" + "member.memberships", + "membership_first", + "membership_first.memberId = member.id AND membership_first.start = (SELECT MIN(m.start) FROM membership m WHERE m.memberId = member.id)" ) .leftJoinAndMapOne( "member.lastMembershipEntry", - (subQuery) => { - return subQuery - .from(membership, "membership") - .where("membership.memberId = member.id") - .orderBy("membership.date", "DESC") - .limit(1); - }, - "membership_last" + "member.memberships", + "membership_last", + "membership_last.memberId = member.id AND membership_last.start = (SELECT MAX(m.start) FROM membership m WHERE m.memberId = member.id)" ) .where("member.id = :id", { id: id }) .getOneOrFail()