From 738d49f1afad3cd75c2520a3696f12cd5754bb07 Mon Sep 17 00:00:00 2001 From: nocnoc_e Date: Fri, 17 Feb 2023 16:36:29 -0500 Subject: [PATCH 1/3] stop here --- app/models/comment.js | 36 +++++++++++++++++++++++ app/routes/comment_routes.js | 55 ++++++++++++++++++++++++++++++++++++ server.js | 2 ++ 3 files changed, 93 insertions(+) create mode 100644 app/models/comment.js create mode 100644 app/routes/comment_routes.js diff --git a/app/models/comment.js b/app/models/comment.js new file mode 100644 index 0000000..1940872 --- /dev/null +++ b/app/models/comment.js @@ -0,0 +1,36 @@ +const mongoose = require('mongoose') +const Schema = mongoose.Schema + + +const commentSchema = new mongoose.Schema({ + body: { + type: String, + required: true + }, + createdAt: { + type: Date, + default: Date.now + }, + userId: { + type: Schema.Types.ObjectId, + ref: 'User' + }, + parentId: { + type: Schema.Types.ObjectId, + ref: 'Comment' + }, + username: [{ + type: Schema.Types.ObjectId, + ref: 'Comment' + }] +}) + +module.exports = mongoose.model('Comment', commentSchema) +// +// // Seed comments +// +// const seed = async () => { +// const users = await User.find(); +// const comments = [ +// { +// text: 'This is a comment', \ No newline at end of file diff --git a/app/routes/comment_routes.js b/app/routes/comment_routes.js new file mode 100644 index 0000000..ce1c0c3 --- /dev/null +++ b/app/routes/comment_routes.js @@ -0,0 +1,55 @@ +const express = require('express') +const router = express.Router() +const Comment = require('../models/comment') + +// Create Comment +router.post('/comments', async (req, res) => { + try { + const comment = await Comment.create(req.body) + res.status(201).json({ comment }) + } catch (error) { + console.log(error) + res.status(500).json({ error: error.message }) + } +}) + +// Get all comments +router.get('/comments', async (req, res) => { + try { + const comments = await Comment.find() + res.status(200).json({ comments }) + } catch (error) { + res.status(500).send(error.message) + } +}) + +// Get one comment +router.get('/comments/:id', async (req, res) => { + try { + const { id } = req.params + const comment = await Comment.findById(id) + if (comment) { + return res.status(200).json({ comment }) + } + res.status(404).send('Comment with the specified ID does not exists') + } catch (error) { + res.status(500).send(error.message) + } +}) + +// Delete comment +router.delete('/comments/:id', async (req, res) => { + try { + const { id } = req.params + const deleted = await Comment.findByIdAndDelete(id) + if (deleted) { + return res.status(200).send('Comment deleted') + } + throw new Error('Comment not found') + } catch (error) { + res.status(500).send(error.message) + } +}) + + +module.exports = router \ No newline at end of file diff --git a/server.js b/server.js index 796b57d..db458a0 100644 --- a/server.js +++ b/server.js @@ -7,6 +7,7 @@ const cors = require('cors') const followCartRoutes = require('./app/routes/followCart_routes') const userRoutes = require('./app/routes/user_routes') const contentRouter = require('./app/routes/content_routes') +const commentRoutes = require('./app/routes/comment_routes') // require middleware const errorHandler = require('./lib/error_handler') @@ -66,6 +67,7 @@ app.use(express.urlencoded({ extended: true })) app.use(requestLogger) // register route files +app.use('/comments', commentRoutes) app.use('/content', contentRouter) app.use(followCartRoutes) app.use(userRoutes) From a4195846f61a2a4290490e0c592fa4b74000d774 Mon Sep 17 00:00:00 2001 From: nocnoc_e Date: Sun, 19 Feb 2023 18:37:19 -0500 Subject: [PATCH 2/3] added update and delete --- app/models/comment.js | 47 +++++--------- app/routes/comment_routes.js | 120 +++++++++++++++++++++-------------- server.js | 2 +- 3 files changed, 89 insertions(+), 80 deletions(-) diff --git a/app/models/comment.js b/app/models/comment.js index 1940872..651ae76 100644 --- a/app/models/comment.js +++ b/app/models/comment.js @@ -1,36 +1,21 @@ +//////////////// COMMENT SCHEMA //////// + const mongoose = require('mongoose') const Schema = mongoose.Schema - -const commentSchema = new mongoose.Schema({ - body: { - type: String, - required: true - }, - createdAt: { - type: Date, - default: Date.now - }, - userId: { +// const commentSchema = require('./comments') +const commentSchema = new mongoose.Schema( + { + owner: { type: Schema.Types.ObjectId, - ref: 'User' - }, - parentId: { - type: Schema.Types.ObjectId, - ref: 'Comment' - }, - username: [{ - type: Schema.Types.ObjectId, - ref: 'Comment' - }] -}) + ref: 'User', + required: true + }, + usercomment: { + type: String, + + } + } + ) -module.exports = mongoose.model('Comment', commentSchema) -// -// // Seed comments -// -// const seed = async () => { -// const users = await User.find(); -// const comments = [ -// { -// text: 'This is a comment', \ No newline at end of file + module.exports = mongoose.model('Comment', commentSchema) \ No newline at end of file diff --git a/app/routes/comment_routes.js b/app/routes/comment_routes.js index ce1c0c3..d6aabdb 100644 --- a/app/routes/comment_routes.js +++ b/app/routes/comment_routes.js @@ -1,55 +1,79 @@ -const express = require('express') -const router = express.Router() -const Comment = require('../models/comment') - -// Create Comment -router.post('/comments', async (req, res) => { - try { - const comment = await Comment.create(req.body) - res.status(201).json({ comment }) - } catch (error) { - console.log(error) - res.status(500).json({ error: error.message }) - } -}) +////////////// Comment Routes ///////// +const express = require('express') +const passport = require('passport') + +const customErrors = require('../../lib/custom_errors') +const handle404 = customErrors.handle404 +const requireOwnership = customErrors.requireOwnership +const errors = require('../../lib/custom_errors') +const removeBlanks = require('../../lib/remove_blank_fields') +const requireToken = passport.authenticate('bearer', { session: false }) +const router = express.Router() +const mongoose = require('mongoose') +const Comment = require(('../models/comment')) + + -// Get all comments -router.get('/comments', async (req, res) => { - try { - const comments = await Comment.find() - res.status(200).json({ comments }) - } catch (error) { - res.status(500).send(error.message) - } -}) -// Get one comment -router.get('/comments/:id', async (req, res) => { - try { - const { id } = req.params - const comment = await Comment.findById(id) - if (comment) { - return res.status(200).json({ comment }) - } - res.status(404).send('Comment with the specified ID does not exists') - } catch (error) { - res.status(500).send(error.message) - } +// ---------------- GET INDEX ---------------- +// ROUTES -> /comments/ + +router.get('/', (req, res, next)=> { + Comment.find({}) + .then(handle404) + .then(comment=> { + res.json({comment: comment}) + }) + .catch(next) }) -// Delete comment -router.delete('/comments/:id', async (req, res) => { - try { - const { id } = req.params - const deleted = await Comment.findByIdAndDelete(id) - if (deleted) { - return res.status(200).send('Comment deleted') - } - throw new Error('Comment not found') - } catch (error) { - res.status(500).send(error.message) - } +// ---------------- Create Comment ---------------- +// ROUTES -> /comments/:userId + +router.post(`/:userId`, (req,res, next)=>{ + const userId = req.params.userId + Comment.create({ + owner: userId, + usercomment: req.body.usercomment + }) + .then(handle404) + .then(comment=> { + console.log(`CREATED A NEW COMMENT`, comment) + res.json({comment: comment}) + }) + .catch(next) }) +//------------ UPDATE Comment ------------------ +// PATCH ROUTES -> /comments/:userId + +router.patch(`/:userId`, (req,res, next)=>{ + const userId = req.params.userId + Comment.findOneAndUpdate({owner: userId}, { + usercomment: req.body.usercomment + }) + .then(handle404) + .then(comment=> { + console.log(`UPDATED A NEW COMMENT`, comment) + res.json({comment: comment}) + }) + .catch(next) +} +) + +//------------ Delete Comment ------------------ +// Delete ROUTES -> /comments/:userId + +router.delete(`/:userId`, (req,res, next)=>{ + const userId = req.params.userId + Comment.findOneAndDelete({owner: userId}) + .then(handle404) + .then(comment=> { + console.log(`DELETED A NEW COMMENT`, comment) + res.json({comment: comment}) + }) + .catch(next) +} +) -module.exports = router \ No newline at end of file +module.exports = router \ No newline at end of file diff --git a/server.js b/server.js index db458a0..379263b 100644 --- a/server.js +++ b/server.js @@ -67,9 +67,9 @@ app.use(express.urlencoded({ extended: true })) app.use(requestLogger) // register route files -app.use('/comments', commentRoutes) app.use('/content', contentRouter) app.use(followCartRoutes) +app.use('comments', commentRoutes) app.use(userRoutes) From bf30938e6afcec940e66e6e5d9241cd03f422c7d Mon Sep 17 00:00:00 2001 From: nocnoc_e Date: Sun, 19 Feb 2023 19:25:32 -0500 Subject: [PATCH 3/3] need to update and delete --- app/routes/comment_routes.js | 52 +++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/app/routes/comment_routes.js b/app/routes/comment_routes.js index d6aabdb..d728da3 100644 --- a/app/routes/comment_routes.js +++ b/app/routes/comment_routes.js @@ -47,33 +47,37 @@ router.post(`/:userId`, (req,res, next)=>{ //------------ UPDATE Comment ------------------ // PATCH ROUTES -> /comments/:userId -router.patch(`/:userId`, (req,res, next)=>{ - const userId = req.params.userId - Comment.findOneAndUpdate({owner: userId}, { - usercomment: req.body.usercomment - }) - .then(handle404) - .then(comment=> { - console.log(`UPDATED A NEW COMMENT`, comment) - res.json({comment: comment}) - }) - .catch(next) -} -) +router.patch('/:commentId', requireToken, removeBlanks, (req, res, next) => { + console.log('Comment id: ', req.params.commentId) + console.log('req.usercomment: ', req.usercomment) + Comment.findById(req.params.commentId) // pass the id of target content + .then(handle404) + .then((comment) => { + console.log('this is the content: ', comment) + requireOwnership(req, comment) // check if user owns content + // returned updated value of content + return comment.updateOne(req.body.comment) + }) + .then(() => + res.sendStatus(204)) // successful 204 on updated + .catch(next) + }) //------------ Delete Comment ------------------ // Delete ROUTES -> /comments/:userId -router.delete(`/:userId`, (req,res, next)=>{ - const userId = req.params.userId - Comment.findOneAndDelete({owner: userId}) - .then(handle404) - .then(comment=> { - console.log(`DELETED A NEW COMMENT`, comment) - res.json({comment: comment}) - }) - .catch(next) -} -) +router.delete('/delete/:id', requireToken, (req, res, next) => { + // Need userId to match the owner of the content + Comment.findById(req.params.id) + .then(handle404) + .then((comment) => { + // when content is passed, check if token matches that of loggedIn user token + // if matched, take content.id, and delete from the database + requireOwnership(req, comment) + comment.deleteOne() + }) + .then(() => res.sendStatus(204)) // send a 204 on successful delete + .catch(next) + }) module.exports = router \ No newline at end of file