Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ export default class NoteService {
};
}

/**
* Returns note list created by user
* @param creatorId - id of the note creator
* @param page - number of current page
* @returns list of the notes ordered by updatedAt DESC
*/
public async getMyNoteList(creatorId: User['id'], page: number): Promise<NoteList> {
const offset = (page - 1) * this.noteListPortionSize;

return {
items: await this.noteRepository.getMyNoteList(
creatorId,
offset,
this.noteListPortionSize
),
};
}

/**
* Create note relation
* @param noteId - id of the current note
Expand Down
57 changes: 56 additions & 1 deletion src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface NoteListRouterOptions {
* Note service instance
*/
noteService: NoteService;

}

/**
Expand Down Expand Up @@ -77,6 +76,62 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
return reply.send(noteListPublic);
});

/**
* Get note list created by the user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we need to introduce the separate method for that. Reuse the existing one. You can use get parameters to pass a feed type like "my"

*/
fastify.get<{
Querystring: {
page: number;
};
}>(
'/my',
{
config: {
policy: ['authRequired'],
},
schema: {
querystring: {
page: {
type: 'number',
minimum: 1,
maximum: 30,
},
},

response: {
'2xx': {
description: 'Query notelist created by user',
properties: {
items: {
id: { type: 'string' },
content: { type: 'string' },
createdAt: { type: 'string' },
creatorId: { type: 'string' },
updatedAt: { type: 'string' },
},
},
},
},
},
},
async (request, reply) => {
const userId = request.userId as number;
const page = request.query.page;

const noteList = await noteService.getMyNoteList(userId, page);
/**
* Wrapping Notelist for public use
*/
const noteListItemsPublic: NotePublic[] = noteList.items.map(definePublicNote);

const noteListPublic: NoteListPublic = {
items: noteListItemsPublic,
};

return reply.send(noteListPublic);
}
);

done();
};

Expand Down
10 changes: 10 additions & 0 deletions src/repository/note.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ export default class NoteRepository {
return await this.storage.getNoteListByUserId(id, offset, limit);
}

/**
* Gets note list created by user
* @param creatorId - id of note creator
* @param offset - number of skipped notes
* @param limit - number of notes to get
*/
public async getMyNoteList(creatorId: number, offset: number, limit: number): Promise<Note[]> {
return await this.storage.getMyNoteList(creatorId, offset, limit);
}

/**
* Get all notes based on their ids
* @param noteIds : list of note ids
Expand Down
55 changes: 52 additions & 3 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,55 @@ export default class NoteSequelizeStorage {
});
}

/**
* Gets note list created by user
* @param creatorId - id of note creator
* @param offset - number of skipped notes
* @param limit - number of notes to get
* @returns list of the notes ordered by updatedAt DESC
*/
public async getMyNoteList(creatorId: number, offset: number, limit: number): Promise<Note[]> {
if (!this.settingsModel) {
throw new Error('NoteStorage: Note settings model not initialized');
}

const reply = await this.model.findAll({
offset: offset,
limit: limit,
where: {
creatorId: creatorId,
},
order: [['updatedAt', 'DESC']],
include: [
{
model: this.settingsModel,
as: 'noteSettings',
attributes: ['cover'],
duplicating: false,
},
],
});

/**
* Convert note model data to Note entity with cover property
*/
return reply.map((note) => {
return {
id: note.id,
/**
* noteSettings is required to be, because we make join
*/
cover: note.noteSettings!.cover,
content: note.content,
updatedAt: note.updatedAt,
createdAt: note.createdAt,
publicId: note.publicId,
creatorId: note.creatorId,
tools: note.tools,
};
});
}

/**
* Gets note by id
* @param hostname - custom hostname
Expand Down Expand Up @@ -356,18 +405,18 @@ export default class NoteSequelizeStorage {
// Fetch all notes and relations in a recursive query
const query = `
WITH RECURSIVE note_tree AS (
SELECT
SELECT
n.id AS "noteId",
n.content,
n.public_id AS "publicId",
nr.parent_id AS "parentId"
FROM ${String(this.database.literal(this.tableName).val)} n
LEFT JOIN ${String(this.database.literal('note_relations').val)} nr ON n.id = nr.note_id
WHERE n.id = :startNoteId

UNION ALL

SELECT
SELECT
n.id AS "noteId",
n.content,
n.public_id AS "publicId",
Expand Down
Loading