@@ -9,16 +9,18 @@ const {
99 getCodeBaseFileForUser,
1010 generateAndSaveToken,
1111 getCodeBaseFilesRating,
12- } = require ( '../../src/engine' )
13- const { authenticateAPI} = require ( '../../src/auth' )
12+ } = require ( '../../../src/engine' )
1413const {
1514 checkRandomCodesCacheExpiry,
1615 getRandomCodesFromCache,
1716 setRandomCodesInCache,
1817 checkSearchUserCacheExpiry,
1918 getSearchUserFromCache,
20- setSearchUserInCache
21- } = require ( '../../src/cache' )
19+ setSearchUserInCache,
20+ } = require ( '../../../src/cache' )
21+ // import middlewares
22+ const { authenticateAPI} = require ( '../../../src/auth' )
23+ const { updateValidator, searchValidator} = require ( '../../../src/validator' )
2224
2325/**
2426 * @endpoint /api/v1/randomCodes
@@ -122,51 +124,35 @@ router.get('/randomCodes', async (req, res) => {
122124 * "message": ""
123125 * }
124126 **/
125- router . put ( '/rateCode' , authenticateAPI , async ( req , res ) => {
127+ router . put ( '/rateCode' , authenticateAPI , updateValidator , async ( req , res ) => {
126128 const { codeId1, codeId2, winner} = req . body
127129
128- if (
129- codeId1 === undefined ||
130- codeId2 === undefined ||
131- winner === undefined
132- ) {
133- res . status ( 400 ) . json ( {
134- status : 400 ,
135- message : 'Invalid request parameters !!' ,
136- } )
137- } else if ( winner !== 1 && winner !== 2 ) {
138- res . status ( 400 ) . json ( {
130+ const codeBaseFiles = await getCodeBaseFilesRating ( codeId1 , codeId2 )
131+ let codeRating1 = 0
132+ let codeRating2 = 0
133+ codeBaseFiles . map ( ( codeBaseFile ) => {
134+ if ( codeBaseFile . codeId === codeId1 ) {
135+ codeRating1 = codeBaseFile . codeRating
136+ } else if ( codeBaseFile . codeId === codeId2 ) {
137+ codeRating2 = codeBaseFile . codeRating
138+ }
139+ } )
140+ const updateResult = await rateCodeAndUpdate ( {
141+ codeId1,
142+ codeId2,
143+ codeRating1,
144+ codeRating2,
145+ winner,
146+ } ) . catch ( ( err ) => {
147+ return res . status ( 400 ) . json ( {
139148 status : 400 ,
140- message : 'Winner can either be 1 or 2 only' ,
141- } )
142- } else {
143- const codeBaseFiles = await getCodeBaseFilesRating ( codeId1 , codeId2 )
144- let codeRating1 = 0
145- let codeRating2 = 0
146- codeBaseFiles . map ( ( codeBaseFile ) => {
147- if ( codeBaseFile . codeId === codeId1 ) {
148- codeRating1 = codeBaseFile . codeRating
149- } else if ( codeBaseFile . codeId === codeId2 ) {
150- codeRating2 = codeBaseFile . codeRating
151- }
152- } )
153- const updateResult = await rateCodeAndUpdate ( {
154- codeId1,
155- codeId2,
156- codeRating1,
157- codeRating2,
158- winner,
159- } ) . catch ( ( err ) => {
160- return res . status ( 400 ) . json ( {
161- status : 400 ,
162- message : 'Bad Request!!!' ,
163- } )
149+ message : 'Bad Request!!!' ,
164150 } )
151+ } )
165152
166- updateResult . status = 200
167- updateResult . message = 'Code Ratings are updated'
168- res . status ( 200 ) . json ( updateResult )
169- }
153+ updateResult . status = 200
154+ updateResult . message = 'Code Ratings are updated'
155+ res . status ( 200 ) . json ( updateResult )
170156} )
171157
172158/**
@@ -187,15 +173,13 @@ router.put('/rateCode', authenticateAPI, async (req, res) => {
187173 * userCodeBaseFiles: []
188174 * }
189175 **/
190- router . get ( '/searchUser' , authenticateAPI , async ( req , res ) => {
191- const { username, sendContent} = req . query
176+ router . get (
177+ '/searchUser' ,
178+ authenticateAPI ,
179+ searchValidator ,
180+ async ( req , res ) => {
181+ const { username, sendContent} = req . query
192182
193- if ( username === undefined ) {
194- res . status ( 400 ) . json ( {
195- status : 400 ,
196- message : 'Bad Request !! Please send username in the query' ,
197- } )
198- } else {
199183 // send the cache data according to username and sendContent
200184 if ( checkSearchUserCacheExpiry ( username , sendContent ) ) {
201185 return res . status ( 200 ) . json ( getSearchUserFromCache ( ) )
@@ -251,6 +235,6 @@ router.get('/searchUser', authenticateAPI, async (req, res) => {
251235 res . status ( 200 ) . json ( getSearchUserFromCache ( ) )
252236 }
253237 }
254- } )
238+ )
255239
256240module . exports = router
0 commit comments