Skip to content

Commit

Permalink
Merge pull request #93 from codex-team/noteList(offset_limit)
Browse files Browse the repository at this point in the history
feat(noteList):  added page parameter and portionSize constant
  • Loading branch information
e11sy authored Oct 22, 2023
2 parents 6128b99 + 8d49087 commit e72b453
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/domain/service/noteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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<NoteList> } note
*/
public async getNoteListByCreatorId(id: number): Promise<NoteList> {
public async getNoteListByCreatorId(id: number, page: number): Promise<NoteList> {
const offset = (page - 1) * this.portionSize;

return {
items: await this.repository.getNoteListByCreatorId(id),
items: await this.repository.getNoteListByCreatorId(id, offset, this.portionSize),
};
}
}
23 changes: 19 additions & 4 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,32 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (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);
});
Expand Down
6 changes: 4 additions & 2 deletions src/repository/note.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoteList> } note
*/
public async getNoteListByCreatorId(id: number): Promise<Note[]> {
return await this.storage.getNoteListByCreatorId(id);
public async getNoteListByCreatorId(id: number, offset: number, limit: number): Promise<Note[]> {
return await this.storage.getNoteListByCreatorId(id, offset, limit);
}
}
6 changes: 5 additions & 1 deletion src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoteList> } note
*/
public async getNoteListByCreatorId(creatorId: number): Promise<Note[]> {
public async getNoteListByCreatorId(creatorId: number, offset: number, limit: number): Promise<Note[]> {
const noteList = await this.model.findAll({
offset: offset,
limit: limit,
where: {
creatorId,
},
Expand Down

0 comments on commit e72b453

Please sign in to comment.