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(noteList): added page parameter and portionSize constant #93

Merged
merged 7 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

/**
e11sy marked this conversation as resolved.
Show resolved Hide resolved
* 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