Created
November 16, 2021 20:59
-
-
Save vmalep/26760259d88eacc83e90d0682c1c5264 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/api/movies', (req, res) => { | |
let sql = 'SELECT * FROM movies' | |
if(req.query.color === '1') { | |
sql += ' WHERE color = 1' | |
} | |
if(req.query.max_duration){ | |
const maxDuration = parseInt(req.query.max_duration) | |
sql.includes('WHERE') ? sql += ' AND ' : sql += ' WHERE' | |
sql += ` duration <= ${maxDuration}` | |
} | |
connection.promise().query(sql) | |
.then(result=>res.status(200).json(result)) | |
.catch(err=>res.status(500).send('Error retrieving data from database')) | |
}) | |
app.get('/api/movies/:id', (req, res) => { | |
const movieId = req.params.id | |
console.log(movieId) | |
connection.promise().query('SELECT * FROM movies WHERE id = ?', [movieId]) | |
.then(result=>res.status(200).json(result[0][0])) | |
.catch(err=>res.status(500).send('Error retrieving data from database')) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment