1- const { default : axios } = require ( 'axios' ) ;
2- const codeRatingModel = require ( '../models/codeRatingModel' ) ;
3- const mongoose = require ( 'mongoose' ) ;
4- const fs = require ( 'fs' ) ;
1+ const { default : axios } = require ( 'axios' )
2+ const codeRatingModel = require ( '../models/codeRatingModel' )
3+ const mongoose = require ( 'mongoose' )
4+ const fs = require ( 'fs' )
5+
6+ require ( 'dotenv' ) . config ( )
57
68// function to read the code file and sends the username in callback function
79const getUserNameFromCodeBaseFile = ( fileName ) => {
@@ -10,78 +12,89 @@ const getUserNameFromCodeBaseFile = (fileName) => {
1012 if ( err ) {
1113 reject ( err )
1214 } else {
13- const lines = buf . toString ( ) . split ( "\n" ) ;
14- let username = "" ;
15+ const lines = buf . toString ( ) . split ( '\n' )
16+ let username = ''
1517 for ( let i = 0 ; i < lines . length ; i ++ ) {
1618 if ( lines [ i ] . search ( 'AUTHOR:' ) != - 1 ) {
1719 username = lines [ i ] . split ( 'AUTHOR:' ) [ 1 ] . trim ( )
18- break ;
20+ break
1921 }
2022 }
2123 fileName = fileName . split ( '/' ) [ 2 ]
22- resolve ( { username, fileName } )
24+ resolve ( { username, fileName} )
2325 }
2426 } )
2527 } )
2628}
2729
2830// function to connect to mongodb using mongoose
2931const connectMongoDB = ( ) => {
30- require ( 'dotenv' ) . config ( )
31- mongoose . connect ( process . env . MONGODB_URI , { useNewUrlParser : true , useUnifiedTopology : true , useCreateIndex : true } ) ;
32- const db = mongoose . connection ;
33- db . on ( 'error' , console . error . bind ( console , 'Connection error:' ) ) ;
34- return db ;
32+ mongoose . connect ( process . env . MONGODB_URI , {
33+ useNewUrlParser : true ,
34+ useUnifiedTopology : true ,
35+ useCreateIndex : true ,
36+ } )
37+ const db = mongoose . connection
38+ db . on ( 'error' , console . error . bind ( console , 'Connection error:' ) )
39+ return db
3540}
3641
3742// uses single call to mongo db
3843const insertDataInMongo = ( records ) => {
3944 const db = connectMongoDB ( )
4045
41- codeRatingModel . insertMany ( records , { ordered : false } )
46+ codeRatingModel
47+ . insertMany ( records , { ordered : false } )
4248 . then ( ( ) => {
43- console . log ( " Data added successfully" )
44- db . close ( ) ;
49+ console . log ( ' Data added successfully' )
50+ db . close ( )
4551 } )
4652 . catch ( ( err ) => {
47- console . log ( " Data is not added: " + err ) ;
48- db . close ( ) ;
53+ console . log ( ' Data is not added: ' + err )
54+ db . close ( )
4955 } )
5056}
5157
5258// this function handles calling the API
5359const callGithubCodeBaseAPI = ( ) => {
54- axios . get ( 'https://api.github.com/repos/TechOUs/HacktoberFest21Community/contents/CodeBase' )
55- . then ( res => {
56-
60+ axios
61+ . get (
62+ `https://api.github.com/repos/TECHOUS/${ process . env . CODE_SRC } /contents/CodeBase`
63+ )
64+ . then ( ( res ) => {
5765 // get all the names from the files
5866 const filePromises = res . data . map ( ( codeObject ) => {
59- return getUserNameFromCodeBaseFile ( `HacktoberFest21Community/${ codeObject . path } ` )
67+ return getUserNameFromCodeBaseFile (
68+ `${ process . env . CODE_SRC } /${ codeObject . path } `
69+ )
6070 } )
6171
6272 // resolve/reject all the file promises
63- Promise . allSettled ( filePromises )
64- . then ( ( fileObjects ) => {
65-
66- // creating a map of filename to usernames
67- const fileMap = new Map ( fileObjects . map ( ( fileObject ) => {
68- return [ fileObject . value . fileName , fileObject . value . username ] ;
69- } ) )
70-
71- const dataToInsert = res . data . map ( ( data ) => {
72- return {
73- codeId : data . sha ,
74- codeRating : 0 ,
75- codeUrl : data . url ,
76- codeName : data . name ,
77- userName : fileMap . get ( data . name )
78- }
73+ Promise . allSettled ( filePromises ) . then ( ( fileObjects ) => {
74+ // creating a map of filename to usernames
75+ const fileMap = new Map (
76+ fileObjects . map ( ( fileObject ) => {
77+ return [
78+ fileObject . value . fileName ,
79+ fileObject . value . username ,
80+ ]
7981 } )
82+ )
8083
81- insertDataInMongo ( dataToInsert ) ;
84+ const dataToInsert = res . data . map ( ( data ) => {
85+ return {
86+ codeId : data . sha ,
87+ codeRating : 0 ,
88+ codeUrl : data . url ,
89+ codeName : data . name ,
90+ userName : fileMap . get ( data . name ) ,
91+ }
8292 } )
93+
94+ insertDataInMongo ( dataToInsert )
95+ } )
8396 } )
84- . catch ( err => console . log ( err ) )
97+ . catch ( ( err ) => console . log ( err ) )
8598}
8699
87- callGithubCodeBaseAPI ( ) ;
100+ callGithubCodeBaseAPI ( )
0 commit comments