-
Notifications
You must be signed in to change notification settings - Fork 6
Feature/stefan/favorites #49
base: master
Are you sure you want to change the base?
Changes from 6 commits
2678d27
5e11250
e049c54
292ef1d
6b593a8
36b256e
c6ad90c
4040e2c
211eb9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const HttpStatus = require('http-status-codes') | ||
const { | ||
mongo: { ObjectId } | ||
} = require('mongoose') | ||
|
||
const { idClaim } = require('../utils').constants | ||
|
||
exports.getFavorites = async (req, res) => { | ||
try { | ||
const share = await req.db.Share.findById(req.param.ShareId) | ||
if (share.ownerId === req.user.id || share.recivers.includes(req.user.id)) { | ||
const user = await req.user.findById(share.ownerId) | ||
return res.status(HttpStatus.OK).json({ | ||
success: true, | ||
favorites: user.favoriteCourses | ||
}) | ||
} else { | ||
return res.status(HttpStatus.Unathorized).json({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e cu litere mari UNATHORIZED |
||
success: false, | ||
message: "you don't have rights to view this" | ||
}) | ||
} | ||
} catch (e) { | ||
return res.status(HttpStatus.InternalServerError).json({ | ||
success: false, | ||
message: 'Something bad hapened!' | ||
}) | ||
} | ||
} | ||
|
||
exports.updateFavorites = async (req, res) => { | ||
try { | ||
let newFavoriteCourses = [] | ||
|
||
if (req.user) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for this if. The requireAuth middleware do that. |
||
for (const course in req.body.coursesToAdd) { | ||
newFavoriteCourses.push(course) | ||
} | ||
for (const course in req.user.favoriteCourses) { | ||
newFavoriteCourses.push(course) | ||
} | ||
newFavoriteCourses = [new Set(newFavoriteCourses)] | ||
|
||
for (const course in req.body.coursesToRemove) { | ||
newFavoriteCourses.filter((item, index) => item !== course) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not work. you should first get the user: User.findOne({_id: ObjectId(req.user[idClaim])}) |
||
|
||
await req.db.User.updateOne( | ||
{ _id: ObjectId(req.user[idClaim]) }, | ||
{ password: newFavoriteCourses } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. password...? |
||
) | ||
|
||
return res.status(HttpStatus.OK).json({ | ||
success: true, | ||
message: 'Favorite courses updated successfully' | ||
}) | ||
} else { | ||
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ | ||
success: false, | ||
message: 'Something bad happened!' | ||
}) | ||
} | ||
} catch (e) { | ||
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ | ||
success: false, | ||
message: 'Something bad happened!' | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const { Schema, model } = require('mongoose') | ||
|
||
const shareSchema = new Schema( | ||
{ | ||
owner: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You used ownerId in controller |
||
type: String, | ||
required: true | ||
}, | ||
recivers: { | ||
type: Array, | ||
default: [] | ||
} | ||
} | ||
) | ||
module.exports = model('share', shareSchema) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ const users = require('./users') | |
const auth = require('./auth') | ||
const schedule = require('./schedule') | ||
const secrets = require('./secrets') | ||
const share = require('./share') | ||
|
||
const { requireAuth } = require('../middlewares') | ||
|
||
|
@@ -15,7 +16,7 @@ router.get('/', (req, res) => { | |
}) | ||
|
||
router.use('/auth', auth) | ||
|
||
router.use('/share', share) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move after requireAuth |
||
router.use(requireAuth()) | ||
|
||
router.use('/users', users) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const router = require('express').Router() | ||
const { shareController } = require('../controllers') | ||
|
||
router.get('/', shareController.getFavorites) | ||
router.patch('/', shareController.updateFavorites) | ||
|
||
module.exports = router |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nu o sa mearga asta. Trebuie ObjectId(param.ShareId)
also use
shareId
and findOne({_id: ..}) instead.