Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Feature/stefan/favorites #49

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ exports.userController = require('./userController')
exports.authController = require('./authController')
exports.scheduleController = require('./scheduleController')
exports.secretsController = require('./secretsController')
exports.shareController = require('./shareController')
69 changes: 69 additions & 0 deletions controllers/shareController.js
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)
Copy link
Member

@procateodor procateodor May 5, 2020

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.

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({
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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)
}
Copy link
Member

Choose a reason for hiding this comment

The 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])})
after add the new courses user.favoriteCourses = new Set(...user.favoriteCourses, ...body.coursesToAdd)
after filter them user.favoriteCourses.filter(course => coursesToRemove.includes(course))


await req.db.User.updateOne(
{ _id: ObjectId(req.user[idClaim]) },
{ password: newFavoriteCourses }
Copy link
Member

Choose a reason for hiding this comment

The 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!'
})
}
}
4 changes: 3 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Year = require('./year')
const Day = require('./day')
const Course = require('./course')
const Secret = require('./secret')
const Share = require('./share')

const db = {
User,
Expand All @@ -13,7 +14,8 @@ const db = {
Year,
Day,
Course,
Secret
Secret,
Share
}

module.exports = db
15 changes: 15 additions & 0 deletions models/share.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { Schema, model } = require('mongoose')

const shareSchema = new Schema(
{
owner: {
Copy link
Member

@procateodor procateodor May 5, 2020

Choose a reason for hiding this comment

The 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)
5 changes: 5 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const userSchema = new Schema(
admin: {
type: Boolean,
default: false
},
favoriteCourses:
{
type: Array,
default: []
}
},
{ timestamps: true }
Expand Down
3 changes: 2 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -15,7 +16,7 @@ router.get('/', (req, res) => {
})

router.use('/auth', auth)

router.use('/share', share)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move after requireAuth

router.use(requireAuth())

router.use('/users', users)
Expand Down
7 changes: 7 additions & 0 deletions routes/share.js
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