Implement a database and an API for it (#2)
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Ajout de la base de données en Sqlite3 et des méthodes permettant de faire des actions sur la base de données. Co-authored-by: hugo.pradier2 <hugo.pradier2@etu.uca.fr> Co-authored-by: cofrizot <colin.frizot@etu.uca.fr> Co-authored-by: clfreville2 <clement.freville2@etu.uca.fr> Reviewed-on: #2 Reviewed-by: Clément FRÉVILLE <clement.freville2@etu.uca.fr> Co-authored-by: Hugo PRADIER <hugo.pradier2@etu.uca.fr> Co-committed-by: Hugo PRADIER <hugo.pradier2@etu.uca.fr>pull/5/head
parent
aaf24387f1
commit
b58317ae88
@ -0,0 +1,328 @@
|
||||
import fs from "fs";
|
||||
import sqlite3 from "sqlite3";
|
||||
|
||||
const dbDirectory = "./src/db";
|
||||
const dbFilePath = `${dbDirectory}/database.db`;
|
||||
|
||||
/* Fonction pour exécuter une requête sur la base de données */
|
||||
|
||||
/* Fonction pour exécuter une requête de modification de la base de données (INSERT, UPDATE, DELETE) */
|
||||
export function runDB(
|
||||
db: sqlite3.Database,
|
||||
query: string,
|
||||
params: any[]
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.run(query, params, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* Fonction pour récupérer plusieurs lignes de la base de données */
|
||||
export function allDB<T>(
|
||||
db: sqlite3.Database,
|
||||
query: string
|
||||
): Promise<unknown[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.all(query, (err, rows) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(rows);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* Fonction pour récupérer une seule ligne de la base de données */
|
||||
export function getDB<T>(
|
||||
db: sqlite3.Database,
|
||||
query: string,
|
||||
params: any[]
|
||||
): Promise<T[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.get(query, params, (err, row: any) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(row);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* Fonctions pour la gestion de la base de données */
|
||||
|
||||
/* Créer le répertoire db s'il n'existe pas */
|
||||
export function createDbDirectory() {
|
||||
if (!fs.existsSync(dbDirectory)) {
|
||||
fs.mkdirSync(dbDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
/* Ouvrir la base de données */
|
||||
export function openDatabase() {
|
||||
console.log("Ouverture de la connexion à la base de données.");
|
||||
return new sqlite3.Database(
|
||||
dbFilePath,
|
||||
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
|
||||
(err: Error | null) => {
|
||||
if (err) console.error(err.message);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/* Fermer la base de données */
|
||||
export function closeDatabase(db: sqlite3.Database) {
|
||||
db.close((err) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
}
|
||||
console.log("Fermeture de la connexion à la base de données.");
|
||||
});
|
||||
}
|
||||
|
||||
/* Create all the tables in the database */
|
||||
export function createTables(db: sqlite3.Database) {
|
||||
createRegisteredUserTable(db);
|
||||
createLanguageTable(db);
|
||||
createWorkTable(db);
|
||||
}
|
||||
|
||||
/////////////////////////// Gestion des utilisateurs ///////////////////////////
|
||||
// CREATE TABLE registered_user (
|
||||
// id_user SERIAL PRIMARY KEY,
|
||||
// login VARCHAR(64) NOT NULL,
|
||||
// password VARCHAR(72) NOT NULL,
|
||||
// permissions INT NOT NULL,
|
||||
// UNIQUE (login)
|
||||
// );
|
||||
|
||||
/* Créer la table registered_user dans la base de données */
|
||||
export function createRegisteredUserTable(db: sqlite3.Database): Promise<void> {
|
||||
const tableRegisteredUser = `CREATE TABLE IF NOT EXISTS registered_user (id_user INTEGER PRIMARY KEY AUTOINCREMENT, login TEXT NOT NULL, password TEXT NOT NULL, permissions INTEGER NOT NULL, UNIQUE (login))`;
|
||||
|
||||
return runDB(db, tableRegisteredUser, []);
|
||||
}
|
||||
|
||||
/* Insérer un utilisateur dans la table registered_user */
|
||||
export function insertUser(
|
||||
db: sqlite3.Database,
|
||||
login: string,
|
||||
password: string,
|
||||
permissions: number
|
||||
) {
|
||||
const insertUserQuery = `INSERT INTO registered_user (login, password, permissions) VALUES (?, ?, ?)`;
|
||||
|
||||
return runDB(db, insertUserQuery, [login, password, permissions]);
|
||||
}
|
||||
|
||||
/* Modifier le login d'un utilisateur dans la table registered_user */
|
||||
export function updateUserLogin(
|
||||
db: sqlite3.Database,
|
||||
id: number,
|
||||
newLogin: string
|
||||
) {
|
||||
const updateUserLoginQuery = `UPDATE registered_user SET login = ? WHERE id_user = ?`;
|
||||
|
||||
return runDB(db, updateUserLoginQuery, [newLogin, id]);
|
||||
}
|
||||
|
||||
/* Modifier le mot de passe d'un utilisateur dans la table registered_user */
|
||||
export function updateUserPassword(
|
||||
db: sqlite3.Database,
|
||||
id: number,
|
||||
newPassword: string
|
||||
) {
|
||||
const updateUserPasswordQuery = `UPDATE registered_user SET password = ? WHERE id_user = ?`;
|
||||
|
||||
return runDB(db, updateUserPasswordQuery, [newPassword, id]);
|
||||
}
|
||||
|
||||
/* Modifier les permissions d'un utilisateur dans la table registered_user */
|
||||
export function updateUserPermissions(
|
||||
db: sqlite3.Database,
|
||||
id: number,
|
||||
newPermissions: number
|
||||
) {
|
||||
const updateUserPermissionsQuery = `UPDATE registered_user SET permissions = ? WHERE id_user = ?`;
|
||||
|
||||
return runDB(db, updateUserPermissionsQuery, [newPermissions, id]);
|
||||
}
|
||||
|
||||
/* Supprimer un utilisateur de la table registered_user par son ID */
|
||||
export function deleteUserById(db: sqlite3.Database, id: number) {
|
||||
const deleteUserQuery = `DELETE FROM registered_user WHERE id_user = ?`;
|
||||
|
||||
return runDB(db, deleteUserQuery, [id]);
|
||||
}
|
||||
|
||||
/* Supprimer un utilisateur de la table registered_user par son login */
|
||||
export function deleteUserByLogin(db: sqlite3.Database, login: string) {
|
||||
const deleteUserQuery = `DELETE FROM registered_user WHERE login = ?`;
|
||||
|
||||
return runDB(db, deleteUserQuery, [login]);
|
||||
}
|
||||
|
||||
/* Supprimer tous les utilisateurs de la table registered_user */
|
||||
export function deleteAllUsers(db: sqlite3.Database) {
|
||||
const deleteAllUsersQuery = `DELETE FROM registered_user`;
|
||||
|
||||
return runDB(db, deleteAllUsersQuery, []);
|
||||
}
|
||||
|
||||
/* Sélectionner tous les utilisateurs de la table registered_user */
|
||||
export function selectAllUsers(db: sqlite3.Database): Promise<unknown[]> {
|
||||
const selectAllUsersQuery = `SELECT * FROM registered_user`;
|
||||
|
||||
return allDB(db, selectAllUsersQuery);
|
||||
}
|
||||
|
||||
/* Sélectionner un utilisateur par son login */
|
||||
export function selectUserByLogin(db: sqlite3.Database, login: string) {
|
||||
const selectUserByLoginQuery = `SELECT * FROM registered_user WHERE login = ?`;
|
||||
|
||||
return getDB(db, selectUserByLoginQuery, [login]);
|
||||
}
|
||||
|
||||
/* Sélectionner un utilisateur par son ID */
|
||||
export function selectUserById(db: sqlite3.Database, id: number) {
|
||||
const selectUserByIdQuery = `SELECT * FROM registered_user WHERE id_user = ?`;
|
||||
|
||||
return getDB(db, selectUserByIdQuery, [id]);
|
||||
}
|
||||
|
||||
/////////////////////////// Gestion des Languages ///////////////////////////
|
||||
// CREATE TABLE language (
|
||||
// id_language SERIAL PRIMARY KEY,
|
||||
// designation VARCHAR(30) NOT NULL,
|
||||
// version INT NOT NULL,
|
||||
// );
|
||||
|
||||
/* Créer la table language dans la base de données */
|
||||
export function createLanguageTable(db: sqlite3.Database): Promise<void> {
|
||||
const tableLanguage = `CREATE TABLE IF NOT EXISTS language (id_language INTEGER PRIMARY KEY AUTOINCREMENT, designation TEXT NOT NULL, version INTEGER NOT NULL)`;
|
||||
return runDB(db, tableLanguage, []);
|
||||
}
|
||||
|
||||
/* Insérer un language dans la table language */
|
||||
export function insertLanguage(
|
||||
db: sqlite3.Database,
|
||||
designation: string,
|
||||
version: number
|
||||
) {
|
||||
const insertLanguageQuery = `INSERT INTO language (designation, version) VALUES (?, ?)`;
|
||||
|
||||
return runDB(db, insertLanguageQuery, [designation, version]);
|
||||
}
|
||||
|
||||
/* Modifier la designation d'un language dans la table language */
|
||||
export function updateLanguageDesignation(
|
||||
db: sqlite3.Database,
|
||||
id: number,
|
||||
newDesignation: string
|
||||
) {
|
||||
const updateLanguageDesignationQuery = `UPDATE language SET designation = ? WHERE id_language = ?`;
|
||||
|
||||
return runDB(db, updateLanguageDesignationQuery, [newDesignation, id]);
|
||||
}
|
||||
|
||||
/* Modifier la version d'un language dans la table language */
|
||||
export function updateLanguageVersion(
|
||||
db: sqlite3.Database,
|
||||
id: number,
|
||||
newVersion: number
|
||||
) {
|
||||
const updateLanguageVersionQuery = `UPDATE language SET version = ? WHERE id_language = ?`;
|
||||
|
||||
return runDB(db, updateLanguageVersionQuery, [newVersion, id]);
|
||||
}
|
||||
|
||||
/* Supprimer un language de la table language par son ID */
|
||||
export function deleteLanguage(db: sqlite3.Database, id: number) {
|
||||
const deleteLanguageQuery = `DELETE FROM language WHERE id_language = ?`;
|
||||
|
||||
return runDB(db, deleteLanguageQuery, [id]);
|
||||
}
|
||||
|
||||
/* Supprimer tous les languages de la table language */
|
||||
export function deleteAllLanguages(db: sqlite3.Database) {
|
||||
const deleteAllLanguagesQuery = `DELETE FROM language`;
|
||||
|
||||
return runDB(db, deleteAllLanguagesQuery, []);
|
||||
}
|
||||
|
||||
/* Sélectionner tous les languages de la table language */
|
||||
export function selectAllLanguages(db: sqlite3.Database): Promise<unknown[]> {
|
||||
const selectAllLanguagesQuery = `SELECT * FROM language`;
|
||||
|
||||
return allDB(db, selectAllLanguagesQuery);
|
||||
}
|
||||
|
||||
/* Sélectionner un language par son ID */
|
||||
export function selectLanguageById(db: sqlite3.Database, id: number) {
|
||||
const selectLanguageByIdQuery = `SELECT * FROM language WHERE id_language = ?`;
|
||||
|
||||
return getDB(db, selectLanguageByIdQuery, [id]);
|
||||
}
|
||||
|
||||
/////////////////////////// Gestion des works ///////////////////////////
|
||||
// CREATE TABLE work (
|
||||
// id_work SERIAL PRIMARY KEY,
|
||||
// link CHAR(36) NOT NULL,
|
||||
// user_id INT REFERENCES registered_user(id_user),
|
||||
// language_id INT NOT NULL REFERENCES language(id_language)
|
||||
// content TEXT NOT NULL,
|
||||
// );
|
||||
|
||||
/* Créer la table work dans la base de données */
|
||||
export function createWorkTable(db: sqlite3.Database): Promise<void> {
|
||||
const tableWork = `CREATE TABLE IF NOT EXISTS work (id_work INTEGER PRIMARY KEY AUTOINCREMENT, link CHAR(36) NOT NULL, user_id INTEGER REFERENCES registered_user(id_user), language_id INTEGER NOT NULL REFERENCES language(id_language), content TEXT NOT NULL)`;
|
||||
return runDB(db, tableWork, []);
|
||||
}
|
||||
|
||||
/* Insérer un work dans la table work */
|
||||
export function insertWork(
|
||||
db: sqlite3.Database,
|
||||
link: string,
|
||||
user_id: number,
|
||||
language_id: number,
|
||||
content: string
|
||||
) {
|
||||
const insertWorkQuery = `INSERT INTO work (link, user_id, language_id, content) VALUES (?, ?, ?, ?)`;
|
||||
|
||||
return runDB(db, insertWorkQuery, [link, user_id, language_id, content]);
|
||||
}
|
||||
|
||||
/* Sélectionner tous les works de la table work */
|
||||
export function selectAllWorks(db: sqlite3.Database): Promise<unknown[]> {
|
||||
const selectAllWorksQuery = `SELECT * FROM work`;
|
||||
|
||||
return allDB(db, selectAllWorksQuery);
|
||||
}
|
||||
|
||||
/* Supprimer tous les works de la table work */
|
||||
export function deleteAllWorks(db: sqlite3.Database) {
|
||||
const deleteAllWorksQuery = `DELETE FROM work`;
|
||||
|
||||
return runDB(db, deleteAllWorksQuery, []);
|
||||
}
|
||||
|
||||
/* Supprimer un work de la table work */
|
||||
export function deleteWork(db: sqlite3.Database, id: number) {
|
||||
const deleteWorkQuery = `DELETE FROM work WHERE id_work = ?`;
|
||||
|
||||
return runDB(db, deleteWorkQuery, [id]);
|
||||
}
|
||||
|
||||
/* Sélectionner un work par son ID */
|
||||
export function selectWorkById(db: sqlite3.Database, id: number) {
|
||||
const selectWorkByIdQuery = `SELECT * FROM work WHERE id_work = ?`;
|
||||
|
||||
return getDB(db, selectWorkByIdQuery, [id]);
|
||||
}
|
Loading…
Reference in new issue