Skip to content

Commit 9ddb319

Browse files
committed
refactor: reuse existing code
1 parent f348312 commit 9ddb319

File tree

4 files changed

+97
-194
lines changed

4 files changed

+97
-194
lines changed

src/domain/service/note.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -225,34 +225,17 @@ export default class NoteService {
225225
}
226226

227227
/**
228-
* Returns note list by creator id
228+
* Returns note list by user id
229229
* @param userId - id of the user
230230
* @param page - number of current page
231-
* @returns list of the notes ordered by time of last visit
231+
* @param filterByCreator - if true, returns only notes created by user, otherwise returns notes visited by user
232+
* @returns list of the notes
232233
*/
233-
public async getNoteListByUserId(userId: User['id'], page: number): Promise<NoteList> {
234+
public async getNoteListByUserId(userId: User['id'], page: number, filterByCreator = false): Promise<NoteList> {
234235
const offset = (page - 1) * this.noteListPortionSize;
235236

236237
return {
237-
items: await this.noteRepository.getNoteListByUserId(userId, offset, this.noteListPortionSize),
238-
};
239-
}
240-
241-
/**
242-
* Returns note list created by user
243-
* @param creatorId - id of the note creator
244-
* @param page - number of current page
245-
* @returns list of the notes ordered by updatedAt DESC
246-
*/
247-
public async getMyNoteList(creatorId: User['id'], page: number): Promise<NoteList> {
248-
const offset = (page - 1) * this.noteListPortionSize;
249-
250-
return {
251-
items: await this.noteRepository.getMyNoteList(
252-
creatorId,
253-
offset,
254-
this.noteListPortionSize
255-
),
238+
items: await this.noteRepository.getNoteListByUserId(userId, offset, this.noteListPortionSize, filterByCreator),
256239
};
257240
}
258241

src/presentation/http/router/noteList.ts

Lines changed: 50 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,49 @@ interface NoteListRouterOptions {
2222
const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, opts, done) => {
2323
const noteService = opts.noteService;
2424

25+
const noteListSchema = {
26+
querystring: {
27+
page: {
28+
type: 'number',
29+
minimum: 1,
30+
maximum: 30,
31+
},
32+
},
33+
response: {
34+
'2xx': {
35+
description: 'Query notelist',
36+
properties: {
37+
items: {
38+
id: { type: 'string' },
39+
content: { type: 'string' },
40+
createdAt: { type: 'string' },
41+
creatorId: { type: 'string' },
42+
updatedAt: { type: 'string' },
43+
},
44+
},
45+
},
46+
},
47+
};
48+
49+
const createNoteListHandler = (filterByCreator: boolean) => {
50+
return async (request: { userId: number | null; query: { page: number } }, reply: { send: (data: NoteListPublic) => void }): Promise<void> => {
51+
const userId = request.userId as number;
52+
const page = request.query.page;
53+
54+
const noteList = await noteService.getNoteListByUserId(userId, page, filterByCreator);
55+
/**
56+
* Wrapping Notelist for public use
57+
*/
58+
const noteListItemsPublic: NotePublic[] = noteList.items.map(definePublicNote);
59+
60+
const noteListPublic: NoteListPublic = {
61+
items: noteListItemsPublic,
62+
};
63+
64+
return reply.send(noteListPublic);
65+
};
66+
};
67+
2568
/**
2669
* Get note list ordered by time of last visit
2770
*/
@@ -35,46 +78,8 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
3578
'authRequired',
3679
],
3780
},
38-
schema: {
39-
querystring: {
40-
page: {
41-
type: 'number',
42-
minimum: 1,
43-
maximum: 30,
44-
},
45-
},
46-
47-
response: {
48-
'2xx': {
49-
description: 'Query notelist',
50-
properties: {
51-
items: {
52-
id: { type: 'string' },
53-
content: { type: 'string' },
54-
createdAt: { type: 'string' },
55-
creatorId: { type: 'string' },
56-
updatedAt: { type: 'string' },
57-
},
58-
},
59-
},
60-
},
61-
},
62-
}, async (request, reply) => {
63-
const userId = request.userId as number;
64-
const page = request.query.page;
65-
66-
const noteList = await noteService.getNoteListByUserId(userId, page);
67-
/**
68-
* Wrapping Notelist for public use
69-
*/
70-
const noteListItemsPublic: NotePublic[] = noteList.items.map(definePublicNote);
71-
72-
const noteListPublic: NoteListPublic = {
73-
items: noteListItemsPublic,
74-
};
75-
76-
return reply.send(noteListPublic);
77-
});
81+
schema: noteListSchema,
82+
}, createNoteListHandler(false));
7883

7984
/**
8085
* Get note list created by the user
@@ -83,54 +88,12 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o
8388
Querystring: {
8489
page: number;
8590
};
86-
}>(
87-
'/my',
88-
{
89-
config: {
90-
policy: ['authRequired'],
91-
},
92-
schema: {
93-
querystring: {
94-
page: {
95-
type: 'number',
96-
minimum: 1,
97-
maximum: 30,
98-
},
99-
},
100-
101-
response: {
102-
'2xx': {
103-
description: 'Query notelist created by user',
104-
properties: {
105-
items: {
106-
id: { type: 'string' },
107-
content: { type: 'string' },
108-
createdAt: { type: 'string' },
109-
creatorId: { type: 'string' },
110-
updatedAt: { type: 'string' },
111-
},
112-
},
113-
},
114-
},
115-
},
91+
}>('/my', {
92+
config: {
93+
policy: ['authRequired'],
11694
},
117-
async (request, reply) => {
118-
const userId = request.userId as number;
119-
const page = request.query.page;
120-
121-
const noteList = await noteService.getMyNoteList(userId, page);
122-
/**
123-
* Wrapping Notelist for public use
124-
*/
125-
const noteListItemsPublic: NotePublic[] = noteList.items.map(definePublicNote);
126-
127-
const noteListPublic: NoteListPublic = {
128-
items: noteListItemsPublic,
129-
};
130-
131-
return reply.send(noteListPublic);
132-
}
133-
);
95+
schema: noteListSchema,
96+
}, createNoteListHandler(true));
13497

