Refactoring et ajout de routes Hero Wars

This commit is contained in:
2025-11-27 10:50:17 +01:00
parent c9ce3f343e
commit 60feffd929
47 changed files with 1224 additions and 236 deletions
+40 -1
View File
@@ -31,7 +31,7 @@ class ArticleService {
const articles = await Article.findAll({
where: { slug },
include: [
{ model: DB.Tag, as: "tagNameTagTagLists", attributes: ["name"], through: { attributes: [] } },
{ 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"] } },
],
@@ -50,6 +50,45 @@ class ArticleService {
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;
+34
View File
@@ -0,0 +1,34 @@
const DB = require('../database/mysql');
const { HeroWar } = DB;
class HeroWarService {
static async createMember(data) {
return HeroWar.create(data);
}
static async getAllMembers() {
return HeroWar.findAll({
order: [['createdAt', 'DESC']],
});
}
static async getMemberById(id) {
return HeroWar.findByPk(id);
}
static async getMemberByName(email) {
return HeroWar.findOne({ where: { email: email } });
}
static async updateMember(data) {
return HeroWar.update(data);
}
static async deleteMember(data) {
return HeroWar.delete(data);
}
}
module.exports = HeroWarService;
+66
View File
@@ -0,0 +1,66 @@
var mongoose = require('mongoose'),
HWClan = mongoose.model('HWClan');
class HWClanService {
static async createClan(data) {
let clan = new HWClan();
Object.assign(clan, data);
return clan.save().then(function () {
return HWClanService.getClanById(data.id);
});
}
static async getAllClans(query, limit, offset) {
return Promise.all([
HWClan.find(query)
.skip(Number(offset))
.limit(Number(limit))
.sort({ title: 'asc' })
.populate('author')
.exec(),
HWClan.count(query).exec()
]).then(function (results) {
const clans = results[0];
const clansCount = results[1];
return {
clans: clans,
clansCount: clansCount
};
});
}
static async getClanById(id) {
return HWClan.findOne({ id: id })
.populate('author')
.then(function (clan) {
if (!clan) {
return null;
}
return clan;
});
}
static async getClanByTitle(title) {
return HWClan.findOne({ title: title })
.populate('author')
.then(function (clan) {
if (!clan) {
return null;
}
return clan;
});
}
static async updateClan(data) {
return data.save().then(function () {
return HWClanService.getClanById(data.id);
});
}
static async deleteClan(data) {
return data.remove();
}
}
module.exports = HWClanService;
+66
View File
@@ -0,0 +1,66 @@
var mongoose = require('mongoose'),
HWMember = mongoose.model('HWMember');
class HWMemberService {
static async createMember(data) {
let member = new HWMember();
Object.assign(member, data);
return member.save().then(function () {
return HWMemberService.getMemberById(data.id);
});
}
static async getAllMembers(query, limit, offset) {
return Promise.all([
HWMember.find(query)
.skip(Number(offset))
.limit(Number(limit))
.sort({ name: 'asc' })
.populate('author')
.exec(),
HWMember.count(query).exec()
]).then(function (results) {
const members = results[0];
const membersCount = results[1];
return {
members: members,
membersCount: membersCount
};
});
}
static async getMemberById(id) {
return HWMember.findOne({ id: id })
.populate('author')
.then(function (member) {
if (!member) {
return null;
}
return member;
});
}
static async getMemberByName(name) {
return HWMember.findOne({ name: name })
.populate('author')
.then(function (member) {
if (!member) {
return null;
}
return member;
});
}
static async updateMember(data) {
return data.save().then(function () {
return HWMemberService.getMemberById(data.id);
});
}
static async deleteMember(data) {
return data.remove();
}
}
module.exports = HWMemberService;
+5 -1
View File
@@ -1,5 +1,9 @@
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.UserService = require('./user.service');
exports.HeroWarService = require('./herowars.service');
exports.HWClanService = require('./hwclan.service');
exports.HWMemberService = require('./hwmember.service');
+76
View File
@@ -0,0 +1,76 @@
const DB = require('../database/mysql');
const { Product } = DB;
class ProductService {
static async createProduct(data) {
return Product.create(data);
}
static async getAllProducts() {
return Product.findAll({
order: [['createdAt', 'DESC']],
});
}
static async getProductsByUserId(ownerId) {
return Product.findAll({
where: { ownerId },
include: [
{
model: DB.User,
as: 'owner',
attributes: ['username', 'email'],
},
],
order: [['createdAt', 'DESC']],
});
}
static async getProductBySlug(slug) {
const products = await Product.findAll({
where: { slug },
include: [
{ model: DB.Category, as: "productCategories", attributes: ["name"], through: { attributes: [] } },
{ model: DB.Tag, as: "productTags", attributes: ["name"], through: { attributes: [] } },
{ model: DB.User, as: "owner", attributes: { exclude: ["email", "phone", "salt", "hash"] } }
],
order: [['createdAt', 'DESC']],
});
//let product = products[0];
return products[0];
}
static async getProductsByCategory(category) {
const limit = 100, offset = 0;
const products = await Product.findAndCountAll({
include: [
{ model: DB.Brand, as: "brand", attributes: ["name", "description"] },
{ model: DB.Category, as: "productCategories", attributes: ["slug", "name", "description"], through: { attributes: [] }, ...(category && { where: { slug: category } }), },
{ model: DB.Tag, as: "productTags", attributes: ["name"], through: { attributes: [] } },
{ model: DB.User, as: "owner", attributes: { exclude: ["email", "phone", "salt", "hash"] } }
],
limit: parseInt(limit),
offset: offset * limit,
order: [["createdAt", "DESC"]],
//where: { categoryId: 4 },
distinct: true,
});
/*
const products = await Product.findAll({
where: { category },
include: [
{ model: DB.Category, as: "productCategories", attributes: ["name"], through: { attributes: [] } },
{ model: DB.Tag, as: "productTags", attributes: ["name"], through: { attributes: [] } },
{ model: DB.User, as: "owner", attributes: { exclude: ["email", "phone", "salt", "hash"] } }
],
order: [['createdAt', 'DESC']],
});
*/
return products;
}
}
module.exports = ProductService;