From d8e0be8a00b34e881b0545215ca06996b011abab Mon Sep 17 00:00:00 2001 From: Imon Ovbude Date: Wed, 24 Apr 2019 10:15:07 -0700 Subject: [PATCH 1/2] added insert deleted post and get deleted post routes to readme --- README.md | 99 ++++++++++ backend/routes/postsRouter.js | 336 ++++++++++++++++------------------ 2 files changed, 256 insertions(+), 179 deletions(-) diff --git a/README.md b/README.md index 6f15fa0..ead27fe 100644 --- a/README.md +++ b/README.md @@ -355,4 +355,103 @@ Returns the new Team Members list: "role": "member" } ] +``` + +### Get All Users With User Permissions /moderators + +- **GET** + +Returns all users list: +```javascript +[ + { + "id": 42, + "username": "Aaliyah.Swaniawski", + "email": "Ethelyn.Runolfsson89@hotmail.com", + "status": "active", + "user_permissions": "basic" + } +] +``` + +### Get an Individual User From The Users Table With User permissions /moderators/:id + +- **GET** + +Expects params:id + +Returns the user: +```javascript +[ + { + "id": 401, + "username": "james", + "email": "james@example.com", + "status": "active", + "user_permissions": "moderator" + } +] +``` + + +### Change User Permissions To Moderator /moderators/changeToMod/:user_id + +- **GET** + +Expects params:user_id + +Returns 1 + +### Change User Permissions To Basic /moderators/changeToBasic/:user_id + +- **GET** + +Expects params:user_id + +Returns 1 + +### Insert Deleted Post and Moderator Who Deleted The Post /posts/insert-deleted-post/:user_id + +- **POST** + +Expects params:user_id +Expects postBody, id: +```javascript + [ + { + id: 46, + body: 'Im an old man that used to love the twilight zone, I think a modern revamp is necessary to incorporate modern pro + } +] +``` + +Returns +```javascript +Result { + command: 'INSERT', + rowCount: 1, + oid: 0, + rows: [], + fields: [], + _parsers: [], + RowCtor: null, + rowAsArray: false, + _getTypeParser: [Function: bound ] + } +``` + +### Get Deleted Post /posts/get-deleted-post + +- **GET** + +Returns +```javascript +[ + { + "id": 1, + "post": "Im an old man that used to love the twilight zone, I think a modern revamp is necessary to incorporate modern problems.", + "post_id": 46, + "username": "imon" + } +] ``` \ No newline at end of file diff --git a/backend/routes/postsRouter.js b/backend/routes/postsRouter.js index 1e43e3b..75d1f8c 100644 --- a/backend/routes/postsRouter.js +++ b/backend/routes/postsRouter.js @@ -5,18 +5,10 @@ require('dotenv').config(); const express = require('express'); const fileUpload = require('express-fileupload'); const Jimp = require('jimp'); -const { - postsDB, - discussionFollowsDB, - userNotificationsDB, - teamsDB -} = require('../db/models/index.js'); +const { postsDB, discussionFollowsDB, userNotificationsDB, teamsDB } = require('../db/models/index.js'); const router = express.Router(); -const { - maxNumOfNotifications, - allowedAvatarTypes -} = require('../config/globals.js'); +const { maxNumOfNotifications, allowedAvatarTypes } = require('../config/globals.js'); const pusher = require('../config/pusherConfig.js'); /*************************************************************************************************** @@ -30,210 +22,196 @@ const { checkRole } = require('../config/middleware/helpers.js'); **************************************************************************************************/ router.get('/search', (req, res) => { - const searchText = req.get('searchText'); - let order = req.get('order'); - let orderType = req.get('orderType'); - if (order === 'undefined') order = null; - if (orderType === 'undefined') orderType = null; - if (!searchText) return res.status(200).json([]); - return postsDB - .search(searchText, order, orderType) - .then(results => { - const newRes = results.filter(res => res.isPrivate !== true); - res.status(200).json(newRes); - }) - .catch(err => - res.status(500).json({ error: `Failed to search(): ${err}` }) - ); + const searchText = req.get('searchText'); + let order = req.get('order'); + let orderType = req.get('orderType'); + if (order === 'undefined') order = null; + if (orderType === 'undefined') orderType = null; + if (!searchText) return res.status(200).json([]); + return postsDB + .search(searchText, order, orderType) + .then((results) => { + const newRes = results.filter((res) => res.isPrivate !== true); + res.status(200).json(newRes); + }) + .catch((err) => res.status(500).json({ error: `Failed to search(): ${err}` })); }); // create a post by a given user_id to a given discussion_id router.post('/:user_id', authenticate, checkRole, (req, res) => { - const { user_id } = req.params; - const { discussion_id, postBody, repliedPostID } = req.body; - const created_at = Date.now(); - // if (!postBody) - // return res.status(400).json({ error: 'Post body must not be empty.' }); - const newPost = { user_id, discussion_id, body: postBody, created_at }; - if (repliedPostID) newPost.reply_to = repliedPostID; - return postsDB - .insert(newPost) - .then(async newId => { - const discFollowers = await discussionFollowsDB.getFollowers( - discussion_id - ); - discFollowers.forEach(async user => { - const newNotification = { - user_id: user.user_id, - discussion_id, - post_id: newId[0], - created_at - }; - const notifications = await userNotificationsDB.getCount(user.user_id); - if (parseInt(notifications.count) >= maxNumOfNotifications) { - await userNotificationsDB.removeOldest(user.user_id); - } - await userNotificationsDB.add(newNotification); - pusher.trigger(`user-${user.uuid}`, 'notification', null); - }); - return res.status(201).json(newId); - }) - .catch(err => - res.status(500).json({ error: `Failed to insert(): ${err}` }) - ); + const { user_id } = req.params; + const { discussion_id, postBody, repliedPostID } = req.body; + const created_at = Date.now(); + // if (!postBody) + // return res.status(400).json({ error: 'Post body must not be empty.' }); + const newPost = { user_id, discussion_id, body: postBody, created_at }; + if (repliedPostID) newPost.reply_to = repliedPostID; + return postsDB + .insert(newPost) + .then(async (newId) => { + const discFollowers = await discussionFollowsDB.getFollowers(discussion_id); + discFollowers.forEach(async (user) => { + const newNotification = { + user_id: user.user_id, + discussion_id, + post_id: newId[0], + created_at + }; + const notifications = await userNotificationsDB.getCount(user.user_id); + if (parseInt(notifications.count) >= maxNumOfNotifications) { + await userNotificationsDB.removeOldest(user.user_id); + } + await userNotificationsDB.add(newNotification); + pusher.trigger(`user-${user.uuid}`, 'notification', null); + }); + return res.status(201).json(newId); + }) + .catch((err) => res.status(500).json({ error: `Failed to insert(): ${err}` })); }); // edit post with given post id router.put('/:user_id', authenticate, (req, res) => { - const { post_id, postBody } = req.body; - const last_edited_at = Date.now(); - const post = { body: postBody, last_edited_at }; - if (!postBody) - return res.status(400).json({ error: 'Post body must not be empty.' }); - if (!post_id) return res.status(400).json({ error: 'Post ID is required.' }); - return postsDB - .update(post_id, post) - .then(() => res.status(201).json({ message: 'Post update successful.' })) - .catch(err => - res.status(500).json({ error: `Failed to update(): ${err}` }) - ); + const { post_id, postBody } = req.body; + const last_edited_at = Date.now(); + const post = { body: postBody, last_edited_at }; + if (!postBody) return res.status(400).json({ error: 'Post body must not be empty.' }); + if (!post_id) return res.status(400).json({ error: 'Post ID is required.' }); + return postsDB + .update(post_id, post) + .then(() => res.status(201).json({ message: 'Post update successful.' })) + .catch((err) => res.status(500).json({ error: `Failed to update(): ${err}` })); }); // remove post with given post id router.delete('/:user_id/:post_id', authenticate, (req, res) => { - const { post_id } = req.params; - const post = req.body; - console.log('post', post) - if (!post_id) return res.status(400).json({ error: 'Post ID is required.' }); - return postsDB - .remove(post_id, post) - .then(() => { - res.status(201).json({ message: 'Post removal successful.' }) - }) - .catch(err => - res.status(500).json({ error: `Failed to remove(): ${err}` }) - ); + const { post_id } = req.params; + const post = req.body; + console.log('post', post); + if (!post_id) return res.status(400).json({ error: 'Post ID is required.' }); + return postsDB + .remove(post_id, post) + .then(() => { + res.status(201).json({ message: 'Post removal successful.' }); + }) + .catch((err) => res.status(500).json({ error: `Failed to remove(): ${err}` })); }); // get the images for a Post router.get('/images/:user_id/:post_id', async (req, res) => { - const { post_id } = req.params; + const { post_id } = req.params; - try { - const images = await postsDB.getPostImagesByPostId(post_id); + try { + const images = await postsDB.getPostImagesByPostId(post_id); - res.status(200).json(images); - } catch (err) { - res.status(500).json({ error: `Unable to getPostImagesByPostId:${err}` }); - } + res.status(200).json(images); + } catch (err) { + res.status(500).json({ error: `Unable to getPostImagesByPostId:${err}` }); + } }); // Upload image for a Post router.post('/images/:user_id', fileUpload(), async (req, res) => { - const post_image = req.body; - if (!req.files || Object.keys(req.files).length === 0) { - return res.status(400).json({ error: 'No files were uploaded.' }); - } - - const imageFile = req.files.imageFile; - const imageBuffer = imageFile.data; - const mimeType = imageFile.mimetype; - if (!allowedAvatarTypes.includes(mimeType)) { - return res.status(401).json({ - error: `${mimeType.replace( - 'image/', - '' - )} is not an allowed avatar type. It must be a jpeg, jpg, png, bmp, or tiff.` - }); - } - - try { - const cImage = await Jimp.read(imageBuffer).then(image => { - return image - .getBase64(Jimp.AUTO, (err, convertedImage) => { - if (err) throw err; - return (post_image.image = convertedImage); - }); - }); - const image = await postsDB.addImage(post_image); - res.status(201).json(image); - } catch (err) { - res.status(500).json({ error: `Unable to addImage():${err}` }); - } + const post_image = req.body; + if (!req.files || Object.keys(req.files).length === 0) { + return res.status(400).json({ error: 'No files were uploaded.' }); + } + + const imageFile = req.files.imageFile; + const imageBuffer = imageFile.data; + const mimeType = imageFile.mimetype; + if (!allowedAvatarTypes.includes(mimeType)) { + return res.status(401).json({ + error: `${mimeType.replace( + 'image/', + '' + )} is not an allowed avatar type. It must be a jpeg, jpg, png, bmp, or tiff.` + }); + } + + try { + const cImage = await Jimp.read(imageBuffer).then((image) => { + return image.getBase64(Jimp.AUTO, (err, convertedImage) => { + if (err) throw err; + return (post_image.image = convertedImage); + }); + }); + const image = await postsDB.addImage(post_image); + res.status(201).json(image); + } catch (err) { + res.status(500).json({ error: `Unable to addImage():${err}` }); + } }); //Update the image with the Post it will be attached to router.put('/images/:user_id', async (req, res) => { - const { image_id, post_id, reply_id, discussion_id, team_id } = req.body; - try { - if (post_id) { - const addPost = await postsDB.updateImageWithPost(image_id, post_id); - - res.status(200).json(addPost); - } else if (reply_id) { - const addReply = await postsDB.updateImageWithReply(image_id, reply_id); - - res.status(200).json(addReply); - } else if (discussion_id) { - const addDiscussion = await postsDB.updateImageWithDiscussion(image_id, discussion_id); - - res.status(200).json(addDiscussion); - } else { - const addTeam = await teamsDB.updateImageWithTeam(image_id, team_id); - - res.status(200).json(addTeam); - } - } catch (err) { - res.status(500).json({ error: `Unable to updateImageWithPost():${err}` }); - } + const { image_id, post_id, reply_id, discussion_id, team_id } = req.body; + try { + if (post_id) { + const addPost = await postsDB.updateImageWithPost(image_id, post_id); + + res.status(200).json(addPost); + } else if (reply_id) { + const addReply = await postsDB.updateImageWithReply(image_id, reply_id); + + res.status(200).json(addReply); + } else if (discussion_id) { + const addDiscussion = await postsDB.updateImageWithDiscussion(image_id, discussion_id); + + res.status(200).json(addDiscussion); + } else { + const addTeam = await teamsDB.updateImageWithTeam(image_id, team_id); + + res.status(200).json(addTeam); + } + } catch (err) { + res.status(500).json({ error: `Unable to updateImageWithPost():${err}` }); + } }); router.delete('/images/:user_id/:image_id', async (req, res) => { - const { image_id } = req.params; + const { image_id } = req.params; - try { - const deleteImage = await postsDB.deleteImage(image_id); + try { + const deleteImage = await postsDB.deleteImage(image_id); - res.status(200).json({ message: 'image deleted' }); - } catch (err) { - res.status(500).json({ error: `Unable to deleteImage():${err}` }); - } + res.status(200).json({ message: 'image deleted' }); + } catch (err) { + res.status(500).json({ error: `Unable to deleteImage():${err}` }); + } }); // Insert Deleted Post and Moderator Who Deleted The Post router.post('/insert-deleted-post/:user_id', (req, res) => { - const user_id = req.params.user_id; - const { post } = req.body; - - const postBody = post.map(p => { - return p.body - }) - - const post_id = post.map(p => { - return p.id - }) - - return postsDB - .insertDeletedPost(user_id, postBody, post_id) - .then(post => { - res.status(200).json(post) - }) - .catch(err => { - res.status(500).json(err) - }) -}) - -// Get Deleted Post -router.get('/get-deleted-post', (req, res) => { + const user_id = req.params.user_id; + const { post } = req.body; + const postBody = post.map((p) => { + return p.body; + }); + + const post_id = post.map((p) => { + return p.id; + }); + + return postsDB + .insertDeletedPost(user_id, postBody, post_id) + .then((post) => { + res.status(200).json(post); + }) + .catch((err) => { + res.status(500).json(err); + }); +}); - return postsDB - .getDeletedPost() - .then(post => { - res.status(200).json(post) - }) - .catch(err => { - res.status(500).json(err) - }) -}) +// Get Deleted Post +router.get('/get-deleted-post', (req, res) => { + return postsDB + .getDeletedPost() + .then((post) => { + res.status(200).json(post); + }) + .catch((err) => { + res.status(500).json(err); + }); +}); module.exports = router; From 6d072475c70c846fb41c80d09b1be922c80d74a8 Mon Sep 17 00:00:00 2001 From: Imon Ovbude Date: Wed, 24 Apr 2019 12:40:53 -0700 Subject: [PATCH 2/2] added email routes to readme --- README.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a53c11e..edd0c4d 100644 --- a/README.md +++ b/README.md @@ -492,4 +492,32 @@ Returns first_name: 'your name', last_name: 'your name' } ] -``` \ No newline at end of file +``` + + +### Post A New Email /emails + +- **POST** + +Expects body +```javascript +{ + email: 'example@example.com' +} +``` + +Returns +```javascript +{ + "message": "Successfully added!" +} +``` + + +### Delete An Email emails/:id + +- **DELETE** + +Expects params: id + +Returns 1 \ No newline at end of file