-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (82 loc) · 3.39 KB
/
server.js
File metadata and controls
107 lines (82 loc) · 3.39 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
const express = require('express');
const cors = require('cors');
const app = express();
require("dotenv").config();
const fs = require('fs/promises');
const pg = require('pg');
const { application } = require('express');
app.use(express.static('./client')) ;
app.use(cors());
app.use((req,res, next) => {
console.log('url', req.url)
next();
})
const { DATABASE_URL, NODE_ENV, PORT } = process.env;
const pool = new pg.Pool({
connectionString: DATABASE_URL,
ssl: NODE_ENV === "production" ? { rejectUnauthorized: false } : false,
});
// read data
app.get('/api/trending', (req,res) => (
pool.query('SELECT * FROM movies where isTrending = true').then((data) => {
res.status(200).type('application/json').send(data.rows)
})
))
app.get('/api/recommended', (req,res) => {
pool.query('SELECT * FROM movies ORDER BY ratingaverage DESC').then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
app.get('/api/drama', (req,res) => {
const query = "SELECT * FROM movies WHERE to_tsvector(name || ' ' || genres0 || ' ' || genres1 || ' ' || genres2) @@ to_tsquery('Drama') ORDER BY ratingaverage DESC"
pool.query(`${query}`).then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
app.get('/api/comedy', (req,res) => {
const query = "SELECT * FROM movies WHERE to_tsvector(name || ' ' || genres0 || ' ' || genres1 || ' ' || genres2) @@ to_tsquery('Comedy') ORDER BY ratingaverage DESC"
pool.query(`${query}`).then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
app.get('/api/action', (req,res) => {
const query = "SELECT * FROM movies WHERE to_tsvector(name || ' ' || genres0 || ' ' || genres1 || ' ' || genres2) @@ to_tsquery('Action') ORDER BY ratingaverage DESC"
pool.query(`${query}`).then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
app.get('/api/scifi', (req,res) => {
const query = "SELECT * FROM movies WHERE to_tsvector(name || ' ' || genres0 || ' ' || genres1 || ' ' || genres2) @@ to_tsquery('Science-Fiction') ORDER BY ratingaverage DESC"
pool.query(`${query}`).then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
app.get('/api/bookmark', (req,res) => {
pool.query('SELECT * FROM movies WHERE isbookmarked = true').then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
// update bookmark
app.get('/api/bookmark/:id', (req,res) => {
let id = req.params.id;
console.log(req.params.id)
pool.query(`SELECT * FROM movies WHERE id = ${id}`).then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
app.patch('/api/bookmark/:id', (req,res) => {
let id = req.params.id;
pool.query(`UPDATE movies SET isBookmarked = NOT isBookmarked WHERE id = ${id}`)
res.status(200).type('application/json').send('ok')
})
//search functionality
app.get('/api/search/:input', (req,res) => {
let input = req.params.input;
console.log(input)
pool.query(`SELECT * FROM movies WHERE to_tsvector(name || ' ' || genres0 || ' ' || genres1 || ' ' || genres2 || ' ' || summary) @@ plainto_tsquery('${input}');`).then((data) => {
res.status(200).type('application/json').send(data.rows)
})
})
app.listen(process.env.PORT || 3000, () => {
console.log('You are now connected on port')
})