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
+1 -1
View File
@@ -2,7 +2,7 @@ APP_ENV="development"
NODE_ENV="development"
DEBUG=false
SERVER_PORT="3200"
SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
JWT_SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
SESSION_LIFETIME="3600"
SENDGRID_API_KEY="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"
SENDGRID_FROM_MAIL="contact@rampeur.fr"
+1 -1
View File
@@ -2,7 +2,7 @@ APP_ENV="development"
NODE_ENV="development"
DEBUG=false
SERVER_PORT="3200"
SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
JWT_SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
SESSION_LIFETIME=3600
SENDGRID_API_KEY="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"
SENDGRID_FROM_MAIL="contact@rampeur.fr"
+1 -1
View File
@@ -2,7 +2,7 @@ APP_ENV="local"
NODE_ENV="development"
DEBUG=false
SERVER_PORT="3200"
SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
JWT_SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
SESSION_LIFETIME=3600
SENDGRID_API_KEY="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"
SENDGRID_FROM_MAIL="contact@rampeur.fr"
+1 -1
View File
@@ -2,7 +2,7 @@ APP_ENV="production"
NODE_ENV="production"
DEBUG=false
SERVER_PORT="3200"
SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
JWT_SECRET="$2b$10$2.JkQCRUa3fXESy/WyKVWO"
SESSION_LIFETIME=3600
SENDGRID_API_KEY="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"
SENDGRID_FROM_MAIL="contact@rampeur.fr"
+6
View File
@@ -20,6 +20,8 @@ module.exports.getProfile = asyncHandler(async (req, res, next) => {
const profile = {
username,
firstname: user.firstname,
lastname: user.lastname,
bio: user.bio,
image: user.image,
following: isFollowing,
@@ -42,6 +44,8 @@ module.exports.followUser = asyncHandler(async (req, res, next) => {
const profile = {
username: username,
firstname: userToFollow.dataValues.firstname,
lastname: userToFollow.dataValues.lastname,
bio: userToFollow.dataValues.bio,
image: userToFollow.dataValues.image,
following: true,
@@ -65,6 +69,8 @@ module.exports.unfollowUser = asyncHandler(async (req, res, next) => {
const profile = {
username: username,
firstname: userToUnfollow.dataValues.firstname,
lastname: userToUnfollow.dataValues.lastname,
bio: userToUnfollow.dataValues.bio,
image: userToUnfollow.dataValues.image,
following: false,
+5 -1
View File
@@ -4,16 +4,20 @@ const ErrorResponse = require("../util/errorResponse");
const { sign } = require("../util/jwt");
module.exports.createUser = asyncHandler(async (req, res, next) => {
const { email, password, username } = req.body.user;
const { email, password, username, firstname, lastname } = req.body.user;
fieldValidation(email, next);
fieldValidation(password, next);
fieldValidation(username, next);
fieldValidation(firstname, next);
fieldValidation(lastname, next);
const user = await User.create({
email: email,
password: password,
username: username,
firstname: firstname,
lastname: lastname,
});
if (user.dataValues.password) {
-27
View File
@@ -1,27 +0,0 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
const slugify = require("slugify");
const Article = sequelize.define("Article", {
slug: {
type: DataTypes.STRING,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
},
body: {
type: DataTypes.TEXT,
allowNull: false,
},
});
Article.beforeValidate((article) => {
article.slug = slugify(article.title, { lower: true });
});
module.exports = Article;
-11
View File
@@ -1,11 +0,0 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
const Comment = sequelize.define("Comment", {
body: {
type: DataTypes.TEXT,
allowNull: false,
},
});
module.exports = Comment;
-16
View File
@@ -1,16 +0,0 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
const Tag = sequelize.define(
"Tag",
{
name: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
},
},
{ timestamps: false }
);
module.exports = Tag;
-58
View File
@@ -1,58 +0,0 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
const bcrypt = require("bcryptjs");
/**
* "email": "jake@jake.jake",
"token": "jwt.token.here",
"username": "jake",
"bio": "I work at statefarm",
"image": null
*/
const User = sequelize.define(
"User",
{
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
bio: {
type: DataTypes.TEXT,
allowNull: true,
},
image: {
type: DataTypes.TEXT,
allowNull: true,
},
},
{ timestamps: false }
);
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;
});
module.exports = User;
+22 -36
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 }
}, {
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.beforeValidate((article) => {
article.slug = slugify(article.title, { lower: true });
});
});
Article.prototype.toJSONFor = function () {
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
id: this.id,
slug: this.slug,
title: this.title,
description: this.description,
body: this.body
};
};
return Article;
};
module.exports = Article;
+15 -25
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 }
}, {
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
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 () {
Comment.prototype.toJSONFor = function () {
return {
id: this.comment_id,
body: this.comment_body
id: this.id,
body: this.body
};
};
return Comment;
};
module.exports = Comment;
+13 -24
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 }
}, {
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
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 () {
Tag.prototype.toJSONFor = function () {
return {
id: this.tag_id,
name: this.tag_name
name: this.name
};
};
return Tag;
};
module.exports = Tag;
+39 -52
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.
/**
* "email": "jake@jake.jake",
"token": "jwt.token.here",
"username": "jake",
"bio": "I work at statefarm",
"image": null
*/
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}
}, {
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
});
});
User.prototype.matchPassword = async function (enteredPassword) {
Model.prototype.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
};
const DEFAULT_SALT_ROUNDS = 10;
const DEFAULT_SALT_ROUNDS = 10;
User.addHook("beforeCreate", async (user) => {
User.addHook("beforeCreate", async (user) => {
const encryptedPassword = await bcrypt.hash(
user.password,
DEFAULT_SALT_ROUNDS
);
user.password = encryptedPassword;
});
});
User.prototype.toJSONFor = function () {
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
id: this.id,
email: this.email,
username: this.username,
firstname: this.firstname,
lastname: this.lastname,
bio: this.bio,
image: this.image,
role: this.role
};
};
return User;
};
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;
+1 -1
View File
@@ -4,7 +4,7 @@
"NODE_ENV" : "development",
"DEBUG": false,
"SERVER_PORT": "3200",
"SECRET": "$2b$10$aEsAUG7Dy8dcTORi1vaQle",
"JWT_SECRET": "$2b$10$aEsAUG7Dy8dcTORi1vaQle",
"SESSION_LIFETIME": "3600",
"MONGODB_URI": "mongodb://localhost:27017/adastradb",
"SENDGRID_API_KEY": "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",
+826 -1
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -34,6 +34,7 @@
"devDependencies": {
"@eslint/js": "^9.0.0",
"eslint": "^9.0.0",
"prettier": "^2.8.0"
"prettier": "^2.8.0",
"sequelize-cli": "^6.6.3"
}
}
-30
View File
@@ -1,30 +0,0 @@
const express = require("express");
const router = express.Router();
const {
getArticle,
getArticles,
createArticle,
articlesFeed,
addFavoriteArticle,
deleteFavoriteArticle,
deleteArticle,
updateArticle,
} = require("../controllers/articles");
const { protect } = require("../middlewares/auth");
router
.route("/articles")
.get(protect, getArticles)
.post(protect, createArticle);
router.route("/articles/feed").get(protect, articlesFeed);
router
.route("/articles/:slug")
.get(protect, getArticle)
.put(protect, updateArticle)
.delete(protect, deleteArticle);
router
.route("/articles/:slug/favorite")
.post(protect, addFavoriteArticle)
.delete(protect, deleteFavoriteArticle);
module.exports = router;
-23
View File
@@ -1,23 +0,0 @@
const express = require("express");
const router = express.Router();
const {
getComment,
getComments,
createComment,
deleteComment,
} = require("../controllers/comments");
const { protect } = require("../middlewares/auth");
router
.route("/articles/:slug/comments")
.get(protect, getComments)
.post(protect, createComment);
router
.route("/articles/:slug/comments/:id")
.get(protect, getComment)
.delete(protect, deleteComment);
module.exports = router;
-16
View File
@@ -1,16 +0,0 @@
const express = require("express");
const router = express.Router();
const {
getProfile,
followUser,
unfollowUser,
} = require("../controllers/profiles");
const { protect } = require("../middlewares/auth");
router.route("/profiles/:username").get(protect, getProfile);
router
.route("/profiles/:username/follow")
.post(protect, followUser)
.delete(protect, unfollowUser);
module.exports = router;
-10
View File
@@ -1,10 +0,0 @@
const express = require("express");
const router = express.Router();
const { getTags } = require("../controllers/tags");
const { protect } = require("../middlewares/auth");
router.route("/tags/").get(getTags);
module.exports = router;
-16
View File
@@ -1,16 +0,0 @@
const express = require("express");
const router = express.Router();
const {
createUser,
loginUser,
getCurrentUser,
updateUser,
} = require("../controllers/users");
const { protect } = require("../middlewares/auth");
router.post("/users", createUser);
router.post("/users/login", loginUser);
router.route("/user").get(protect, getCurrentUser).put(protect, updateUser);
module.exports = router;
+59 -36
View File
@@ -1,25 +1,28 @@
var appEnv = process.env.APP_ENV;
if (appEnv == undefined) {
appEnv = 'development';
}
const dotenv = require("dotenv");
dotenv.config({ path: `.env.${appEnv}` });
require('dotenv').config({ path: `.env.${appEnv}` });
const { promisify } = require('util');
const express = require("express");
const sequelize = require("./util/database");
const chalk = require('chalk');
const morgan = require("morgan");
const colors = require("colors");
const utils = require('./routes/utils');
const { errorHandler } = require("./middlewares/errorHandler");
// Import Models
var isProduction = appEnv === 'production';
/*
var db = require("./models");
*/
// 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();
@@ -27,11 +30,26 @@ const app = express();
app.use(express.json());
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
} else {
morgan.token('statusColor', (req, res) => {
let status = (typeof res.headersSent !== 'boolean' ? Boolean(res.header) : res.headersSent)
? res.statusCode
: undefined
return status >= 500 ? chalk.red(status)
: status >= 429 ? chalk.magenta(status)
: status >= 400 ? chalk.yellow(status)
: status >= 300 ? chalk.cyan(status)
: status >= 200 ? chalk.green(status)
: chalk.underline(status);
});
app.use(morgan('[:date[iso]] ":method :url HTTP/:http-version" :statusColor :res[content-length] ":referrer" ":user-agent" :remote-addr'));
//app.use(require('method-override')());
//app.use(express.static(__dirname + '/public'));
} else {
app.use(morgan("dev"));
}
// CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
@@ -46,12 +64,13 @@ app.use((req, res, next) => {
next();
});
// Route files
const users = require("./routes/users");
const profiles = require("./routes/profiles");
const articles = require("./routes/articles");
const comments = require("./routes/comments");
const tags = require("./routes/tags");
const users = require("./routes/api/users");
const profiles = require("./routes/api/profiles");
const articles = require("./routes/api/articles");
const comments = require("./routes/api/comments");
const tags = require("./routes/api/tags");
// Mount routers
app.use(users);
@@ -60,42 +79,39 @@ app.use(articles);
app.use(comments);
app.use(tags);
const PORT = process.env.PORT || process.env.SERVER_PORT;
//app.use(require('./routes'));
app.use(errorHandler);
console.log(`User: ${User}`);
//console.log(`User: ${User}`);
// Relations
db.User.belongsToMany(User, {
User.belongsToMany(User, {
as: "followers",
through: "Followers",
foreignKey: "userId",
timestamps: false,
});
db.User.belongsToMany(User, {
User.belongsToMany(User, {
as: "following",
through: "Followers",
foreignKey: "followerId",
timestamps: false,
});
db.User.hasMany(Article, {
User.hasMany(Article, {
foreignKey: "authorId",
onDelete: "CASCADE",
});
Article.belongsTo(User, { as: "author", foreignKey: "authorId" });
User.hasMany(Comment, {
foreignKey: "authorId",
onDelete: "CASCADE",
});
Comment.belongsTo(User, { as: "author", foreignKey: "authorId" });
Article.hasMany(Comment, {
foreignKey: "articleId",
onDelete: "CASCADE",
});
Comment.belongsTo(Article, { foreignKey: "articleId" });
User.belongsToMany(Article, {
as: "favorites",
through: "Favorites",
@@ -106,7 +122,6 @@ Article.belongsToMany(User, {
foreignKey: "articleId",
timestamps: false,
});
Article.belongsToMany(Tag, {
through: "TagLists",
as: "tagLists",
@@ -119,24 +134,32 @@ Tag.belongsToMany(Article, {
uniqueKey: false,
timestamps: false,
});
/*
const sync = async () => await sequelize.sync({ force: true });
sync().then(() => {
User.create({
email: "test@test.com",
password: "123456",
username: "neo",
email: "jgautier.webdev@gmail.com",
username: "adastra",
firstname: "Julien",
lastname: "Gautier",
password: "DKxp24PSnr",
role: "admin"
});
User.create({
email: "test2@test.com",
password: "123456",
username: "celeb_neo",
email: "rampeur@gmail.com",
username: "solide",
firstname: "Rampeur",
lastname: "Solide",
password: "DKxp24PSnr",
role: "user"
});
});
*/
const startServer = async () => {
const port = process.env.SERVER_PORT || 3200;
await promisify(app.listen).bind(app)(port);
console.log(`${utils.getDateTimeISOString()}`, chalk.green(`${process.env.APP_ENV} API server listening on port ${port}`));
}
const server = app.listen(
PORT,
console.log(
`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold
)
);
// finally, let's start our server...
startServer();
+1 -1
View File
@@ -2,7 +2,7 @@ const jwt = require("jsonwebtoken");
module.exports.sign = async (user) => {
const token = await jwt.sign(
{ id: user.id, username: user.username, email: user.email },
{ userId: user.userId, username: user.username, email: user.email },
process.env.JWT_SECRET
);
return token;