From 90ed70ab5fa343e336590275f0769adfb67e6437 Mon Sep 17 00:00:00 2001 From: e11sy Date: Sun, 22 Oct 2023 19:49:37 +0300 Subject: [PATCH 1/6] feat(noteList): offset and limit parameters added * added offset and limit parameters for getNoteListByCreatorId func --- src/domain/service/noteList.ts | 6 ++-- src/presentation/http/router/noteList.ts | 29 +++++++++++++++++-- src/repository/note.repository.ts | 6 ++-- .../storage/postgres/orm/sequelize/note.ts | 6 +++- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 4503610d..0d5b1e48 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -23,11 +23,13 @@ export default class NoteListService { * Returns 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 { + public async getNoteListByCreatorId(id: number, offset: number, limit: number): Promise { return { - items: await this.repository.getNoteListByCreatorId(id), + items: await this.repository.getNoteListByCreatorId(id, offset, limit), }; } } diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index f880b096..55118a86 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -25,15 +25,38 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o /** * Get note list by userId */ - fastify.get('/', { + fastify.get<{ + Querystring: { + offset: number, + limit: number, + }, + }>('/', { config: { policy: [ 'authRequired', ], }, + schema: { + querystring: { + offset: { + type: 'number', + }, + limit: { + 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 offset = request.query.offset; + + /** + * if limit > 30 or limit < 1 error throws (in scedule) + */ + const limit = request.query.limit; + const noteList = await noteListService.getNoteListByCreatorId(userId, offset, limit); 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, }, From 2f3c66d5083d4df898ad10be252e1d48ee4362d6 Mon Sep 17 00:00:00 2001 From: e11sy Date: Sun, 22 Oct 2023 20:22:38 +0300 Subject: [PATCH 2/6] chore(noteList) : limit and offset replaced with page *added constant portipnSize insead of variable limit *added page *offset is now calculated by page --- src/domain/service/noteList.ts | 9 +++++++-- src/presentation/http/router/noteList.ts | 18 +++++------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 0d5b1e48..63747279 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -10,6 +10,10 @@ export default class NoteListService { */ public repository: NoteRepository; + /** + * Number of notes shown in one time + */ + private readonly portionSize = 30; /** * Note service constructor * @@ -27,9 +31,10 @@ export default class NoteListService { * @param limit - number of notes to get * @returns { Promise } note */ - public async getNoteListByCreatorId(id: number, offset: number, limit: number): Promise { + public async getNoteListByCreatorId(id: number, page: number): Promise { + const offset = (page - 1) * this.portionSize; return { - items: await this.repository.getNoteListByCreatorId(id, offset, limit), + 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 55118a86..c4bb2474 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -23,12 +23,11 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o const noteListService = opts.noteListService; /** - * Get note list by userId + * Get note list for one page by userId */ fastify.get<{ Querystring: { - offset: number, - limit: number, + page: number; }, }>('/', { config: { @@ -38,10 +37,7 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o }, schema: { querystring: { - offset: { - type: 'number', - }, - limit: { + page: { type: 'number', minimum: 1, maximum: 30, @@ -50,13 +46,9 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o }, }, async (request, reply) => { const userId = request.userId as number; - const offset = request.query.offset; + const page = request.query.page; - /** - * if limit > 30 or limit < 1 error throws (in scedule) - */ - const limit = request.query.limit; - const noteList = await noteListService.getNoteListByCreatorId(userId, offset, limit); + const noteList = await noteListService.getNoteListByCreatorId(userId, page); return reply.send(noteList); }); From 6e2947e42197b321d824f471daae78e362d8b21e Mon Sep 17 00:00:00 2001 From: e11sy Date: Sun, 22 Oct 2023 20:25:42 +0300 Subject: [PATCH 3/6] fic: comment logic parameters logic in comments fixed --- src/domain/service/noteList.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 63747279..8318ff03 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -27,8 +27,7 @@ export default class NoteListService { * Returns note list by creator id * * @param id - note creator id - * @param offset - number of skipped notes - * @param limit - number of notes to get + * @param page - number of current page * @returns { Promise } note */ public async getNoteListByCreatorId(id: number, page: number): Promise { From e78c0bb39b61b34a1623dba18ef99c138aec40da Mon Sep 17 00:00:00 2001 From: e11sy <130844513+e11sy@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:33:39 +0300 Subject: [PATCH 4/6] style fix Co-authored-by: Peter Savchenko --- src/domain/service/noteList.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 8318ff03..2e66f95d 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -14,7 +14,8 @@ export default class NoteListService { * Number of notes shown in one time */ private readonly portionSize = 30; - /** + + /** * Note service constructor * * @param repository - note repository From f3737a2cb0750d2aa0cdc0fa518cacb4ba8573e0 Mon Sep 17 00:00:00 2001 From: e11sy <130844513+e11sy@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:34:06 +0300 Subject: [PATCH 5/6] comment fix Co-authored-by: Peter Savchenko --- src/domain/service/noteList.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 2e66f95d..9d5cd7c1 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -11,7 +11,7 @@ export default class NoteListService { public repository: NoteRepository; /** - * Number of notes shown in one time + * Number of notes shown in one portion */ private readonly portionSize = 30; From 50ba09c21eaf7d21cd3a3473493621f4b356422b Mon Sep 17 00:00:00 2001 From: e11sy Date: Sun, 22 Oct 2023 20:42:00 +0300 Subject: [PATCH 6/6] chore: warning fixes lint fixes --- src/domain/service/noteList.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 8318ff03..29d5c529 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -13,7 +13,9 @@ export default class NoteListService { /** * Number of notes shown in one time */ + private readonly portionSize = 30; + /** * Note service constructor * @@ -32,6 +34,7 @@ export default class NoteListService { */ public async getNoteListByCreatorId(id: number, page: number): Promise { const offset = (page - 1) * this.portionSize; + return { items: await this.repository.getNoteListByCreatorId(id, offset, this.portionSize), };