-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (41 loc) · 1.74 KB
/
index.js
File metadata and controls
48 lines (41 loc) · 1.74 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
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
const app = express();
const port = 5000;
const db = new pg.Client({
user: "postgres",
database: "book-notes",
password: "SQL-FTW'25",
host: "localhost",
port: 5432,
});
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
db.connect();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get("/", async (req, res) => {
const data = await db.query("SELECT * FROM notes ORDER BY rating DESC");
res.render("index.ejs", { notes: data.rows });
});
app.post("/order", async (req, res) => {
const data = await db.query("SELECT * FROM notes ORDER BY " + req.body.order);
res.render("index.ejs", { notes: data.rows });
});
app.post("/edit", async(req, res) => {
await db.query("UPDATE notes SET description=$1 WHERE id=$2", [req.body.edit, req.body.id]);
const data = await db.query("SELECT * FROM notes ORDER BY rating DESC");
res.render("index.ejs", { notes: data.rows });
});
app.post("/new-note", async(req, res) => {
var currentDate = new Date();
var date = months[currentDate.getMonth()] + " " + currentDate.getDate() + ", " + currentDate.getFullYear();
await db.query("INSERT INTO notes (title, author, rating, isbn, description, date) VALUES ($1, $2, $3, $4, $5, $6)",
[req.body.author, req.body.title, req.body.rating, req.body.isbn, req.body.description, date]);
const data = await db.query("SELECT * FROM NOTES ORDER by rating DESC");
res.render("index.ejs", { notes: data.rows });
});
app.listen(port, (req, res) => {
console.log(`Listening on port ${port}.`);
});