Skip to content

Commit

Permalink
modified user's api
Browse files Browse the repository at this point in the history
  • Loading branch information
Rupeshiya committed Aug 6, 2020
1 parent 8f92e6b commit 0477cfe
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ module.exports = {

getAllEventByUser: async (req, res, next) => {
try {
const events = await Event.find({ createdBy: req.user._id }, {}, helper.paginate(req))
const { id } = req.params
const events = await Event.find({ createdBy: id }, {}, helper.paginate(req))
.sort({ eventDate: -1 })
.populate('createdBy', '_id name.firstName name.lastName')
.exec()
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ module.exports = {
getPostByUser: async (req, res, next) => {
try {
const posts = await PostModel.find(
{ userId: req.user._id },
{ userId: req.params.id },
{},
helper.paginate(req)
)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module.exports = {
},
projectCreatedByUser: async (req, res, next) => {
try {
const { id } = req.user
const { id } = req.params
const projects = await Project.find({ createdBy: id }, {}, helper.paginate(req))
.populate('createdBy', '_id name.firstName name.lastName email')
.sort({ updatedAt: -1 })
Expand Down
33 changes: 29 additions & 4 deletions app/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,30 @@ module.exports = {
// GET USER PROFILE
userProfile: async (req, res, next) => {
try {
const user = req.user
const { id } = req.params
const user = await User.findById({ _id: id })
.populate('followings', [
'name.firstName',
'name.lastName',
'info.about.designation',
'_id',
'isAdmin'
])
.populate('followers', [
'name.firstName',
'name.lastName',
'info.about.designation',
'_id',
'isAdmin'
])
.populate('blocked', [
'name.firstName',
'name.lastName',
'info.about.designation',
'_id',
'isAdmin'
])
.exec()
if (!user) {
return res.status(HttpStatus.NOT_FOUND).json({ msg: 'No such user exist!' })
}
Expand Down Expand Up @@ -81,11 +104,13 @@ module.exports = {
}

try {
const { id } = req.params
const user = await User.findById(id)
updates.forEach((update) => {
req.user[update] = req.body[update]
user[update] = req.body[update]
})
await req.user.save()
return res.status(HttpStatus.OK).json({ data: req.user })
await user.save()
return res.status(HttpStatus.OK).json({ data: user })
} catch (error) {
return res.status(HttpStatus.BAD_REQUEST).json({ error })
}
Expand Down
2 changes: 1 addition & 1 deletion app/routes/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ router.delete(

// GET ALL EVENT POSTED BY A USER
router.get(
'/me/all',
'/:id/all',
isUnderMaintenance,
auth,
eventController.getAllEventByUser
Expand Down
2 changes: 1 addition & 1 deletion app/routes/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ router.patch(

// GET POST PER USER
router.get(
'/me/all',
'/:id/all',
auth,
postController.getPostByUser
)
Expand Down
2 changes: 1 addition & 1 deletion app/routes/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ router.delete(

// GET PROJECTS CREATED BY A USER
router.get(
'/me/all',
'/:id/all',
isUnderMaintenance,
auth,
projectController.projectCreatedByUser
Expand Down
6 changes: 3 additions & 3 deletions app/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ router.post(

// get user profile
router.get(
'/me',
'/:id',
isUnderMaintenance,
auth,
userController.userProfile
)

// update user info
router.patch(
'/me',
'/:id',
isUnderMaintenance,
auth,
userController.userProfileUpdate
Expand Down Expand Up @@ -116,7 +116,7 @@ router.patch(

// GET PERSONAL OVERVIEW
router.get(
'/overview',
'/me/overview',
isUnderMaintenance,
auth,
userController.getPersonalOverview
Expand Down

0 comments on commit 0477cfe

Please sign in to comment.