Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions api/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,29 @@ 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
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;
});
}
// _____________________________

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
Expand All @@ -105,8 +121,7 @@ exports.makePost = (req,res) => {
return(image)
})

}

}

const post = new Post({
_id: new mongoose.Types.ObjectId(),
Expand All @@ -116,7 +131,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(),
Expand All @@ -125,7 +140,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)=>{
Expand Down Expand Up @@ -323,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})
Expand Down
1 change: 1 addition & 0 deletions api/controllers/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 34 additions & 6 deletions api/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 12 additions & 1 deletion api/routes/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions api/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
3 changes: 0 additions & 3 deletions config/keys.js

This file was deleted.

File renamed without changes.