-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.48 KB
/
index.js
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
const express = require("express");
const cors = require("cors");
const pool = require("./db");
const app = express();
//middleware
app.use(express.json()); //access to req.body
app.use(cors());
//posting movie in db
app.post("/api/movies", async (req, res) => {
try {
const { name, genre } = req.body;
const newMovie = await pool.query(
"INSERT INTO movie (name, genre) VALUES($1, $2) RETURNING *",
[name, genre]
);
res.json(newMovie.rows[0]);
} catch (err) {
console.error(err.message);
}
});
//get all movies
app.get("/api/movies", async (req, res) => {
const getAll = await pool.query("SELECT * FROM movie");
res.json(getAll.rows);
});
app.listen(5000, () => {
console.log("Listening on port 5000...");
});
//get movies with specific id
app.get("/api/movies/:id", async (req, res) => {
const { id } = req.params;
var getSpecificMovie = await pool.query("SELECT * FROM movie WHERE id = $1", [
id,
]);
res.json(getSpecificMovie.rows[0]);
});
//update movie
app.put("/api/movies/:id", async (req, res) => {
const { id } = req.params;
const { name, genre } = req.body;
const updateMovies = await pool.query(
"UPDATE movie SET name = $1, genre = $2 WHERE id = $3",
[name, genre, id]
);
res.json(updateMovies.rows[0]);
});
//delete movie
app.delete("/api/movies/:id", async (req, res) => {
const { id } = req.params;
const deleteMovie = await pool.query("DELETE FROM movie WHERE id = $1", [id]);
res.json("Movie was deleted");
});