Mise à jour de routes et models

This commit is contained in:
Rampeur
2025-08-10 09:15:18 +02:00
parent 61fdac2434
commit b68dd4c800
28 changed files with 1211 additions and 617 deletions
+30 -44
View File
@@ -1,50 +1,36 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
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
});
const Article = sequelize.define("Article", {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
slug: { type: DataTypes.STRING, allowNull: false },
title: { type: DataTypes.STRING, allowNull: false },
description: { type: DataTypes.TEXT },
body: { type: DataTypes.TEXT, allowNull: false },
createdAt: { type: DataTypes.DATE, allowNull: true },
updatedAt: { type: DataTypes.DATE, allowNull: true }
}, {
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.beforeValidate((article) => {
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
};
Article.prototype.toJSONFor = function () {
return {
id: this.id,
slug: this.slug,
title: this.title,
description: this.description,
body: this.body
};
return Article;
};
module.exports = Article;
+22 -32
View File
@@ -1,35 +1,25 @@
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
});
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
Comment.prototype.toJSONFor = function () {
return {
id: this.comment_id,
body: this.comment_body
};
const Comment = sequelize.define("Comment", {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
body: { type: DataTypes.TEXT, allowNull: false },
createdAt: { type: DataTypes.DATE, allowNull: true },
updatedAt: { type: DataTypes.DATE, allowNull: true }
}, {
sequelize,
timestamps: false,
modelName: 'Comment',
tableName: 'comment',
createdAt: true,
updatedAt: true
});
Comment.prototype.toJSONFor = function () {
return {
id: this.id,
body: this.body
};
};
return Comment;
};
module.exports = Comment;
+20 -31
View File
@@ -1,34 +1,23 @@
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
});
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
Tag.prototype.toJSONFor = function () {
return {
id: this.tag_id,
name: this.tag_name
};
const Tag = sequelize.define("Tag", {
name: { type: DataTypes.STRING, primaryKey: true, allowNull: false },
createdAt: { type: DataTypes.DATE, allowNull: true },
updatedAt: { type: DataTypes.DATE, allowNull: true }
}, {
sequelize,
timestamps: false,
modelName: 'Tag',
tableName: 'tag',
createdAt: true,
updatedAt: true
});
Tag.prototype.toJSONFor = function () {
return {
name: this.name
};
};
return Tag;
};
module.exports = Tag;
+56 -69
View File
@@ -1,74 +1,61 @@
'use strict';
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
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
});
/**
* "email": "jake@jake.jake",
"token": "jwt.token.here",
"username": "jake",
"bio": "I work at statefarm",
"image": null
*/
User.prototype.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
const User = sequelize.define("User", {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
email: { type: DataTypes.STRING, allowNull: false, unique: true },
username: { type: DataTypes.STRING, allowNull: false, unique: true },
firstname: { type: DataTypes.STRING },
lastname: { type: DataTypes.STRING },
bio: { type: DataTypes.TEXT, allowNull: true },
image: { type: DataTypes.TEXT, allowNull: true },
role: { type: DataTypes.STRING },
password: { type: DataTypes.STRING, allowNull: false },
createdAt: { type: DataTypes.DATE, allowNull: true },
updatedAt: { type: DataTypes.DATE, allowNull: true }
}, {
sequelize,
timestamps: false,
modelName: 'User',
tableName: 'user',
createdAt: true,
updatedAt: true
});
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;
Model.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.id,
email: this.email,
username: this.username,
firstname: this.firstname,
lastname: this.lastname,
bio: this.bio,
image: this.image,
role: this.role
};
};
module.exports = User;
+10 -10
View File
@@ -16,11 +16,11 @@ module.exports = (sequelize, DataTypes) => {
}
}
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 }
articleId: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
slug: { type: DataTypes.STRING, allowNull: false },
title: { type: DataTypes.STRING, allowNull: false },
description: { type: DataTypes.TEXT },
body: { type: DataTypes.TEXT, allowNull: false }
}, {
sequelize,
timestamps: false,
@@ -36,11 +36,11 @@ module.exports = (sequelize, DataTypes) => {
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
articleId: this.articleId,
slug: this.slug,
title: this.title,
description: this.description,
body: this.body
};
};
+4 -4
View File
@@ -13,8 +13,8 @@ module.exports = (sequelize, DataTypes) => {
}
}
Comment.init({
comment_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
comment_body: { type: DataTypes.TEXT, allowNull: false }
commentId: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
body: { type: DataTypes.TEXT, allowNull: false }
}, {
sequelize,
timestamps: false,
@@ -26,8 +26,8 @@ module.exports = (sequelize, DataTypes) => {
Comment.prototype.toJSONFor = function () {
return {
id: this.comment_id,
body: this.comment_body
commentId: this.commentId,
body: this.body
};
};
+2 -3
View File
@@ -13,7 +13,7 @@ module.exports = (sequelize, DataTypes) => {
}
}
Tag.init({
tag_name: { type: DataTypes.STRING, allowNull: false, primaryKey: true }
name: { type: DataTypes.STRING, primaryKey: true, allowNull: false }
}, {
sequelize,
timestamps: false,
@@ -25,8 +25,7 @@ module.exports = (sequelize, DataTypes) => {
Tag.prototype.toJSONFor = function () {
return {
id: this.tag_id,
name: this.tag_name
name: this.name
};
};
+17 -26
View File
@@ -17,19 +17,15 @@ module.exports = (sequelize, DataTypes) => {
}
}
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}
userId: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false},
email: {type: DataTypes.STRING, allowNull: false, unique: true},
username: {type: DataTypes.STRING, allowNull: false, unique: true},
firstname: {type: DataTypes.STRING},
lastname: {type: DataTypes.STRING},
bio: {type: DataTypes.TEXT, allowNull: true},
image: {type: DataTypes.TEXT, allowNull: true},
role: {type: DataTypes.STRING},
password: {type: DataTypes.STRING, allowNull: false}
}, {
sequelize,
timestamps: false,
@@ -55,19 +51,14 @@ module.exports = (sequelize, DataTypes) => {
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
userId: this.userId,
email: this.email,
username: this.username,
firstname: this.firstname,
lastname: this.lastname,
bio: this.bio,
image: this.image,
role: this.role
};
};
return User;