This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<p id="counter-render"></p> | |
<button id="button-increment">Increment</button> |
This file contains hidden or 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
const connection = require('./db-config'); | |
const express = require('express'); | |
const app = express(); | |
const Joi = require('joi'); | |
const port = process.env.PORT || 3000; | |
connection.connect((err) => { | |
if (err) { | |
console.error('error connecting: ' + err.stack); |
This file contains hidden or 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
queries: | |
SELECT t.name, COUNT(*) AS nb_player | |
FROM player as p | |
JOIN team AS t ON p.team_id=t.id | |
GROUP by t.id | |
ORDER BY nb_player DESC; | |
SELECT t.name | |
FROM player as p | |
JOIN team AS t ON p.team_id=t.id |
This file contains hidden or 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
Queries: | |
SELECT w.lastname, w.firstname, p.role, t.name | |
FROM player AS p | |
JOIN wizard AS w ON p.wizard_id=w.id | |
JOIN team AS t ON p.team_id=t.id | |
ORDER BY t.name, p.role, w.lastname, w.firstname; | |
SELECT w.lastname, w.firstname | |
FROM player AS p | |
JOIN wizard AS w ON p.wizard_id=w.id |
This file contains hidden or 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.post('/api/movies', (req, res) => { | |
const { title, director, year, color, duration } = req.body | |
connection.promise().query( | |
'INSERT INTO movies(title, director, year, color, duration) VALUES (?, ?, ?, ?, ?)', | |
[title, director, year, color, duration]) | |
.then(result => { | |
const id = result.insertId | |
const createdMovie = { id, title, director, year, color, duration } | |
res.status(201).json(createdMovie) | |
res.redirect('Movie successfully saved') |
This file contains hidden or 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}` |
This file contains hidden or 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.delete('/api/movies/:id', (req, res) => { | |
const movieId = req.params.id; | |
connection.promise().query( | |
'DELETE FROM movies WHERE id = ?', | |
[movieId],) | |
.then(result=>res.status(200).send('🎉 Movie deleted!')) | |
.catch(err=>{ | |
console.log(err) | |
res.status(500).send('😱 Error deleting an movie') | |
}) |
This file contains hidden or 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.put('/api/movies/:id', (req, res) => { | |
const movieId = req.params.id | |
const moviePropsToUpdate = req.body | |
connection.promise().query( | |
'UPDATE movies SET ? WHERE id = ?', | |
[moviePropsToUpdate, movieId],) | |
.then(result=>res.status(200).send('Movie updated successfully 🎉')) | |
.catch(err=>{ | |
console.log(err) | |
res.status(500).send('Error updating a movie') |
This file contains hidden or 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.post('/api/users', (req, res) => { | |
const { firstname, lastname, email } = req.body | |
connection.promise().query( | |
'INSERT INTO users(firstname, lastname, email) VALUES (?, ?, ?)', | |
[firstname, lastname, email],) | |
.then(result=>res.status(201).send('User successfully saved') | |
.catch(err=>res.status(500).send('Error saving the user')) | |
) | |
}) |
This file contains hidden or 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
mysql> USE wild_db_quest; | |
Reading table information for completion of table and column names | |
You can turn off this feature to get a quicker startup with -A | |
Database changed | |
mysql> INSERT INTO school (name, country, capacity) | |
-> VALUES ('Beauxbatons Academy of Magic', 'France', 550), | |
-> ('Castelobruxo', 'Brazil', 380), | |
-> ('Durmstrang Institute', 'Norway', 570), | |
-> ('Hogwarts School of Witchcraft and Wizardry', 'United Kingdom', 450), |
NewerOlder