Mise à jour de controleurs, models et routes
This commit is contained in:
@@ -56,7 +56,7 @@ UserSchema.methods.toAuthJSON = function () {
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/users/user.jpg',
|
||||
image: this.image || '/assets/images/avatars/default.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg'
|
||||
};
|
||||
};
|
||||
@@ -71,7 +71,7 @@ UserSchema.methods.toJSONFor = function () {
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/users/user.jpg',
|
||||
image: this.image || '/assets/images/avatars/default.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg'
|
||||
};
|
||||
};
|
||||
@@ -85,7 +85,7 @@ UserSchema.methods.toProfileJSONFor = function (user) {
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/users/user.jpg',
|
||||
image: this.image || '/assets/images/avatars/default.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg',
|
||||
following: user ? user.isFollowing(this._id) : false
|
||||
};
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
const { DataTypes, Model } = require("sequelize");
|
||||
const sequelize = require("../../config/sequelize");
|
||||
const slug = require("slug");
|
||||
|
||||
class Product extends Model { }
|
||||
|
||||
const ProductModel = () => {
|
||||
Product.init(
|
||||
{
|
||||
id: {
|
||||
autoIncrement: true,
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
primaryKey: true
|
||||
},
|
||||
slug: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: false
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: false
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
body: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: false
|
||||
},
|
||||
authorId: {
|
||||
type: DataTypes.STRING(24),
|
||||
allowNull: true,
|
||||
references: {
|
||||
model: 'user',
|
||||
key: 'id'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'product',
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{
|
||||
name: "PRIMARY",
|
||||
unique: true,
|
||||
using: "BTREE",
|
||||
fields: [
|
||||
{ name: "id" },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "authorId",
|
||||
using: "BTREE",
|
||||
fields: [
|
||||
{ name: "authorId" },
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
);
|
||||
|
||||
Product.beforeValidate((product) => {
|
||||
product.slug = slug(`${product.title}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
||||
});
|
||||
|
||||
Product.prototype.toJSONFor = function () {
|
||||
return {
|
||||
id: this.id,
|
||||
slug: this.slug,
|
||||
title: this.title,
|
||||
description: this.description,
|
||||
body: this.body
|
||||
};
|
||||
};
|
||||
|
||||
return Product;
|
||||
};
|
||||
|
||||
module.exports = ProductModel;
|
||||
@@ -33,16 +33,16 @@ const UserModel = () => {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true
|
||||
},
|
||||
bio: {
|
||||
type: DataTypes.TEXT,
|
||||
phone: {
|
||||
type: DataTypes.STRING(20),
|
||||
allowNull: true
|
||||
},
|
||||
image: {
|
||||
type: DataTypes.TEXT,
|
||||
type: DataTypes.STRING(128),
|
||||
allowNull: true
|
||||
},
|
||||
role: {
|
||||
type: DataTypes.STRING(255),
|
||||
type: DataTypes.STRING(20),
|
||||
allowNull: true
|
||||
},
|
||||
salt: {
|
||||
@@ -87,6 +87,14 @@ const UserModel = () => {
|
||||
}
|
||||
);
|
||||
|
||||
User.beforeCreate((user) => {
|
||||
if (typeof user.username === 'undefined') {
|
||||
user.username = `${user.firstname}_${user.lastname}`;
|
||||
}
|
||||
user.setPassword(user.password);
|
||||
user.role = 'User';
|
||||
});
|
||||
|
||||
User.prototype.validPassword = function (password) {
|
||||
let hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
return this.hash === hash;
|
||||
@@ -112,16 +120,13 @@ const UserModel = () => {
|
||||
return {
|
||||
id: this.id,
|
||||
email: this.email,
|
||||
role: this.role,
|
||||
token: this.generateJWT(),
|
||||
username: this.username,
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
/*phone: this.phone,
|
||||
licence: this.licence,
|
||||
poids: this.poids,*/
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/users/user.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg'
|
||||
phone: this.phone,
|
||||
image: this.image || '/assets/images/avatars/default.jpg',
|
||||
role: this.role,
|
||||
token: this.generateJWT()
|
||||
};
|
||||
};
|
||||
|
||||
@@ -132,24 +137,20 @@ const UserModel = () => {
|
||||
username: this.username,
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
bio: this.bio,
|
||||
phone: this.phone,
|
||||
image: this.image,
|
||||
role: this.role
|
||||
};
|
||||
};
|
||||
|
||||
User.prototype.toProfileJSONFor = function (user) {
|
||||
User.prototype.toProfileJSONFor = function () {
|
||||
return {
|
||||
email: this.email,
|
||||
username: this.username,
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
phone: this.phone,
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/users/user.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg',
|
||||
following: user ? user.isFollowing(this.id) : false
|
||||
image: this.image || '/assets/images/avatars/default.jpg'
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -42,6 +42,28 @@ const associate = () => {
|
||||
DB.User.hasMany(DB.Follower, { as: "followers", foreignKey: "userId" });
|
||||
DB.Follower.belongsTo(DB.User, { as: "follower", foreignKey: "followerId" });
|
||||
DB.User.hasMany(DB.Follower, { as: "followerFollowers", foreignKey: "followerId" });
|
||||
|
||||
/*
|
||||
// Relations
|
||||
DB.User.belongsToMany(DB.User, { as: "followers", through: "Followers", foreignKey: "userId", timestamps: false });
|
||||
DB.User.belongsToMany(DB.User, { as: "following", through: "Followers", foreignKey: "followerId", timestamps: false });
|
||||
|
||||
DB.User.hasMany(DB.Article, { foreignKey: "authorId", onDelete: "CASCADE" });
|
||||
DB.Article.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
||||
|
||||
DB.User.hasMany(DB.Comment, { foreignKey: "authorId", onDelete: "CASCADE" });
|
||||
DB.Comment.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
||||
|
||||
DB.Article.hasMany(DB.Comment, { foreignKey: "articleId", onDelete: "CASCADE" });
|
||||
DB.Comment.belongsTo(DB.Article, { foreignKey: "articleId" });
|
||||
|
||||
DB.User.belongsToMany(DB.Article, { as: "favorites", through: "Favorites", timestamps: false });
|
||||
DB.Article.belongsToMany(DB.User, { through: "Favorites", foreignKey: "articleId", timestamps: false });
|
||||
|
||||
DB.Article.belongsToMany(DB.Tag, { through: "TagLists", as: "tagLists", foreignKey: "articleId", timestamps: false, onDelete: "CASCADE" });
|
||||
DB.Tag.belongsToMany(DB.Article, { through: "ArticleTags", uniqueKey: false, timestamps: false });
|
||||
*/
|
||||
|
||||
};
|
||||
|
||||
module.exports = associate;
|
||||
|
||||
Reference in New Issue
Block a user