From 36b974f97518e1d72b6ea1b56b9b7bd87bb67b9d Mon Sep 17 00:00:00 2001 From: cvpua Date: Mon, 17 Aug 2020 23:36:39 +0800 Subject: [PATCH 1/3] Add documentation about the req.body in routes --- api/controllers/post.js | 23 +++++++++++++++----- api/controllers/signup.js | 1 + api/routes/posts.js | 40 +++++++++++++++++++++++++++++------ api/routes/signup.js | 13 +++++++++++- api/routes/users.js | 4 ++++ client/notes.txt => notes.txt | 0 6 files changed, 69 insertions(+), 12 deletions(-) rename client/notes.txt => notes.txt (100%) diff --git a/api/controllers/post.js b/api/controllers/post.js index 7ce8dd5..57b316b 100644 --- a/api/controllers/post.js +++ b/api/controllers/post.js @@ -90,13 +90,27 @@ exports.makePost = (req,res) => { const dir = 'assets/' + req.body.author + '/posts/' + title + '_' + Date.now() + '/images/'; let imageArray = null; + + let items = null; + + // please comment out this part if nag-error sa paggawa ng post + items = req.body.items.map(item => { + + const newItem = new Item({ + name : item.name, + amount : item.amount, + total : item.total + }); + return newItem; + }); + // _____________________________ + if(req.files && req.files.length > 1){ imageArray = req.files.map(file =>{ image = new Image({ _id: new mongoose.Types.ObjectId(), - total: req.body.total, image: { imageName : file.filename, url: 'http://localhost:5000/'+dir+file.filename @@ -105,8 +119,7 @@ exports.makePost = (req,res) => { return(image) }) - } - + } const post = new Post({ _id: new mongoose.Types.ObjectId(), @@ -116,7 +129,7 @@ exports.makePost = (req,res) => { type : req.body.type, status : req.body.status, description : req.body.description, - items : [], + items : items, location : req.body.location, tags : req.body.tags, datePosted : Date.now(), @@ -125,7 +138,7 @@ exports.makePost = (req,res) => { comments : [] }) - // There is images and needs to create folders and move images + // There are images therefore it needs to create folders and move images if(req.files.length > 0){ fs.move('./uploads',dir,(error)=>{ diff --git a/api/controllers/signup.js b/api/controllers/signup.js index 25c8191..754d53d 100644 --- a/api/controllers/signup.js +++ b/api/controllers/signup.js @@ -92,6 +92,7 @@ exports.login = (req,res) => { }) return res.status(200).json({ message : "Logged in", + userId : user._id, username : user.username, email : user.email, isLoggedIn : true, diff --git a/api/routes/posts.js b/api/routes/posts.js index 1c5b3d2..019d83a 100644 --- a/api/routes/posts.js +++ b/api/routes/posts.js @@ -8,20 +8,48 @@ const PostController = require('../controllers/post'); //get all posts router.get('/api/posts',PostController.getAllPosts) + + // get a post +//reqs attributes +// postId router.get('/api/posts/:postId',PostController.getPost) //make a post +//req attributes +// avatar , +// title , +// author , +// type , +// status , +// description , +// items, +// location, +// tags, +// datePosted, +// deadline, +// photos , router.post('/api/posts',checkAuth,PostController.makePost); //make a comment -router.patch('/api/posts/:postId/comments',PostController.makeComment); - -//like/unlike a post -router.patch('/api/posts/:postId/likes',PostController.likePost); - -router.delete('/api/posts/:postId',PostController.deletePost); +// req attributes +// avatar +// username +// content +// postId +router.patch('/api/posts/:postId/comments',checkAuth,PostController.makeComment); + +// like/unlike a post +// req attributes +// userId +// postId +router.patch('/api/posts/:postId/likes',checkAuth,PostController.likePost); + +// delete a post +// req attributes +// postId +router.delete('/api/posts/:postId',checkAuth,PostController.deletePost); module.exports = router; \ No newline at end of file diff --git a/api/routes/signup.js b/api/routes/signup.js index 69de816..30a4b4c 100644 --- a/api/routes/signup.js +++ b/api/routes/signup.js @@ -10,11 +10,22 @@ const User = require('../models/user'); const { response } = require('express'); - +// signup +// req attributes: +// username +// password +// firstName +// lastName +// email +// location +// contactNumber router.post('/api/signup', Auth.signup) // login user +// req attributes: +// email +// password router.post('/api/login', Auth.login) module.exports = router; \ No newline at end of file diff --git a/api/routes/users.js b/api/routes/users.js index 44db2fd..ae4a2b5 100644 --- a/api/routes/users.js +++ b/api/routes/users.js @@ -10,11 +10,15 @@ router.get('/api/users',UserController.getAllUsers) // get one user +// req attributes: +// userId router.get('/api/users/:userId',UserController.getUser) // get all post liked by a user +// req attribute: +// userId router.get('/api/users/:userId/likedPosts',UserController.getLikedPosts) diff --git a/client/notes.txt b/notes.txt similarity index 100% rename from client/notes.txt rename to notes.txt From 3c526d3f544fb566787567b3f9f21e7771866f64 Mon Sep 17 00:00:00 2001 From: cvpua Date: Tue, 18 Aug 2020 10:35:51 +0800 Subject: [PATCH 2/3] Fix items iteration --- api/controllers/post.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/api/controllers/post.js b/api/controllers/post.js index 57b316b..a171a48 100644 --- a/api/controllers/post.js +++ b/api/controllers/post.js @@ -94,15 +94,17 @@ exports.makePost = (req,res) => { let items = null; // please comment out this part if nag-error sa paggawa ng post - items = req.body.items.map(item => { - - const newItem = new Item({ - name : item.name, - amount : item.amount, - total : item.total + if(req.body.items && req.body.items.length > 0){ + items = req.body.items.map(item => { + console.log(item); + const newItem = new Item({ + name : item.name, + amount : item.amount, + total : item.total + }); + return newItem; }); - return newItem; - }); + } // _____________________________ if(req.files && req.files.length > 1){ @@ -336,6 +338,7 @@ exports.deletePost = (req,res) => { .exec() .then(user =>{ user.posts = user.posts.filter(post => String(post._id) !== req.params.postId) + post.type === "request" ? user.requestCount = user.requestCount - 1 : user.donationCount = user.donationCount - 1 user.save() .then(response => { res.status(200).json({message : "Post deleted!",post}) From fdecd6ddb9f3e53c9ff7183cf212f56b48e59e3a Mon Sep 17 00:00:00 2001 From: Carl Vincent Pua <44056997+cvpua@users.noreply.github.com> Date: Thu, 1 Apr 2021 08:47:04 +0800 Subject: [PATCH 3/3] Delete keys.js --- config/keys.js | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 config/keys.js diff --git a/config/keys.js b/config/keys.js deleted file mode 100644 index df5702b..0000000 --- a/config/keys.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - mongoURI : "mongodb+srv://grouptwo:padthebest@donasource.8omhn.mongodb.net/?retryWrites=true&w=majority" -} \ No newline at end of file