From 61fdac24340e3f1626085c24e3de3538aeeb8980 Mon Sep 17 00:00:00 2001 From: Rampeur Date: Fri, 8 Aug 2025 20:18:39 +0200 Subject: [PATCH] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/Article.js | 50 ++++++++++++++++++++++++++++ models/Comment.js | 35 +++++++++++++++++++ models/Tag.js | 34 +++++++++++++++++++ models/User.js | 74 +++++++++++++++++++++++++++++++++++++++++ models/index.js | 9 +++-- models/mysql/Article.js | 4 +-- models/mysql/Membre.js | 57 ------------------------------- models/mysql/User.js | 2 +- server.js | 14 ++++---- 9 files changed, 208 insertions(+), 71 deletions(-) create mode 100644 models/Article.js create mode 100644 models/Comment.js create mode 100644 models/Tag.js create mode 100644 models/User.js delete mode 100644 models/mysql/Membre.js diff --git a/models/Article.js b/models/Article.js new file mode 100644 index 0000000..cc08952 --- /dev/null +++ b/models/Article.js @@ -0,0 +1,50 @@ +const slugify = require("slugify"); +//const slug = require("slug"); + +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Article extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + /* define association here */ + } + } + Article.init({ + article_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, + article_slug: { type: DataTypes.STRING, allowNull: false }, + article_title: { type: DataTypes.STRING, allowNull: false }, + article_description: { type: DataTypes.TEXT }, + article_body: { type: DataTypes.TEXT, allowNull: false } + }, { + sequelize, + timestamps: false, + modelName: 'Article', + tableName: 'article', + createdAt: true, + updatedAt: true + }); + + Article.beforeValidate((article) => { + //article.slug = slug(article.title) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36); + article.slug = slugify(article.title, { lower: true }); + + }); + + Article.prototype.toJSONFor = function () { + return { + id: this.article_id, + slug: this.article_slug, + title: this.article_title, + description: this.article_description, + body: this.article_body + }; + }; + + return Article; +}; diff --git a/models/Comment.js b/models/Comment.js new file mode 100644 index 0000000..437faa3 --- /dev/null +++ b/models/Comment.js @@ -0,0 +1,35 @@ +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Comment extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + /* define association here */ + } + } + Comment.init({ + comment_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, + comment_body: { type: DataTypes.TEXT, allowNull: false } + }, { + sequelize, + timestamps: false, + modelName: 'Comment', + tableName: 'comment', + createdAt: true, + updatedAt: true + }); + + Comment.prototype.toJSONFor = function () { + return { + id: this.comment_id, + body: this.comment_body + }; + }; + + return Comment; +}; \ No newline at end of file diff --git a/models/Tag.js b/models/Tag.js new file mode 100644 index 0000000..882f01f --- /dev/null +++ b/models/Tag.js @@ -0,0 +1,34 @@ +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Tag extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + /* define association here */ + } + } + Tag.init({ + tag_name: { type: DataTypes.STRING, allowNull: false, primaryKey: true } + }, { + sequelize, + timestamps: false, + modelName: 'Tag', + tableName: 'tag', + createdAt: true, + updatedAt: true + }); + + Tag.prototype.toJSONFor = function () { + return { + id: this.tag_id, + name: this.tag_name + }; + }; + + return Tag; +}; \ No newline at end of file diff --git a/models/User.js b/models/User.js new file mode 100644 index 0000000..55a2159 --- /dev/null +++ b/models/User.js @@ -0,0 +1,74 @@ +'use strict'; + +const bcrypt = require("bcryptjs"); + +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class User extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + /* define association here */ + } + } + User.init({ + user_id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false}, + user_username: {type: DataTypes.STRING, allowNull: false}, + user_lastname: {type: DataTypes.STRING}, + user_firstname: {type: DataTypes.STRING}, + user_email: {type: DataTypes.STRING}, + user_adresse: {type: DataTypes.STRING}, + user_ville: {type: DataTypes.STRING}, + user_region: {type: DataTypes.STRING}, + user_cp: {type: DataTypes.STRING}, + user_pays: {type: DataTypes.STRING}, + user_tel: {type: DataTypes.STRING}, + user_mobile: {type: DataTypes.STRING}, + user_role: {type: DataTypes.INTEGER} + }, { + sequelize, + timestamps: false, + modelName: 'User', + tableName: 'user', + createdAt: true, + updatedAt: true + }); + + User.prototype.matchPassword = async function (enteredPassword) { + return await bcrypt.compare(enteredPassword, this.password); + }; + + const DEFAULT_SALT_ROUNDS = 10; + + User.addHook("beforeCreate", async (user) => { + const encryptedPassword = await bcrypt.hash( + user.password, + DEFAULT_SALT_ROUNDS + ); + user.password = encryptedPassword; + }); + + User.prototype.toJSONFor = function () { + return { + id: this.mbr_id, + username: this.mbr_pseudo, + lastname: this.mbr_nom, + firstname: this.mbr_prenom, + email: this.mbr_email, + adresse: this.mbr_adresse, + ville: this.mbr_ville, + region: this.mbr_region, + cp: this.mbr_cp, + pays: this.mbr_pays, + tel: this.mbr_tel, + mobile: this.mbr_mobile, + statut: this.mbr_statut + }; + }; + return User; +}; diff --git a/models/index.js b/models/index.js index 74125e7..6683ea3 100644 --- a/models/index.js +++ b/models/index.js @@ -1,5 +1,4 @@ -exports.User = require('./mysql/User'); -exports.Application = require('./mysql/Article'); -exports.Aeronef = require('./mysql/Comment'); -exports.Canopy = require('./mysql/Membre'); -exports.Dropzone = require('./mysql/Tag'); \ No newline at end of file +exports.User = require('./User'); +exports.Article = require('./Article'); +exports.Comment = require('./Comment'); +exports.Tag = require('./Tag'); \ No newline at end of file diff --git a/models/mysql/Article.js b/models/mysql/Article.js index 2027c4d..5294a82 100644 --- a/models/mysql/Article.js +++ b/models/mysql/Article.js @@ -1,5 +1,5 @@ -const slug = require("slug"); +const slugify = require("slugify"); const { Model @@ -31,7 +31,7 @@ module.exports = (sequelize, DataTypes) => { }); Article.beforeValidate((article) => { - article.slug = slug(article.title) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36); + article.slug = slugify(article.title, { lower: true }); }); Article.prototype.toJSONFor = function () { diff --git a/models/mysql/Membre.js b/models/mysql/Membre.js deleted file mode 100644 index e3ce729..0000000 --- a/models/mysql/Membre.js +++ /dev/null @@ -1,57 +0,0 @@ -'use strict'; -const { - Model -} = require('sequelize'); -module.exports = (sequelize, DataTypes) => { - class Membre extends Model { - /** - * Helper method for defining associations. - * This method is not a part of Sequelize lifecycle. - * The `models/index` file will call this method automatically. - */ - static associate(models) { - /* define association here */ - } - } - Membre.init({ - mbr_id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false}, - mbr_pseudo: {type: DataTypes.STRING, allowNull: false}, - mbr_nom: {type: DataTypes.STRING}, - mbr_prenom: {type: DataTypes.STRING}, - mbr_email: {type: DataTypes.STRING}, - mbr_adresse: {type: DataTypes.STRING}, - mbr_ville: {type: DataTypes.STRING}, - mbr_region: {type: DataTypes.STRING}, - mbr_cp: {type: DataTypes.STRING}, - mbr_pays: {type: DataTypes.STRING}, - mbr_tel: {type: DataTypes.STRING}, - mbr_mobile: {type: DataTypes.STRING}, - mbr_statut: {type: DataTypes.INTEGER} - }, { - sequelize, - timestamps: false, - modelName: 'Membre', - tableName: 'membre', - createdAt: false, - updatedAt: false - }); - - Membre.prototype.toJSONFor = function () { - return { - id: this.mbr_id, - pseudo: this.mbr_pseudo, - nom: this.mbr_nom, - prenom: this.mbr_prenom, - email: this.mbr_email, - adresse: this.mbr_adresse, - ville: this.mbr_ville, - region: this.mbr_region, - cp: this.mbr_cp, - pays: this.mbr_pays, - tel: this.mbr_tel, - mobile: this.mbr_mobile, - statut: this.mbr_statut - }; - }; - return Membre; -}; diff --git a/models/mysql/User.js b/models/mysql/User.js index fd9d64e..55a2159 100644 --- a/models/mysql/User.js +++ b/models/mysql/User.js @@ -1,6 +1,6 @@ 'use strict'; -const bcrypt = require('bcrypt'); +const bcrypt = require("bcryptjs"); const { Model diff --git a/server.js b/server.js index 3527a02..510be04 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,5 @@ var appEnv = process.env.APP_ENV; -console.log(`Env: ${appEnv}`); + if (appEnv == undefined) { appEnv = 'development'; } @@ -13,11 +13,13 @@ const colors = require("colors"); const { errorHandler } = require("./middlewares/errorHandler"); // Import Models +/* const User = require("./models/User"); const Article = require("./models/Article"); const Tag = require("./models/Tag"); const Comment = require("./models/Comment"); - +*/ +var db = require("./models/mysql"); const app = express(); @@ -61,22 +63,22 @@ app.use(tags); const PORT = process.env.PORT || process.env.SERVER_PORT; app.use(errorHandler); - +console.log(`User: ${User}`); // Relations -User.belongsToMany(User, { +db.User.belongsToMany(User, { as: "followers", through: "Followers", foreignKey: "userId", timestamps: false, }); -User.belongsToMany(User, { +db.User.belongsToMany(User, { as: "following", through: "Followers", foreignKey: "followerId", timestamps: false, }); -User.hasMany(Article, { +db.User.hasMany(Article, { foreignKey: "authorId", onDelete: "CASCADE", });