Skip to content
42 changes: 42 additions & 0 deletions src/controllers/Post/createPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

/**
* @module Create Post Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller will allow the user to create his own post, if all parameters are correct.
*
* @todo
* Nothing for now.
*/




module.exports.createPost = (req, res) => {

// check if all the parameters are valid.

const { postTitle, postContent } = req.body;

if (!postTitle || !postContent) {
return res.status(400).send('post content or post title cannot be blank.');
}

// create a new post record

Post.create({
title: postTitle,
content: postContent,
// ownerId: 1,
ownerId: req.user.id,
})


// inform the user with the process status.

return res.status(200).send('post created');
}

52 changes: 52 additions & 0 deletions src/controllers/Post/deletePost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

/**
* @module Delete Post Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller will allow the user to delete his own post, if all parameters are correct.
*
* @todo
* Nothing for now.
*/




module.exports.deletePost = async (req, res) => {


if (!req.params.postId) {
return res.status(400).send('invalid post id.');
}

// find the original post.

const foundPost = await Post.findOne({
where: {
id: req.params.postId,
}
});

// check that it exists.

if (!foundPost) {
return res.status(404).send({ msg: 'The post that you are trying to update does not exist.' });
}

// check if the owner is the active user.

if (foundPost.ownerId != req.user.id) {
return res.status(401).send({ msg: 'You are not allowed to change a ressource that you do not own.' })
}

foundPost.destroy()


// inform the user with the process status.

return res.status(200).send('post deleted');
}

34 changes: 34 additions & 0 deletions src/controllers/Post/getAllPosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/**
* @module Get All Posts Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller allows to read a all the existing posts, if all parameters are correct.
*
* @todo
* Nothing for now.
*/




module.exports.getAllPosts = async (req, res) => {

// fetch all the posts.

const foundPosts = await Post.findAll({
attributes: ['title', 'content'],
})

if (!foundPosts || !foundPosts.length ) {
return res.status(404).send('No posts for now.');
}

// send back to the user the fetched data.

return res.status(200).json(foundPosts);
}

44 changes: 44 additions & 0 deletions src/controllers/Post/getPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/**
* @module Get Post Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller allows to read a specific post, if all parameters are correct.
*
* @todo
* Nothing for now.
*/




module.exports.getPost = async (req, res) => {

// check if all the parameters are valid.

if (!req.params.postId) {
return res.status(400).send('post id can not be null.');
}


// fetch the requested post.

const foundPost = await Post.findOne({
where: {
id: req.params.postId,
},
attributes: ['title', 'content'],
})

if (!foundPost) {
return res.status(404).send('Post ressource not found.');
}

// send back to the user the fetched data.

return res.status(200).json(foundPost);
}

14 changes: 14 additions & 0 deletions src/controllers/Post/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { createPost } = require('./createPost');
const { getPost } = require('./getPost');
const { getAllPosts } = require('./getAllPosts');
const { updatePost } = require('./updatePost');
const { deletePost } = require('./deletePost');


module.exports = {
createPost,
getPost,
getAllPosts,
updatePost,
deletePost,
};
62 changes: 62 additions & 0 deletions src/controllers/Post/updatePost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

/**
* @module Update Post Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller will allow the user to update his own post, if all parameters are correct.
*
* @todo
* Nothing for now.
*/




module.exports.updatePost = async (req, res) => {

// check if all the parameters are valid.

const { newPostTitle, newPostContent } = req.body;

if (!newPostTitle || !newPostContent) {
return res.status(400).send('post content or post title cannot be blank.');
}

if (!req.params.postId) {
return res.status(400).send('invalid post id.');
}

// find the original post.

const foundPost = await Post.findOne({
where: {
id: req.params.postId,
}
});

// check that it exists.

if (!foundPost) {
return res.status(404).send({ msg: 'The post that you are trying to update does not exist.' });
}

// check if the owner is the active user.

if (foundPost.ownerId != req.user.id) {
return res.status(401).send({ msg: 'You are not allowed to change a ressource that you do not own.' })
}

foundPost.update({
title: newPostTitle,
content: newPostContent,
})


// inform the user with the process status.

return res.status(200).send('post updated');
}

44 changes: 44 additions & 0 deletions src/controllers/User/Posts/listAllUserPosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/**
* @module list All User Posts Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller allows to list all the posts related to a specific user, if all parameters are correct.
*
* @todo
* Nothing for now.
*/




module.exports.listAllUserPosts = async (req, res) => {

// check if all the parameters are valid.

if (!req.params.id) {
return res.status(400).send('user id can not be null.');
}


// list the user posts.

const foundPosts = await Post.findAll({
where: {
ownerId: req.params.id,
},
attributes: ['title', 'content'],
})

if (!foundPosts) {
return res.status(404).send('This user did not posted yet.');
}

// send back to the user the fetched data.

return res.status(200).json(foundPosts);
}

45 changes: 45 additions & 0 deletions src/controllers/User/Posts/listOneUserPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/**
* @module List One User Post Controller
*
* @param {Request} req - HTTP Request from the client
* @param {Response} res - HTTP Response for the client
*
* @description
* This controller allows to read a specific post written by a given user, if all parameters are correct.
*
* @todo
* Nothing for now.
*/




module.exports.listOneUserPost = async (req, res) => {


// fetch the requested post.

const foundPost = await Post.findOne({
where: {
id: req.params.postId,
},
attributes: ['title', 'content', 'ownerId'],
})

if (!foundPost) {
return res.status(404).send('Post ressource not found.');
}

// check if the given user is the owner.

if (req.params.id != foundPost.ownerId) {
return res.status(403).send('The requested post does not belong to the selected user.');
}


// send back to the user the fetched data.

return res.status(200).json(foundPost);
}

8 changes: 8 additions & 0 deletions src/controllers/User/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const { countUsers } = require('./Helpers/countUsers');
const { listUsers } = require('./Helpers/listUsers');
const { queryUser } = require('./Helpers/queryUser');


const { listAllUserPosts } = require('./Posts/listAllUserPosts');
const { listOneUserPost } = require('./Posts/listOneUserPost');



module.exports = {
confirmEmail,
deleteAccount,
Expand All @@ -46,4 +52,6 @@ module.exports = {
countUsers,
listUsers,
queryUser,
listAllUserPosts,
listOneUserPost,
};
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('dotenv').config();
require('./models/index');
require('./models')

// Configure Imports
const express = require('express');
Expand All @@ -9,7 +9,6 @@ const schedule = require('node-schedule');
const { removeUnconfirmedAccounts, emailConfirmationRemainder } = require('./jobs/accountCleanup');
const { eventRemainder } = require("./jobs/eventNotifications");

require('./models')

// Configure Local Variables
const app = express();
Expand Down Expand Up @@ -44,6 +43,11 @@ app.use('/event', require('./routes/eventRoutes'));
app.use("/user", require("./routes/userRoutes"));
app.use("/u", require("./routes/uRoutes"));

// Post Endpoints

app.use('/posts', require('./routes/postRoutes'));


// Sync the Database
(async () => {

Expand Down
Loading