Skip to content

Instantly share code, notes, and snippets.

View Guillaume-Cojan's full-sized avatar
👨‍💻

Guillaume Cojan Guillaume-Cojan

👨‍💻
View GitHub Profile
function hello(name: string) {
console.log("Hello " + name);
}
const firstName = "bob";
hello(firstName);
hello(firstName + " marley");
function concat(a: string, b: string): string {
@Guillaume-Cojan
Guillaume-Cojan / Express and Mongoose
Last active July 26, 2021 09:52
Express and Mongoose
const mongoose = require("mongoose")
const schema = mongoose.Schema({
title: String,
content: String,
})
module.exports = mongoose.model("Post", schema)
// Model User
const create = ({ firstname, lastname, email, password }) => {
return hashPassword(password).then((password) => {
return db
.query("INSERT INTO users SET ?", {
firstname,
lastname,
email,
password,
1: mysql> SELECT team.name, COUNT(*) AS nb_players
FROM player
LEFT JOIN team ON team.id = player.team_id
GROUP BY team.name;
+------------+------------+
| name | nb_players |
+------------+------------+
| Hufflepuff | 12 |
| Gryffindor | 36 |
| Slytherin | 21 |
mysql> SELECT lastname, firstname, role, name FROM player INNER JOIN wizard ON wizard.id=player.wizard_id INNER JOIN team ON team.id=player.team_id ORDER BY name ASC, role ASC, lastname ASC, firstname ASC;
+-----------------+-------------+--------+------------+
| lastname | firstname | role | name |
+-----------------+-------------+--------+------------+
| Black | Sirius | beater | Gryffindor |
| Brown | Lavender | beater | Gryffindor |
| Finnigan | Seamus | beater | Gryffindor |
| Hagrid | Rubeus | beater | Gryffindor |
| Longbottom | Alice | beater | Gryffindor |
| McGonagall | Minerva | beater | Gryffindor |
@Guillaume-Cojan
Guillaume-Cojan / gist:03215e4f74e61bc3fe1233bfa105c9b1
Created June 14, 2021 08:11
Express 9 - 🌏 Setup an architecture
t
@Guillaume-Cojan
Guillaume-Cojan / gist:815e503a84b51fe002f2bdd0da43b784
Created June 14, 2021 08:10
Express 8 - 🔧 POST and PUT in detail : Validate user input
app.post('/api/movies', (req, res) => {
const { title, director, year, color, duration } = req.body;
const db = connection.promise();
let validationErrors = null;
db.query('SELECT * FROM users WHERE title = ?', [title])
.then(([result]) => {
if (result[0]) return Promise.reject('DUPLICATE_MOVIE');
validationErrors = Joi.object({
title: Joi.string().email().max(255).required(),
director: Joi.string().max(255).required(),
@Guillaume-Cojan
Guillaume-Cojan / gist:e8ee7251facece5d85478eb41837ff5a
Created June 14, 2021 08:06
Express 7 - 🔧 POST and PUT in detail : Return an adequate response
const connection = require("./db-config");
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
connection.connect((err) => {
if (err) {
console.error("error connecting: " + err.stack);
} else {
@Guillaume-Cojan
Guillaume-Cojan / gist:a0e97955a853f0d9de5ce3948bd7d481
Created May 30, 2021 20:36
Express 5 - 🗑️ DELETE method and data deletion
app.delete('/api/movies/:id', (req, res) => {
const movieId = req.params.id;
connection.query(
'DELETE FROM movies WHERE id = ?',
[movieId],
(err, results) => {
if (err) {
console.log(err);
res.status(500).send('Error deleting a movie');
} else {
@Guillaume-Cojan
Guillaume-Cojan / gist:4c9fe4e5e33ca36ebdd18efa7886d5bf
Created May 30, 2021 20:34
Express 4 - 🛸 PUT method and data modification
const connection = require('./db-config');
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
connection.connect((err) => {
if (err) {
console.error('error connecting: ' + err.stack);
} else {