Mise à jour et ajout de controllers, routes, services et utilities

This commit is contained in:
2025-11-30 17:54:01 +01:00
parent bcf107cd1f
commit 9355431730
36 changed files with 978 additions and 842 deletions
-94
View File
@@ -1,94 +0,0 @@
const DB = require('../database/mysql');
const { Article } = DB;
class ArticleService {
static async createArticle(data) {
return Article.create(data);
}
static async getAllArticles() {
return Article.findAll({
order: [['createdAt', 'DESC']],
});
}
static async getArticlesByUserId(authorId) {
return Article.findAll({
where: { authorId },
include: [
{
model: DB.User,
as: 'author',
attributes: ['username', 'email'],
},
],
order: [['createdAt', 'DESC']],
});
}
static async getArticleBySlug(slug) {
const articles = await Article.findAll({
where: { slug },
include: [
{ model: DB.Tag, as: "articleTags", attributes: ["name"], through: { attributes: [] } },
{ model: DB.User, as: "author", attributes: { exclude: ["email", "phone", "salt", "hash"] } },
//{ model: DB.User, as: "author", attributes: { include: ["id", "username", "image", "role"] } },
],
order: [['createdAt', 'DESC']],
});
/*
.then(data => {
data.tagList = data.tagNameTagTagLists;
delete data.tagNameTagTagLists;
return data;
});
*/
//let article = articles[0];
//article.tagList = article.tagNameTagTagLists;
//delete article.tagNameTagTagLists;
return articles[0];
}
static appendTagList(articleTags, article) {
const tagList = articleTags.map((tag) => tag.tagName);
if (!article) return tagList;
delete article.dataValues.tagLists; //articleTags
article.dataValues.tagList = tagList;
}
static async appendFavorites(loggedUser, article) {
console.log("article: ", article);
//const favorited = await article.hasUser(loggedUser ? loggedUser : null);
const favorited = await article.hasUserIdUsers(loggedUser ? loggedUser : null);
article.dataValues.favorited = loggedUser ? favorited : false;
//const favoritesCount = await article.countUsers();
const favoritesCount = await article.countFavorites();
article.dataValues.favoritesCount = favoritesCount;
}
static async appendFollowers(loggedUser, toAppend) {
if (toAppend?.author) {
const author = await toAppend.getAuthor();
//const following = await author.hasFollower(loggedUser ? loggedUser : null);
const following = await author.hasFollowerIdUsers(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.hasFollowerIdUsers(
loggedUser ? loggedUser : null
);
toAppend.dataValues.following = loggedUser ? following : false;
const followersCount = await toAppend.countFollowers();
toAppend.dataValues.followersCount = followersCount;
}
}
}
module.exports = ArticleService;
+14 -8
View File
@@ -1,9 +1,15 @@
/**
* Sequelize models for MySQL
*/
exports.ArticleService = require('./mysql/article.service');
exports.CommentService = require('./mysql/comment.service');
exports.ProductService = require('./mysql/product.service');
exports.TagService = require('./mysql/tag.service');
exports.UserService = require('./mysql/user.service');
//exports.HeroWarService = require('./mysql/herowars.service');
exports.ArticleService = require('./article.service');
exports.CommentService = require('./comment.service');
exports.ProductService = require('./product.service');
exports.TagService = require('./tag.service');
exports.UserService = require('./user.service');
exports.HeroWarService = require('./herowars.service');
exports.HWClanService = require('./hwclan.service');
exports.HWMemberService = require('./hwmember.service');
/**
* Mongoose models for MongoDB
*/
exports.ClanService = require('./mongo/clan.service');
exports.MemberService = require('./mongo/member.service');
@@ -1,12 +1,12 @@
var mongoose = require('mongoose'),
HWClan = mongoose.model('HWClan');
class HWClanService {
class ClanService {
static async createClan(data) {
let clan = new HWClan();
Object.assign(clan, data);
return clan.save().then(function () {
return HWClanService.getClanById(data.id);
return ClanService.getClanById(data.id);
});
}
@@ -53,7 +53,7 @@ class HWClanService {
static async updateClan(data) {
return data.save().then(function () {
return HWClanService.getClanById(data.id);
return ClanService.getClanById(data.id);
});
}
@@ -63,4 +63,4 @@ class HWClanService {
}
module.exports = HWClanService;
module.exports = ClanService;
+5
View File
@@ -0,0 +1,5 @@
/**
* Mongoose models for MongoDB
*/
exports.ClanService = require('./clan.service');
exports.MemberService = require('./member.service');
@@ -1,12 +1,12 @@
var mongoose = require('mongoose'),
HWMember = mongoose.model('HWMember');
class HWMemberService {
class MemberService {
static async createMember(data) {
let member = new HWMember();
Object.assign(member, data);
return member.save().then(function () {
return HWMemberService.getMemberById(data.id);
return MemberService.getMemberById(data.id);
});
}
@@ -53,7 +53,7 @@ class HWMemberService {
static async updateMember(data) {
return data.save().then(function () {
return HWMemberService.getMemberById(data.id);
return MemberService.getMemberById(data.id);
});
}
@@ -63,4 +63,4 @@ class HWMemberService {
}
module.exports = HWMemberService;
module.exports = MemberService;
+201
View File
@@ -0,0 +1,201 @@
const slug = require("slug");
const DB = require('../../database/mysql');
const { Article, Tag, User } = DB;
class ArticleService {
static async createArticle(data) {
return Article.create(data);
}
static async deleteArticle(article) {
return article.destroy();
}
static async getAllArticles() {
return Article.findAll({
order: [['createdAt', 'DESC']],
});
}
static async getAllArticlesAndCount(searchOptions) {
return Article.findAndCountAll(searchOptions);
}
static async getIncludeOptionsArticles() {
return ArticleService.getArticleIncludeAssoc();
}
static async getSearchOptionsArticles(tag, author, limit, offset) {
const searchOptions = {
include: [
{
model: Tag,
as: "articleTags",
attributes: ["name"],
through: { attributes: [] }, // ? this will remove the rows from the join table
// where: tag ? { name: tag } : {},
...(tag && { where: { name: tag } }),
},
{
model: User,
as: "author",
attributes: { exclude: ["password", "email", "hash", "salt"] },
// where: author ? { username: author } : {},
...(author && { where: { username: author } }),
},
],
limit: parseInt(limit),
offset: parseInt(offset),
order: [["createdAt", "DESC"]],
distinct: true,
};
return searchOptions;
}
static async getArticlesByUserId(authorId) {
return Article.findAll({
where: { authorId },
include: [
{
model: DB.User,
as: 'author',
attributes: ['username', 'email'],
},
],
order: [['createdAt', 'DESC']],
});
}
static async getArticleBySlug(slug) {
const articles = await Article.findAll({
where: { slug },
include: ArticleService.getArticleIncludeAssoc(),
order: [['createdAt', 'DESC']],
});
/*
.then(data => {
data.tagList = data.tagNameTagTagLists;
delete data.tagNameTagTagLists;
return data;
});
*/
//let article = articles[0];
//article.tagList = article.tagNameTagTagLists;
//delete article.tagNameTagTagLists;
return articles[0];
}
static async getArticleBySlugWithTagList(slug) {
const articles = await Article.findAll({
where: { slug },
include: ArticleService.getArticleIncludeAssoc(),
order: [['createdAt', 'DESC']],
});
if (articles[0]) {
const articleTags = await articles[0].getTagLists();
const tagList = articleTags.map((tag) => tag.tagName);
delete articles[0].dataValues.tagLists;
articles[0].dataValues.tagList = tagList;
}
return articles[0];
}
static async getArticleById(id) {
const articles = await Article.findAll({
where: { id },
include: ArticleService.getArticleIncludeAssoc(),
order: [['createdAt', 'DESC']],
});
return articles[0];
}
static async getArticleByIdWithTagList(id) {
const articles = await Article.findAll({
where: { id },
include: ArticleService.getArticleIncludeAssoc(),
order: [['createdAt', 'DESC']],
});
if (articles[0]) {
const articleTags = await articles[0].getTagLists();
const tagList = articleTags.map((tag) => tag.tagName);
delete articles[0].dataValues.tagLists;
articles[0].dataValues.tagList = tagList;
}
return articles[0];
}
static getArticleIncludeAssoc() {
const assocs = [
{ model: DB.Tag, as: "articleTags", attributes: ["name"], through: { attributes: [] } },
{ model: DB.User, as: "author", attributes: { exclude: ["email", "phone", "salt", "hash"] } },
//{ model: DB.User, as: "author", attributes: { include: ["id", "username", "image", "role"] } },
]
return assocs;
}
static async isSlugUsed(slug) {
const article = await Article.findOne({ where: { slug: slug } });
return !!article;
}
static async appendTagList(article) {
const articleTags = await article.getTagLists();
const tagList = articleTags.map((tag) => tag.tagName);
if (!article) return tagList;
delete article.dataValues.tagLists;
article.dataValues.tagList = tagList;
}
static async appendFavorites(loggedUser, article) {
const favorited = await article.hasArticleFavoriteUsers(loggedUser ? loggedUser : null);
article.dataValues.favorited = loggedUser ? favorited : false;
const favoritesCount = await article.countFavorites();
article.dataValues.favoritesCount = favoritesCount;
}
static async appendFollowers(loggedUser, toAppend) {
// If toAppend has an author (it's an Article)
if (toAppend?.author) {
await this.appendArticleFollowers(loggedUser, toAppend);
} else if (toAppend?.id && toAppend?.hasUserIdUserFollower) {
// toAppend is a User model
await this.appendUserFollowers(loggedUser, toAppend);
}
}
static async appendArticleFollowers(loggedUser, article) {
// Append follower info to an article's author
const author = await article.getAuthor();
const following = loggedUser ? await author.hasUserIdUserFollower(loggedUser) : false;
article.author.dataValues.following = following;
const followersCount = await author.countFollowers();
article.author.dataValues.followersCount = followersCount;
}
static async appendUserFollowers(loggedUser, user) {
// Append follower info to a user
const following = loggedUser ? await user.hasUserIdUserFollower(loggedUser) : false;
user.dataValues.following = following;
const followersCount = await user.countFollowers();
user.dataValues.followersCount = followersCount;
}
static slugify(title) {
return slug(`${title}`);
}
static async updateArticleById(data, id) {
return Article.update(data, { where: { id: id } });
}
}
module.exports = ArticleService;
@@ -1,4 +1,4 @@
const DB = require('../database/mysql');
const DB = require('../../database/mysql');
const { Comment } = DB;
+8
View File
@@ -0,0 +1,8 @@
/**
* Sequelize models for MySQL
*/
exports.ArticleService = require('./article.service');
exports.CommentService = require('./comment.service');
exports.ProductService = require('./product.service');
exports.TagService = require('./tag.service');
exports.UserService = require('./user.service');
@@ -1,4 +1,4 @@
const DB = require('../database/mysql');
const DB = require('../../database/mysql');
const { Product } = DB;
+36
View File
@@ -0,0 +1,36 @@
const DB = require('../../database/mysql');
const { Tag } = DB;
class TagService {
static async createTag(name) {
return Tag.create({ name: name });
}
static async deleteTag(tag) {
return tag.destroy();
}
static async getAllTags() {
return Tag.findAll({
attributes: ["name"],
order: [['createdAt', 'DESC']],
});
}
static async getTagByPk(tag) {
return Tag.findByPk(tag.trim());
}
static async getTagByName(name) {
return Tag.findOne({ where: { name: name } });
}
static async tagIsInDB(name) {
const tag = await Tag.findOne({ where: { name: name } });
return !!tag;
}
}
module.exports = TagService;
+62
View File
@@ -0,0 +1,62 @@
const DB = require('../../database/mysql');
const { User } = DB;
class UserService {
static async createUser(data) {
return User.create(data);
}
static async getAllUsers() {
return User.findAll({
order: [['createdAt', 'DESC']],
});
}
static async getUserById(id) {
return User.findByPk(id);
}
static async getLoggedUserById(id) {
return User.findOne({
attributes: { exclude: ["password", "email", "hash", "salt"] },
where: { id: id }
});
}
static async getUserByEmail(email) {
return User.findOne({ where: { email: email } });
}
static async getUserByUsername(username) {
return User.findOne({ where: { username: username } });
}
static async getFavorites(user, searchOptions) {
return user.getArticleIdArticles(searchOptions);
}
static async getUserFollowed(user) {
// Sequelize generated method for "users this user follows" is named
// `getUserIdUserFollowers()` (see `src/database/relationships/index.js`).
return user.getUserIdUserFollowers();
}
static async getFavoritesCount(user) {
return user.countArticleIdArticles();
}
static async addFavoriteArticle(user, article) {
return user.addArticleIdArticles(article);
}
static async removeFavoriteArticle(user, article) {
return user.removeArticleIdArticles(article);
}
static async updateUser(data) {
return User.update(data);
}
}
module.exports = UserService;
-19
View File
@@ -1,19 +0,0 @@
const DB = require('../database/mysql');
const { Tag } = DB;
class TagService {
static async createTag(data) {
return Tag.create(data);
}
static async getAllTags() {
return Tag.findAll({
attributes: ["name"],
order: [['createdAt', 'DESC']],
});
}
}
module.exports = TagService;
-29
View File
@@ -1,29 +0,0 @@
const DB = require('../database/mysql');
const { User } = DB;
class UserService {
static async createUser(data) {
return User.create(data);
}
static async getAllUsers() {
return User.findAll({
order: [['createdAt', 'DESC']],
});
}
static async getUserById(id) {
return User.findByPk(id);
}
static async getUserByEmail(email) {
return User.findOne({ where: { email: email } });
}
static async updateUser(data) {
return User.update(data);
}
}
module.exports = UserService;