Skip to content

Commit 6f47862

Browse files
committed
feat: add create post page and homepage feed
- add create post route - add post creation form - implement form validation for create post form - add post creation time column in the database - create cards for each post on homepage - change the authErrors.ejs placement in login & register page. - remove the header from login & register page - fix the width of error page
1 parent 7812c08 commit 6f47862

15 files changed

+555
-111
lines changed

app.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const LocalStrategy = require('passport-local').Strategy;
77
const indexRouter = require('./routes/indexRouter.js');
88
const loginRouter = require('./routes/loginRouter.js');
99
const registerRouter = require('./routes/registerRouter.js');
10+
const postRouter = require('./routes/postRouter.js');
1011
const customErrors = require('./errors/CustomErrors.js');
1112
const db = require('./db/query.js');
1213
const pool = require('./db/pool.js');
@@ -83,6 +84,7 @@ app.use((req, res, next) => {
8384
app.use('/', indexRouter);
8485
app.use('/login', loginRouter);
8586
app.use('/register', registerRouter);
87+
app.use('/create-post', postRouter);
8688
app.post('/logout', (req, res) => {
8789
req.logOut((err) => {
8890
if (err) {

controllers/indexController.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
const asyncHandler = require('express-async-handler');
12
const db = require('../db/query.js');
23

34
const indexController = {
4-
renderIndex: (req, res) => {
5-
const user = {
6-
...res.locals.currentUser
7-
};
8-
const posts = db.getPosts(user.is_member);
9-
res.render('index', {user, posts});
10-
}
5+
renderIndex: asyncHandler(async (req, res) => {
6+
const user = {
7+
...res.locals.currentUser
8+
};
9+
const posts = await db.getPosts();
10+
res.render('index', {user, posts});
11+
}),
1112
};
1213

1314
module.exports = indexController;

controllers/loginController.js

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const loginController = {
3232
if (err) {
3333
return next(err);
3434
}
35+
console.log(err);
36+
console.log(user);
3537
if (!user) {
3638
return res.render('login', { sessionErrors: [info.message] });
3739
}

controllers/postController.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const {body, validationResult} = require('express-validator');
2+
const asyncHandler = require('express-async-handler');
3+
const db = require('../db/query.js');
4+
5+
// VALIDATIONS //
6+
7+
8+
const validateTitle = [
9+
body('title').trim().notEmpty().withMessage('The title is required'),
10+
body('title').isLength({ min: 5 }).withMessage('The title must be at least 5 characters long'),
11+
body('title').isLength({ max: 80 }).withMessage('The title must be at most 80 characters long')
12+
];
13+
14+
const validateContent = [
15+
body('content').trim().notEmpty().withMessage('The content cannot be empty'),
16+
body('content').isLength({ min: 5 }).withMessage('The content must be at least 5 characters long'),
17+
body('content').isLength({ max: 500 }).withMessage('The content must be at most 500 characters long')
18+
];
19+
20+
// CONTROLLER //
21+
22+
23+
const postController = {
24+
renderForm: asyncHandler(async (req, res) => {
25+
const user = {
26+
...res.locals.currentUser
27+
};
28+
// const postsCreated = db.getPostsCreated(user.id);
29+
res.render('createPost', {user});
30+
}),
31+
32+
createPost: [
33+
validateTitle,
34+
validateContent,
35+
asyncHandler(async (req, res) => {
36+
const errors = validationResult(req);
37+
if (!errors.isEmpty()) {
38+
res.render('createPost', {errors: errors.array()});
39+
} else {
40+
const {title, content} = req.body;
41+
const userId = res.locals.currentUser.id;
42+
await db.createPost(title, content, userId);
43+
res.redirect('/');
44+
}
45+
})
46+
],
47+
48+
}
49+
50+
module.exports = postController;

db/populatedb.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const SQL = `
1515
user_id INTEGER NOT NULL,
1616
title VARCHAR(255) NOT NULL,
1717
content TEXT,
18+
created_at TIMESTAMP DEFAULT NOW(),
1819
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
1920
);
2021

db/query.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,24 @@ const findUserById = asyncHandler(async (id) => {
4444
return rows[0];
4545
});
4646

47-
const getPosts = asyncHandler(async (isMember) => {
48-
console.log(isMember);
47+
const getPosts = asyncHandler(async () => {
48+
const {rows} = await pool.query(
49+
'SELECT * FROM posts ORDER BY created_at DESC'
50+
);
51+
return rows;
52+
});
53+
54+
const createPost = asyncHandler(async (title, content, userId) => {
55+
await pool.query(
56+
'INSERT INTO posts (title, content, user_id) VALUES ($1, $2, $3)', [title, content, userId]
57+
);
4958
});
5059

5160
module.exports = {
5261
filterUsername,
5362
registerUser,
5463
findUser,
5564
findUserById,
56-
getPosts
65+
getPosts,
66+
createPost
5767
};

0 commit comments

Comments
 (0)