Skip to content
Open
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
26 changes: 24 additions & 2 deletions src/domain/entities/notePublic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Note } from '@domain/entities/note.js';
import type { Note, NoteContent } from '@domain/entities/note.js';

type NotePublicProperties = 'content' | 'createdAt' | 'updatedAt' | 'creatorId' | 'cover';

Expand All @@ -10,7 +10,7 @@ export interface NotePublic extends Pick<Note, NotePublicProperties> {
}

/**
*Change Note to NotePublic
* Change Note to NotePublic
* @param note - Note data to compose a public note
*/
export function definePublicNote(note: Note): NotePublic {
Expand All @@ -25,3 +25,25 @@ export function definePublicNote(note: Note): NotePublic {

return notePublic;
}

/**
* Change Note to a trimmed NotePublic for list responses.
* Content only includes the first block as a preview.
* @param note - Note data to compose a note list item
*/
export function defineNoteListItem(note: Note): NotePublic {
const trimmedContent: NoteContent = {
blocks: note.content.blocks.slice(0, 1),
};

const notePublic: NotePublic = {
id: note.publicId,
content: trimmedContent,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
creatorId: note.creatorId,
cover: note.cover,
};

return notePublic;
}
10 changes: 5 additions & 5 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FastifyPluginCallback } from 'fastify';
import type NoteService from '@domain/service/note.js';
import { definePublicNote, type NotePublic } from '@domain/entities/notePublic.js';
import { defineNoteListItem, type NotePublic } from '@domain/entities/notePublic.js';
import type { NoteListPublic } from '@domain/entities/noteList.js';

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
properties: {
items: {
id: { type: 'string' },
content: { type: 'string' },
content: { type: 'object' },
createdAt: { type: 'string' },
creatorId: { type: 'string' },
updatedAt: { type: 'string' },
Expand All @@ -68,7 +68,7 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
/**
* Wrapping Notelist for public use
*/
const noteListItemsPublic: NotePublic[] = noteList.items.map(definePublicNote);
const noteListItemsPublic: NotePublic[] = noteList.items.map(defineNoteListItem);

const noteListPublic: NoteListPublic = {
items: noteListItemsPublic,
Expand Down Expand Up @@ -105,7 +105,7 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
properties: {
items: {
id: { type: 'string' },
content: { type: 'string' },
content: { type: 'object' },
createdAt: { type: 'string' },
creatorId: { type: 'string' },
updatedAt: { type: 'string' },
Expand All @@ -122,7 +122,7 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
/**
* Wrapping Notelist for public use
*/
const noteListItemsPublic: NotePublic[] = noteList.items.map(definePublicNote);
const noteListItemsPublic: NotePublic[] = noteList.items.map(defineNoteListItem);

const noteListPublic: NoteListPublic = {
items: noteListItemsPublic,
Expand Down
Loading