Skip to content

Instantly share code, notes, and snippets.

@vmalep
Created November 16, 2021 20:59
Show Gist options
  • Save vmalep/26760259d88eacc83e90d0682c1c5264 to your computer and use it in GitHub Desktop.
Save vmalep/26760259d88eacc83e90d0682c1c5264 to your computer and use it in GitHub Desktop.
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