@@ -3,8 +3,35 @@ const router = express.Router();
33const  mongoose  =  require ( 'mongoose' ) ; 
44const  bcrypt  =  require ( 'bcrypt' ) ; 
55const  jwt  =  require ( 'jsonwebtoken' ) ; 
6+ const  checkAuth  =  require ( '../middleware/check-auth.js' ) ; 
67
78const  User  =  require ( '../models/user' ) ; 
9+ const  Post  =  require ( '../models/post' ) ; 
10+ const  {  json }  =  require ( 'body-parser' ) ; 
11+ 
12+ router . get ( "/" ,  checkAuth ,  ( req ,  res ,  next )  =>  { 
13+  var  usersData  =  [ ] 
14+ 
15+  User . find ( ) 
16+  . exec ( ) 
17+  . then ( async  docs  =>  { 
18+  const  Posts  =  await  Post . find ( ) ; 
19+ 
20+  await  docs . forEach ( async  element  =>  { 
21+  await  usersData . push ( { 
22+  _id : element . _id , 
23+  username : element . username , 
24+  role : element . role , 
25+  bio : element . bio , 
26+  followers : element . followers , 
27+  following : element . following , 
28+  posts : Posts . filter ( Post  =>  Post . createdBy . get ( 'username' )  ===  element . username ) 
29+  } ) 
30+  } ) ; 
31+  res . status ( 200 ) . json ( usersData ) ; 
32+  } ) 
33+  . catch ( err  =>  res . status ( 500 ) . json ( {  error : err  } ) ) 
34+ } ) ; 
835
936router . post ( "/signup" ,  ( req ,  res ,  next )  =>  { 
1037 User . find ( {  email : req . body . email  } ) 
@@ -25,7 +52,11 @@ router.post("/signup", (req, res, next) => {
2552 _id : new  mongoose . Types . ObjectId ( ) , 
2653 username : req . body . username , 
2754 email : req . body . email , 
28-  password : hash 
55+  password : hash , 
56+  role : 'member' , 
57+  bio : '' , 
58+  followers : [ ] , 
59+  following : [ ] 
2960 } ) ; 
3061 user 
3162 . save ( ) 
@@ -63,8 +94,9 @@ router.post("/login", (req, res, next) => {
6394 if  ( result )  { 
6495 const  token  =  jwt . sign ( 
6596 { 
97+  userId : user [ 0 ] . _id , 
6698 username : user [ 0 ] . username , 
67-  userId : user [ 0 ] . _id 
99+  role : user [ 0 ] . role 
68100 } , 
69101 process . env . PRIVATE_KEY , 
70102 { 
@@ -88,8 +120,71 @@ router.post("/login", (req, res, next) => {
88120 } ) ; 
89121} ) ; 
90122
123+ router . get ( '/:user' ,  ( req ,  res ,  next )  =>  { 
124+  var  user  =  req . params . user ; 
125+ 
126+  User . find ( {  username : user  } ) 
127+  . select ( '_id username role bio followers following' ) 
128+  . exec ( ) 
129+  . then ( async  doc  =>  { 
130+  const  Posts  =  await  Post . find ( ) ; 
131+ 
132+  res . status ( 200 ) . json ( { 
133+  _id : doc [ 0 ] . _id , 
134+  username : doc [ 0 ] . username , 
135+  role : doc [ 0 ] . role , 
136+  bio : doc [ 0 ] . bio , 
137+  followers : doc [ 0 ] . followers , 
138+  following : doc [ 0 ] . following , 
139+  posts : Posts . filter ( Post  =>  Post . createdBy . get ( 'username' )  ===  user ) 
140+  } ) 
141+  } ) 
142+  . catch ( err  =>  res . status ( 500 ) . json ( {  err } ) ) 
143+ 
144+  // User.find({ username: user }) 
145+  // .select('_id username role bio followers following') 
146+  // .exec() 
147+  // .then(async doc => { 
148+  // const Posts = await Post.find(); 
149+  // const UserData = { 
150+  // _id: doc._id, 
151+  // username: doc.username, 
152+  // role: doc.role, 
153+  // bio: doc.bio, 
154+  // followers: doc.followers, 
155+  // following: doc.following, 
156+  // posts: Posts.filter(Post => Post.createdBy.get('username') === doc.username) 
157+  // } 
158+  // console.log(UserData) 
159+  // res.status(200) 
160+ 
161+  // }) 
162+  // .catch(err => { 
163+  // res.status(500).json({ error: err }) 
164+  // }) 
165+ } ) ; 
166+ 
167+ router . patch ( '/:userId' ,  checkAuth ,  ( req ,  res ,  next )  =>  { 
168+  const  id  =  req . params . userId ; 
169+  const  updateOps  =  { } ; 
170+  for  ( const  ops  of  req . body )  { 
171+  updateOps [ ops . propName ]  =  ops . value ; 
172+  } 
173+  User . update ( {  _id : id  } ,  {  $set : updateOps  } ) 
174+  . exec ( ) 
175+  . then ( result  =>  { 
176+  res . status ( 200 ) . json ( { 
177+  message : 'User updated' 
178+  } ) ; 
179+  } ) 
180+  . catch ( err  =>  { 
181+  res . status ( 500 ) . json ( { 
182+  error : err 
183+  } ) 
184+  } ) 
185+ } ) ; 
91186
92- router . delete ( '/:userId' ,  ( req ,  res ,  next )  =>  { 
187+ router . delete ( '/:userId' ,  checkAuth ,   ( req ,  res ,  next )  =>  { 
93188 User . remove ( {  _id : req . params . userId  } ) 
94189 . exec ( ) 
95190 . then ( result  =>  res . status ( 200 ) . json ( {  message : 'User deleted'  } ) ) 
0 commit comments