generated from lucien/api-template
add: added auth to the api
This commit is contained in:
parent
6017eb9d1f
commit
217c763abd
8 changed files with 790 additions and 279 deletions
82
back/libs/mysql.js
Normal file
82
back/libs/mysql.js
Normal file
|
@ -0,0 +1,82 @@
|
|||
const mysql = require("mysql");
|
||||
|
||||
function getConnection() {
|
||||
return mysql.createConnection({
|
||||
host: process.env.MYSQL_HOST,
|
||||
user: process.env.MYSQL_USER,
|
||||
password: process.env.MYSQL_PASSWORD,
|
||||
database: process.env.MYSQL_DATABASE,
|
||||
});
|
||||
}
|
||||
|
||||
// +-------------------------------+
|
||||
// | Users |
|
||||
// +-------------------------------+
|
||||
|
||||
function addUser(connection, name, lastname, email, numero, password) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
`INSERT INTO users (name, lastname, email, numero, password) VALUES (?, ?, ?, ?, ?)`,
|
||||
[name, lastname, email, numero, password],
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function getUsers(connection) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
`SELECT * FROM users`,
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function getUser(connection, id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
`SELECT * FROM users WHERE id = ?`,
|
||||
[id],
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function getUserByEmail(connection, email) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
`SELECT * FROM users WHERE email = ?`,
|
||||
[email],
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getConnection,
|
||||
|
||||
addUser,
|
||||
getUsers,
|
||||
getUser,
|
||||
getUserByEmail,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue