@@ -22,6 +22,49 @@ interface NoteListRouterOptions {
2222const 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} ;
0 commit comments