Ajout initial des fichiers de la version mysal

This commit is contained in:
Rampeur
2025-08-08 18:34:15 +02:00
parent 29635cdf83
commit 9a534d5a17
30 changed files with 1381 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
const { Sequelize } = require('sequelize');
const Op = Sequelize.Op;
const sequelize = new Sequelize(
process.env.MYSQL_DBNAME,
process.env.MYSQL_DBUSER,
process.env.MYSQL_DBPASS,
{
host: process.env.MYSQL_DBHOST,
port: process.env.MYSQL_DBPORT,
dialect: process.env.MYSQL_DBTYPE,
$like: Op.like,
$not: Op.not
}
);
const checkConnection = async () => {
try {
await sequelize.authenticate();
console.log(`DB Connected`.cyan.underline.bold);
} catch (error) {
console.error("Unable to connect to the database:".red.bold, error);
}
};
checkConnection();
module.exports = sequelize;
+8
View File
@@ -0,0 +1,8 @@
class ErrorResponse extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
}
}
module.exports = ErrorResponse;
+38
View File
@@ -0,0 +1,38 @@
const appendTagList = (articleTags, article) => {
const tagList = articleTags.map((tag) => tag.name);
if (!article) return tagList;
delete article.dataValues.tagLists;
article.dataValues.tagList = tagList;
};
const appendFavorites = async (loggedUser, article) => {
const favorited = await article.hasUser(loggedUser ? loggedUser : null);
article.dataValues.favorited = loggedUser ? favorited : false;
const favoritesCount = await article.countUsers();
article.dataValues.favoritesCount = favoritesCount;
};
const appendFollowers = async (loggedUser, toAppend) => {
//
if (toAppend?.author) {
const author = await toAppend.getAuthor();
const following = await author.hasFollower(loggedUser ? loggedUser : null);
toAppend.author.dataValues.following = loggedUser ? following : false;
const followersCount = await author.countFollowers();
toAppend.author.dataValues.followersCount = followersCount;
//
} else {
const following = await toAppend.hasFollower(
loggedUser ? loggedUser : null
);
toAppend.dataValues.following = loggedUser ? following : false;
const followersCount = await toAppend.countFollowers();
toAppend.dataValues.followersCount = followersCount;
}
};
module.exports = { appendTagList, appendFavorites, appendFollowers };
+14
View File
@@ -0,0 +1,14 @@
const jwt = require("jsonwebtoken");
module.exports.sign = async (user) => {
const token = await jwt.sign(
{ id: user.id, username: user.username, email: user.email },
process.env.JWT_SECRET
);
return token;
};
module.exports.verify = async (token) => {
const decoded = await jwt.verify(token, process.env.JWT_SECRET);
return decoded;
};