-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
166 lines (149 loc) · 4.78 KB
/
app.js
File metadata and controls
166 lines (149 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
import bcrypt from "bcryptjs";
import passport from "passport";
import { Strategy } from "passport-local";
import "dotenv/config";
const port = 3000;
const saltRounds = 10;
const app = express();
const db = new pg.Client({
host: process.env.PGHOST,
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE,
port: process.env.PGPORT,
});
db.connect();
//middlewares
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true}));
//client requests
app.get("/", async (req, res) => {
try {
const result = await db.query("SELECT * FROM blogs ORDER BY created_at DESC;");
let posts = result.rows;
res.render("home.ejs", {
posts: posts,
});
} catch (err) {
console.error(err);
res.send("Error fetching posts");
}
});
app.get("/login", (req, res) => {
res.render("login.ejs");
});
app.get("/register", (req, res) => {
res.render("register.ejs");
});
app.post("/register", async (req, res) => {
console.log(req.body);
try {
const checkUsername = await db.query("SELECT * FROM users WHERE username=$1", [req.body.username]);
const checkEmail = await db.query("SELECT * FROM users WHERE email=$1", [req.body.email]);
if(checkUsername.rows.length > 0 || checkEmail.rows.length > 0) {
console.log("Needed unique username and email");
res.redirect("/register");
} else {
bcrypt.hash(req.body.password, saltRounds, async(err, hash) => {
if(err) {
console.log(err);
} else {
const result = await db.query("INSERT INTO users(username, email, password_hash, is_verified) VALUES($1, $2, $3, true) RETURNING *;", [req.body.username, req.body.email, hash])
const user = result.rows[0];
// req.login(user, (err)=> {
// console.log("logged in successfully");
// res.redirect("/");
// })
}
})
}
} catch(err) {
console.log(err);
}
})
app.get("/create", (req, res) => {
res.render("create.ejs");
});
app.post("/create", async (req, res) => {
const title = req.body.title;
const content = req.body.content;
try {
if(!title.trim() || !content.trim()) {
return res.status(400).send("Title and content cannot be empty.")
}
await db.query("INSERT INTO blogs(title, content, author_id) VALUES ($1, $2, 1);", [title, content]);// TEMPORARY FIX BY SAYING AUTHOR_ID AS 1 FOR EVERY BLOG
res.redirect("/");
} catch (err) {
console.error(err);
res.send("Error creating post");
}
});
app.get("/posts/:id", async (req, res) => {
const postId = Number(req.params.id);
try {
const result = await db.query("SELECT * FROM blogs WHERE id = $1", [postId]);
const post = result.rows[0];
if (result.rows.length > 0) {
res.render("post.ejs", {
post: post,
});
}
else {
res.status(404).render("404.ejs", {
message: "Post not found!",
});
}
} catch (err) {
console.error(err);
res.send("Error fetching post");
}
});
app.get("/edit/:id", async (req, res) => {
const postId = Number(req.params.id);
try {
const result = await db.query("SELECT * FROM blogs WHERE id = $1", [postId]);
const post = result.rows[0];
if(result.rows.length > 0) {
res.render("edit.ejs", {
post: post,
});
}
else {
res.status(404).render("404.ejs", {
message: "Post not found!",
});
}
} catch (err) {
console.error(err);
res.send("Error loading post");
}
});
app.post("/edit/:id", async (req, res) => {
const postId = Number(req.params.id);
try {
const result = await db.query("SELECT * FROM blogs WHERE id = $1;", [postId]);
if(result.rows.length > 0) {
await db.query("UPDATE blogs SET title = $1, content = $2 WHERE id = $3;", [req.body.title, req.body.content, postId]);
}
res.redirect("/");
} catch (err) {
console.error(err);
res.send("Error updating post");
}
});
app.post("/delete/:id", async (req, res) => {
const id = req.params.id;
try {
await db.query("DELETE FROM blogs WHERE id = $1", [id]);
res.redirect("/");
} catch {
console.error(err);
res.send("Error deleting post");
}
});
app.listen(port, ()=> {
console.log(`Server running on port ${port}...`);
});