From 2678d27ddd340fff26b2ccffcf2a11e0759ae228 Mon Sep 17 00:00:00 2001 From: stefanT9 <46680707+stefanT9@users.noreply.github.com> Date: Fri, 24 Apr 2020 17:00:53 +0300 Subject: [PATCH 1/8] Added favoriteCourses field to the model --- models/user.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/models/user.js b/models/user.js index 5245857..2a2a4ce 100644 --- a/models/user.js +++ b/models/user.js @@ -47,6 +47,11 @@ const userSchema = new Schema( admin: { type: Boolean, default: false + }, + favoriteCourses: + { + type: Array, + default: [] } }, { timestamps: true } From 5e1125093325fd34840efc8a89fa8f0a7d927198 Mon Sep 17 00:00:00 2001 From: stefanT9 <46680707+stefanT9@users.noreply.github.com> Date: Fri, 24 Apr 2020 17:01:34 +0300 Subject: [PATCH 2/8] Added routes to the controller --- controllers/userController.js | 75 +++++++++++++++++++++++++++++++++++ routes/users.js | 3 ++ 2 files changed, 78 insertions(+) diff --git a/controllers/userController.js b/controllers/userController.js index 93339fb..5d4a742 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -190,3 +190,78 @@ exports.resetPassword = async (req, res) => { }) } } + +exports.getFavorites = async (req, res) =>{ + try{ + + if(req.user) + { + return res.status(HttpStatus.OK).json({ + success: true, + favorites: req.user.favoriteCourses + }) + } + 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!' + }) + } +} + +exports.updateFavorites = async (req, res) =>{ + try{ + const newFavoriteCourses=[] + + if(req.user) + { + for(let course in req.body.coursesToAdd) + { + newFavoriteCourses.push(course) + } + for(let course in req.user.favoriteCourses) + { + newFavoriteCourses.push(course) + } + newFavoriteCourses=[new Set(newFavoriteCourses)] + + for(let course in req.body.coursesToRemove) + { + newFavoriteCourses.filter((item,index) => item!=course) + } + + await req.db.User.updateOne( + { _id: ObjectId(req.user[idClaim]) }, + { password: newFavoriteCourses } + ) + + 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!' + }) + } +} \ No newline at end of file diff --git a/routes/users.js b/routes/users.js index 8456139..872784d 100644 --- a/routes/users.js +++ b/routes/users.js @@ -29,4 +29,7 @@ router.post( userController.resetPassword ) +router.get('/favorites', userController.getFavorites) +router.patch('/favorites', userController.getFavorites) + module.exports = router From e049c540bd24f231e396178eecd99188581c0fd8 Mon Sep 17 00:00:00 2001 From: stefanT9 <46680707+stefanT9@users.noreply.github.com> Date: Sun, 26 Apr 2020 22:19:11 +0300 Subject: [PATCH 3/8] Added share model to db --- models/index.js | 4 +++- models/shares.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 models/shares.js diff --git a/models/index.js b/models/index.js index 63ab59b..6205a31 100644 --- a/models/index.js +++ b/models/index.js @@ -5,6 +5,7 @@ const Year = require('./year') const Day = require('./day') const Course = require('./course') const Secret = require('./secret') +const Share = require('./shares') const db = { User, @@ -13,7 +14,8 @@ const db = { Year, Day, Course, - Secret + Secret, + Share } module.exports = db diff --git a/models/shares.js b/models/shares.js new file mode 100644 index 0000000..0f98d4c --- /dev/null +++ b/models/shares.js @@ -0,0 +1,16 @@ +const { Schema, model } = require('mongoose') + +const shareSchema = new Schema( + { + owner: { + type: String, + required: true + }, + recivers:{ + type: Array, + default: [] + } + } + ) + +module.exports = model('shares', shareSchema) From 292ef1d06af6d50317567358fa65034d5518043c Mon Sep 17 00:00:00 2001 From: stefanT9 <46680707+stefanT9@users.noreply.github.com> Date: Mon, 27 Apr 2020 18:20:29 +0300 Subject: [PATCH 4/8] Made controller to acces shared courses --- controllers/shareController.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 controllers/shareController.js diff --git a/controllers/shareController.js b/controllers/shareController.js new file mode 100644 index 0000000..af8f9b7 --- /dev/null +++ b/controllers/shareController.js @@ -0,0 +1,27 @@ +exports.getFavoriteCourses= async (req, req) => { + 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: req.user.favoriteCourses + }) + } + else{ + return res.status(HttpStatus.Unathorized).json({ + 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!" + }) + + } +} \ No newline at end of file From 6b593a815b32a25e4a9fc30487428cdfbe9100b8 Mon Sep 17 00:00:00 2001 From: stefanT9 Date: Mon, 4 May 2020 18:06:23 +0300 Subject: [PATCH 5/8] Moved share controllers to a different route --- controllers/index.js | 1 + controllers/shareController.js | 90 +++++++++++++++++++++++++--------- controllers/userController.js | 75 ---------------------------- models/index.js | 2 +- models/share.js | 15 ++++++ models/shares.js | 16 ------ routes/index.js | 3 +- routes/share.js | 7 +++ 8 files changed, 92 insertions(+), 117 deletions(-) create mode 100644 models/share.js delete mode 100644 models/shares.js create mode 100644 routes/share.js diff --git a/controllers/index.js b/controllers/index.js index b2c7917..81bd458 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -2,3 +2,4 @@ exports.userController = require('./userController') exports.authController = require('./authController') exports.scheduleController = require('./scheduleController') exports.secretsController = require('./secretsController') +exports.shareController = require('./shareController') diff --git a/controllers/shareController.js b/controllers/shareController.js index af8f9b7..4dab11f 100644 --- a/controllers/shareController.js +++ b/controllers/shareController.js @@ -1,27 +1,69 @@ -exports.getFavoriteCourses= async (req, req) => { - 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: req.user.favoriteCourses - }) - } - else{ - return res.status(HttpStatus.Unathorized).json({ - success: false, - message: "you don't have rights to view this" - }) - } +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({ + 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!" - }) + } 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) { + 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) + } + + await req.db.User.updateOne( + { _id: ObjectId(req.user[idClaim]) }, + { password: newFavoriteCourses } + ) + 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!' + }) } -} \ No newline at end of file + } catch (e) { + return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ + success: false, + message: 'Something bad happened!' + }) + } +} diff --git a/controllers/userController.js b/controllers/userController.js index 5d4a742..93339fb 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -190,78 +190,3 @@ exports.resetPassword = async (req, res) => { }) } } - -exports.getFavorites = async (req, res) =>{ - try{ - - if(req.user) - { - return res.status(HttpStatus.OK).json({ - success: true, - favorites: req.user.favoriteCourses - }) - } - 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!' - }) - } -} - -exports.updateFavorites = async (req, res) =>{ - try{ - const newFavoriteCourses=[] - - if(req.user) - { - for(let course in req.body.coursesToAdd) - { - newFavoriteCourses.push(course) - } - for(let course in req.user.favoriteCourses) - { - newFavoriteCourses.push(course) - } - newFavoriteCourses=[new Set(newFavoriteCourses)] - - for(let course in req.body.coursesToRemove) - { - newFavoriteCourses.filter((item,index) => item!=course) - } - - await req.db.User.updateOne( - { _id: ObjectId(req.user[idClaim]) }, - { password: newFavoriteCourses } - ) - - 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!' - }) - } -} \ No newline at end of file diff --git a/models/index.js b/models/index.js index 6205a31..e7d0cce 100644 --- a/models/index.js +++ b/models/index.js @@ -5,7 +5,7 @@ const Year = require('./year') const Day = require('./day') const Course = require('./course') const Secret = require('./secret') -const Share = require('./shares') +const Share = require('./share') const db = { User, diff --git a/models/share.js b/models/share.js new file mode 100644 index 0000000..e44c7f3 --- /dev/null +++ b/models/share.js @@ -0,0 +1,15 @@ +const { Schema, model } = require('mongoose') + +const shareSchema = new Schema( + { + owner: { + type: String, + required: true + }, + recivers: { + type: Array, + default: [] + } + } +) +module.exports = model('share', shareSchema) diff --git a/models/shares.js b/models/shares.js deleted file mode 100644 index 0f98d4c..0000000 --- a/models/shares.js +++ /dev/null @@ -1,16 +0,0 @@ -const { Schema, model } = require('mongoose') - -const shareSchema = new Schema( - { - owner: { - type: String, - required: true - }, - recivers:{ - type: Array, - default: [] - } - } - ) - -module.exports = model('shares', shareSchema) diff --git a/routes/index.js b/routes/index.js index 7286c2e..3bd0589 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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) router.use(requireAuth()) router.use('/users', users) diff --git a/routes/share.js b/routes/share.js new file mode 100644 index 0000000..4467dd7 --- /dev/null +++ b/routes/share.js @@ -0,0 +1,7 @@ +const router = require('express').Router() +const { shareController } = require('../controllers') + +router.get('/', shareController.getFavorites) +router.patch('/', shareController.updateFavorites) + +module.exports = router From 36b256eb3f4d465ae1b0a54b27dfbafd84414129 Mon Sep 17 00:00:00 2001 From: stefanT9 Date: Mon, 4 May 2020 18:12:17 +0300 Subject: [PATCH 6/8] Deleted leftover code --- routes/users.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/routes/users.js b/routes/users.js index 872784d..8456139 100644 --- a/routes/users.js +++ b/routes/users.js @@ -29,7 +29,4 @@ router.post( userController.resetPassword ) -router.get('/favorites', userController.getFavorites) -router.patch('/favorites', userController.getFavorites) - module.exports = router From c6ad90ca8e4533372dc06d11aece20b678010046 Mon Sep 17 00:00:00 2001 From: stefanT9 Date: Mon, 4 May 2020 18:40:14 +0300 Subject: [PATCH 7/8] Added route to update recivers --- controllers/shareController.js | 44 ++++++++++++++++++++++++++++++++-- routes/share.js | 4 ++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/controllers/shareController.js b/controllers/shareController.js index 4dab11f..5fc70fe 100644 --- a/controllers/shareController.js +++ b/controllers/shareController.js @@ -55,9 +55,49 @@ exports.updateFavorites = async (req, res) => { message: 'Favorite courses updated successfully' }) } else { - return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ + return res.status(HttpStatus.Unathorized).json({ + success: false, + message: 'You need to be loged in!' + }) + } + } catch (e) { + return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ + success: false, + message: 'Something bad happened!' + }) + } +} + +exports.updateRecivers = async (req, res) => { + try { + let newRecivers = [] + + if (req.user) { + for (const reciverId in req.body.reciverToAdd) { + newRecivers.push(reciverId) + } + for (const reciverId in req.share.recivers) { + newRecivers.push(reciverId) + } + newRecivers = [new Set(newRecivers)] + + for (const reciverId in req.body.reciverToRemove) { + newRecivers.filter((item, index) => item !== reciverId) + } + const share = req.db.Share.find({ ownerId: req.user.id }) + await req.db.Share.updateOne( + { _id: share._id }, + { recivers: newRecivers } + ) + + return res.status(HttpStatus.OK).json({ + success: true, + message: 'Share recivers updated successfully' + }) + } else { + return res.status(HttpStatus.Unathorized).json({ success: false, - message: 'Something bad happened!' + message: 'You need to be loged in!' }) } } catch (e) { diff --git a/routes/share.js b/routes/share.js index 4467dd7..b60024c 100644 --- a/routes/share.js +++ b/routes/share.js @@ -2,6 +2,6 @@ const router = require('express').Router() const { shareController } = require('../controllers') router.get('/', shareController.getFavorites) -router.patch('/', shareController.updateFavorites) - +router.patch('/favoriteCourses', shareController.updateFavorites) +router.patch('/shareRecivers', shareController.updateRecivers) module.exports = router From 211eb9f677f47d53c65a95d970b0ce19caa780e6 Mon Sep 17 00:00:00 2001 From: stefanT9 Date: Tue, 12 May 2020 19:48:07 +0300 Subject: [PATCH 8/8] fixed some errors --- controllers/shareController.js | 103 +++++++++++++++------------------ models/share.js | 2 +- routes/index.js | 2 +- 3 files changed, 49 insertions(+), 58 deletions(-) diff --git a/controllers/shareController.js b/controllers/shareController.js index 5fc70fe..4c7ac27 100644 --- a/controllers/shareController.js +++ b/controllers/shareController.js @@ -7,21 +7,23 @@ 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) + var user = await req.db.User.findOne({ _id: ObjectId(req.user[idClaim]) }) + const share = await req.db.Share.findOne({ _id: ObjectId(req.query.shareId) }) + + if (share.ownerId === user._id || share.recivers.includes(user._id)) { + const user = await req.db.User.findOne({ _id: share.ownerId }) return res.status(HttpStatus.OK).json({ success: true, favorites: user.favoriteCourses }) } else { - return res.status(HttpStatus.Unathorized).json({ + return res.status(HttpStatus.UNATHORIZED).json({ success: false, message: "you don't have rights to view this" }) } } catch (e) { - return res.status(HttpStatus.InternalServerError).json({ + return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ success: false, message: 'Something bad hapened!' }) @@ -30,36 +32,30 @@ exports.getFavorites = async (req, res) => { exports.updateFavorites = async (req, res) => { try { + var user = await req.db.User.findOne({ _id: ObjectId(req.user[idClaim]) }) let newFavoriteCourses = [] - if (req.user) { - 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.coursesToAdd) { + newFavoriteCourses.push(course) + } + for (const course in user.favoriteCourses) { + newFavoriteCourses.push(course) + } + newFavoriteCourses = [new Set(newFavoriteCourses)] - for (const course in req.body.coursesToRemove) { - newFavoriteCourses.filter((item, index) => item !== course) - } + for (const course in req.body.coursesToRemove) { + newFavoriteCourses.filter((item, index) => item !== course) + } - await req.db.User.updateOne( - { _id: ObjectId(req.user[idClaim]) }, - { password: newFavoriteCourses } - ) + await req.db.User.updateOne( + { _id: ObjectId(req.user[idClaim]) }, + { favorites: newFavoriteCourses } + ) - return res.status(HttpStatus.OK).json({ - success: true, - message: 'Favorite courses updated successfully' - }) - } else { - return res.status(HttpStatus.Unathorized).json({ - success: false, - message: 'You need to be loged in!' - }) - } + return res.status(HttpStatus.OK).json({ + success: true, + message: 'Favorite courses updated successfully' + }) } catch (e) { return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ success: false, @@ -70,36 +66,31 @@ exports.updateFavorites = async (req, res) => { exports.updateRecivers = async (req, res) => { try { - let newRecivers = [] + var user = await req.db.User.findOne({ _id: ObjectId(req.user[idClaim]) }) + const share = await req.db.Share.find({ ownerId: user._id }) - if (req.user) { - for (const reciverId in req.body.reciverToAdd) { - newRecivers.push(reciverId) - } - for (const reciverId in req.share.recivers) { - newRecivers.push(reciverId) - } - newRecivers = [new Set(newRecivers)] + let newRecivers = [] - for (const reciverId in req.body.reciverToRemove) { - newRecivers.filter((item, index) => item !== reciverId) - } - const share = req.db.Share.find({ ownerId: req.user.id }) - await req.db.Share.updateOne( - { _id: share._id }, - { recivers: newRecivers } - ) + for (const reciverId in req.body.reciverToAdd) { + newRecivers.push(reciverId) + } + for (const reciverId in req.share.recivers) { + newRecivers.push(reciverId) + } + newRecivers = [new Set(newRecivers)] - return res.status(HttpStatus.OK).json({ - success: true, - message: 'Share recivers updated successfully' - }) - } else { - return res.status(HttpStatus.Unathorized).json({ - success: false, - message: 'You need to be loged in!' - }) + for (const reciverId in req.body.reciverToRemove) { + newRecivers.filter((item, index) => item !== reciverId) } + await req.Share.updateOne( + { _id: share._id }, + { recivers: newRecivers } + ) + + return res.status(HttpStatus.OK).json({ + success: true, + message: 'Share recivers updated successfully' + }) } catch (e) { return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ success: false, diff --git a/models/share.js b/models/share.js index e44c7f3..0022cfb 100644 --- a/models/share.js +++ b/models/share.js @@ -2,7 +2,7 @@ const { Schema, model } = require('mongoose') const shareSchema = new Schema( { - owner: { + ownerId: { type: String, required: true }, diff --git a/routes/index.js b/routes/index.js index 3bd0589..3b9b1a7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -16,9 +16,9 @@ router.get('/', (req, res) => { }) router.use('/auth', auth) -router.use('/share', share) router.use(requireAuth()) +router.use('/share', share) router.use('/users', users) router.use('/schedule', schedule) router.use('/secrets', secrets)