-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connection.js
96 lines (81 loc) · 2.16 KB
/
db_connection.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
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
// Tuto ici : https://expressjs.com/en/starter/hello-world.html
// https://www.w3schools.com/nodejs/nodejs_mysql.asp
var express = require('express')
var app = express()
var mysql = require('mysql')
var con = mysql.createConnection({
host: 'localhost',
user: 'fabino',
password: 'MonopolI',
database: 'movies',
})
con.connect(function (err) {
if (err) throw err
console.log('Connected!')
})
var cors = require('cors')
app.use(
cors({
origin: "*",
})
);
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
res.send('hello world')
})
// get 20 first movies
app.get('/api/movies', function (req, res) {
con.query(
'SELECT * FROM movies LIMIT 20;',
function (err, result) {
if (err) throw err
res.send(result);
}
)
})
// get all movies for search in
app.get('/api/allmovies', function (req, res) {
con.query('SELECT * FROM movies;', function (err, result) {
if (err) throw err
res.send(result)
})
})
app.get('/api/genres', function (req, res) {
con.query(
'SELECT * FROM genres;',
function (err, result) {
if (err) throw err
res.send(result);
}
)
})
app.get('/api/producers', function (req, res) {
con.query(
'SELECT id,name FROM producers;',
function (err, result) {
if (err) throw err
res.send(result);
}
)
})
app.get('/api/movies/:id', function (req, res) {
con.query("SELECT * FROM movies WHERE id =" + req.params['id'], function (err, result) {
if (err) throw err;
res.send(result);
});
})
app.get('/api/movies/:id/genres', function (req, res) {
con.query("SELECT name FROM genres WHERE id IN (SELECT genre_id FROM movies WHERE id = " + req.params['id'] + " );", function (err, result) {
if (err) throw err;
res.send(result);
});
})
app.get('/api/movies/:id/producers', function (req, res) {
con.query("SELECT name FROM producers WHERE id IN (SELECT producer_id FROM movies WHERE id = " + req.params['id'] + " );", function (err, result) {
if (err) throw err;
res.send(result);
});
})
app.listen(8000, () => {
console.log("Serveur à l'écoute")
})