diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 4503610d..486e5f34 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -10,6 +10,12 @@ export default class NoteListService { */ public repository: NoteRepository; + /** + * Number of notes shown in one portion + */ + + private readonly portionSize = 30; + /** * Note service constructor * @@ -23,11 +29,14 @@ export default class NoteListService { * Returns note list by creator id * * @param id - note creator id + * @param page - number of current page * @returns { Promise } note */ - public async getNoteListByCreatorId(id: number): Promise { + public async getNoteListByCreatorId(id: number, page: number): Promise { + const offset = (page - 1) * this.portionSize; + return { - items: await this.repository.getNoteListByCreatorId(id), + items: await this.repository.getNoteListByCreatorId(id, offset, this.portionSize), }; } } diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index f880b096..c4bb2474 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -23,17 +23,32 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o const noteListService = opts.noteListService; /** - * Get note list by userId + * Get note list for one page by userId */ - fastify.get('/', { + fastify.get<{ + Querystring: { + page: number; + }, + }>('/', { config: { policy: [ 'authRequired', ], }, + schema: { + querystring: { + page: { + type: 'number', + minimum: 1, + maximum: 30, + }, + }, + }, }, async (request, reply) => { - const { userId } = request; - const noteList = await noteListService.getNoteListByCreatorId(userId as number); + const userId = request.userId as number; + const page = request.query.page; + + const noteList = await noteListService.getNoteListByCreatorId(userId, page); return reply.send(noteList); }); diff --git a/src/repository/note.repository.ts b/src/repository/note.repository.ts index 50a92cb0..e2baa164 100644 --- a/src/repository/note.repository.ts +++ b/src/repository/note.repository.ts @@ -83,9 +83,11 @@ export default class NoteRepository { * Gets note list by creator id * * @param id - note creator id + * @param offset - number of skipped notes + * @param limit - number of notes to get * @returns { Promise } note */ - public async getNoteListByCreatorId(id: number): Promise { - return await this.storage.getNoteListByCreatorId(id); + public async getNoteListByCreatorId(id: number, offset: number, limit: number): Promise { + return await this.storage.getNoteListByCreatorId(id, offset, limit); } } diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index f33ec9b1..42e24449 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -208,10 +208,14 @@ export default class NoteSequelizeStorage { * Gets note list by creator id * * @param creatorId - note creator id + * @param offset - number of skipped notes + * @param limit - number of notes to get * @returns { Promise } note */ - public async getNoteListByCreatorId(creatorId: number): Promise { + public async getNoteListByCreatorId(creatorId: number, offset: number, limit: number): Promise { const noteList = await this.model.findAll({ + offset: offset, + limit: limit, where: { creatorId, },