13598
done();
13699
};

src/repository/note.repository.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,14 @@ export default class NoteRepository {
7373
}
7474

7575
/**
76-
* Gets note list by creator id
77-
* @param id - note creator id
76+
* Gets note list by user id
77+
* @param id - user id
7878
* @param offset - number of skipped notes
7979
* @param limit - number of notes to get
80+
* @param filterByCreator - if true, returns only notes created by user, otherwise returns notes visited by user
8081
*/
81-
public async getNoteListByUserId(id: number, offset: number, limit: number): Promise<Note[]> {
82-
return await this.storage.getNoteListByUserId(id, offset, limit);
83-
}
84-
85-
/**
86-
* Gets note list created by user
87-
* @param creatorId - id of note creator
88-
* @param offset - number of skipped notes
89-
* @param limit - number of notes to get
90-
*/
91-
public async getMyNoteList(creatorId: number, offset: number, limit: number): Promise<Note[]> {
92-
return await this.storage.getMyNoteList(creatorId, offset, limit);
82+
public async getNoteListByUserId(id: number, offset: number, limit: number, filterByCreator = false): Promise<Note[]> {
83+
return await this.storage.getNoteListByUserId(id, offset, limit, filterByCreator);
9384
}
9485

9586
/**

src/repository/storage/postgres/orm/sequelize/note.ts

Lines changed: 37 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -224,94 +224,60 @@ export default class NoteSequelizeStorage {
224224
}
225225

226226
/**
227-
* Gets note list by creator id
227+
* Gets note list by user id
228228
* @param userId - id of certain user
229229
* @param offset - number of skipped notes
230230
* @param limit - number of notes to get
231+
* @param filterByCreator - if true, returns only notes created by user, otherwise returns notes visited by user
231232
* @returns list of the notes
232233
*/
233-
public async getNoteListByUserId(userId: number, offset: number, limit: number): Promise<Note[]> {
234-
if (this.visitsModel === null) {
235-
throw new Error('NoteStorage: NoteVisit model should be defined');
236-
}
237-
234+
public async getNoteListByUserId(userId: number, offset: number, limit: number, filterByCreator = false): Promise<Note[]> {
238235
if (!this.settingsModel) {
239236
throw new Error('NoteStorage: Note settings model not initialized');
240237
}
241238

242-
const reply = await this.model.findAll({
243-
offset: offset,
244-
limit: limit,
245-
where: {
246-
'$noteVisits.user_id$': userId,
247-
},
248-
order: [[
249-
{
250-
model: this.visitsModel,
251-
as: 'noteVisits',
252-
},
253-
'visited_at',
254-
'DESC',
255-
]],
256-
include: [{
257-
model: this.visitsModel,
258-
as: 'noteVisits',
259-
duplicating: false,
260-
}, {
239+
if (!filterByCreator && this.visitsModel === null) {
240+
throw new Error('NoteStorage: NoteVisit model should be defined');
241+
}
242+
243+
const where: Record<string, unknown> = filterByCreator
244+
? { creatorId: userId }
245+
: { '$noteVisits.user_id$': userId };
246+
247+
const order = filterByCreator
248+
? [['updatedAt', 'DESC']]
249+
: [[
250+
{
251+
model: this.visitsModel!,
252+
as: 'noteVisits',
253+
},
254+
'visited_at',
255+
'DESC',
256+
]];
257+
258+
const include: Array<Record<string, unknown>> = [
259+
{
261260
model: this.settingsModel,
262261
as: 'noteSettings',
263262
attributes: ['cover'],
264263
duplicating: false,
265-
}],
266-
});
267-
268-
/**
269-
* Convert note model data to Note entity with cover property
270-
*/
271-
return reply.map((note) => {
272-
return {
273-
id: note.id,
274-
/**
275-
* noteSettings is required to be, because we make join
276-
*/
277-
cover: note.noteSettings!.cover,
278-
content: note.content,
279-
updatedAt: note.updatedAt,
280-
createdAt: note.createdAt,
281-
publicId: note.publicId,
282-
creatorId: note.creatorId,
283-
tools: note.tools,
284-
};
285-
});
286-
}
264+
},
265+
];
287266

288-
/**
289-
* Gets note list created by user
290-
* @param creatorId - id of note creator
291-
* @param offset - number of skipped notes
292-
* @param limit - number of notes to get
293-
* @returns list of the notes ordered by updatedAt DESC
294-
*/
295-
public async getMyNoteList(creatorId: number, offset: number, limit: number): Promise<Note[]> {
296-
if (!this.settingsModel) {
297-
throw new Error('NoteStorage: Note settings model not initialized');
267+
if (!filterByCreator) {
268+
include.unshift({
269+
model: this.visitsModel!,
270+
as: 'noteVisits',
271+
duplicating: false,
272+
});
298273
}
299274

300275
const reply = await this.model.findAll({
301-
offset: offset,
302-
limit: limit,
303-
where: {
304-
creatorId: creatorId,
305-
},
306-
order: [['updatedAt', 'DESC']],
307-
include: [
308-
{
309-
model: this.settingsModel,
310-
as: 'noteSettings',
311-
attributes: ['cover'],
312-
duplicating: false,
313-
},
314-
],
276+
offset,
277+
limit,
278+
where,
279+
order: order as never,
280+
include,
315281
});
316282

317283
/**

0 commit comments

Comments
 (0)