Skip to content

Commit

Permalink
added two GET endpoints
Browse files Browse the repository at this point in the history
- added GET '/note/:noteId/history'
- added GET '/note/:noteId/history/:historyId'
  • Loading branch information
e11sy committed Jul 20, 2024
1 parent e91696b commit 241aec1
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { type NotePublic, definePublicNote } from '@domain/entities/notePublic.j
import type NoteVisitsService from '@domain/service/noteVisits.js';
import type EditorToolsService from '@domain/service/editorTools.js';
import type EditorTool from '@domain/entities/editorTools.js';
import type { NoteHistoryMeta, NoteHistoryRecord } from '@domain/entities/noteHistory.js';

/**
* Interface for the note router.
Expand Down Expand Up @@ -647,6 +648,78 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
});
});

/**
* Get note history preview
*/
fastify.get<{
Params: {
notePublicId: NotePublicId;
};
Reply: {
noteHistoryMeta: NoteHistoryMeta[];
} | ErrorResponse;
}>('/:notePublicId/history', {
config: {
policy: [
'authRequired',
'userCanEdit',
],
},
preHandler: [
noteResolver,
],
}, async (request, reply) => {
const { note } = request;
const noteId = request.note?.id as number;

if (note === null) {
return reply.notFound('Note not found');
}

return reply.send({
noteHistoryMeta: await noteService.getNoteHistoryByNoteId(noteId),
});
});

/**
* Get note history record
*/
fastify.get<{
Params: {
notePublicId: NotePublicId;
historyId: NoteHistoryRecord['id'];
};
Reply: {
noteHistoryRecord: NoteHistoryRecord;
} | ErrorResponse;
}>('/:notePublicId/history/:historyId', {
config: {
policy: [
'authRequired',
'userCanEdit',
],
},
preHandler: [
noteResolver,
],
}, async (request, reply) => {
const { note } = request;
const historyId = request.params.historyId;

/**
* Check if note exists
*/
if (note === null) {
return reply.notFound('Note not found');
}

const historyRecord = await noteService.getHistoryResordById(historyId);

return reply.send({
noteHistoryRecord: historyRecord,
});
});

done();
};

Expand Down

0 comments on commit 241aec1

Please sign in to comment.