Skip to content

Commit 25c6c66

Browse files
committed
edited route user and models
1 parent 79c3eef commit 25c6c66

File tree

5 files changed

+130
-12
lines changed

5 files changed

+130
-12
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
/node_modules
2-
/yarn.lock
3-
/package-lock.json
4-
/nodemon.json
2+
/nodemon.json
3+
/package-lock.json

api/models/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const postSchema = mongoose.Schema({
55
title: { type: String, required: true },
66
description: { type: String, required: true },
77
code: { type: String, required: true },
8-
createdBy: { type: String, required: true }
8+
createdBy: {type: Map, of: String, required: true}
99
});
1010

1111
module.exports = mongoose.model('Post', postSchema);

api/models/user.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const userSchema = mongoose.Schema({
99
unique: true,
1010
match: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
1111
},
12-
password: { type: String, required: true }
12+
password: { type: String, required: true },
13+
role: { type: String },
14+
followers: [{type: Map, of: String}],
15+
following: [{type: Map, of: String}],
16+
bio: { type: String },
1317
});
1418

1519
module.exports = mongoose.model('User', userSchema);

api/routes/post.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const checkAuth = require('../middleware/check-auth.js');
55

66
const Post = require('../models/post');
77

8-
router.get('/', (req, res, next) => {
8+
router.get('/', checkAuth, (req, res, next) => {
99
Post.find()
1010
.select('_id title description code createdBy')
1111
.exec()
@@ -26,7 +26,7 @@ router.get('/', (req, res, next) => {
2626
})
2727
);
2828
})
29-
.catch()
29+
.catch(err => res.status(500).json({ error: err }))
3030
});
3131

3232
router.post('/', checkAuth, (req, res, next) => {
@@ -57,7 +57,7 @@ router.post('/', checkAuth, (req, res, next) => {
5757
.catch(err => res.status(500).json({ error: err }))
5858
});
5959

60-
router.get('/:postId', (req, res, next) => {
60+
router.get('/:postId', checkAuth, (req, res, next) => {
6161
const id = req.params.postId;
6262
Post.findById(id)
6363
.select('_id title description code createdBy')
@@ -75,8 +75,28 @@ router.get('/:postId', (req, res, next) => {
7575
res.status(500).json({ error: err })
7676
})
7777
});
78+
79+
router.patch('/:postId', checkAuth, (req, res, next) => {
80+
const id = req.params.postId;
81+
const updateOps = {};
82+
for (const ops of req.body) {
83+
updateOps[ops.propName] = ops.value;
84+
}
85+
Post.update({ _id: id }, { $set: updateOps })
86+
.exec()
87+
.then(result => {
88+
res.status(200).json({
89+
message: 'Product updated'
90+
});
91+
})
92+
.catch(err => {
93+
res.status(500).json({
94+
error: err
95+
})
96+
})
97+
});
7898

79-
router.delete('/:postId', (req, res, next) => {
99+
router.delete('/:postId', checkAuth, (req, res, next) => {
80100
const id = req.params.postId;
81101
Post.remove({ _id: id })
82102
.exec()

api/routes/user.js

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,35 @@ const router = express.Router();
33
const mongoose = require('mongoose');
44
const bcrypt = require('bcrypt');
55
const jwt = require('jsonwebtoken');
6+
const checkAuth = require('../middleware/check-auth.js');
67

78
const 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

936
router.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

Comments
 (0)