Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create post #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
41 changes: 41 additions & 0 deletions src/controllers/Post/createPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

/**
* @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 = async (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: req.user.id,
})


// inform the user with the process status.

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

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



module.exports = {
createPost,
};
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
7 changes: 7 additions & 0 deletions src/models/Post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('Post', {
title: DataTypes.STRING,
content: DataTypes.TEXT,
ownerId: DataTypes.INTEGER,
});
};
6 changes: 6 additions & 0 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ if (env === 'development') {
const Article = require('./Article');
const User = require('./User');
const Event = require('./Event');
const Post = require('./Post');

// Define Models
global.Article = Article(sequelize, DataTypes);
global.User = User(sequelize, DataTypes);
global.Event = Event(sequelize, DataTypes);
global.Post = Post(sequelize,DataTypes);

// Setup Relationships

Expand Down Expand Up @@ -73,4 +75,8 @@ global.User.belongsToMany(global.User, {



// TODO: Relationship for post system.



global.sequelize = sequelize;
10 changes: 10 additions & 0 deletions src/routes/postRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express');
const router = express.Router();
const postControllers = require('../controllers/Post');




router.post('/create', postControllers.createPost);

module.exports = router;