From 4c765945b3979965a21a48b37af85b84c590cdb4 Mon Sep 17 00:00:00 2001 From: Julien Gautier Date: Sun, 26 Apr 2026 00:39:29 +0200 Subject: [PATCH] refactor(naming): harmonize controller and route file names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Delete product.controller.js, merge unique functions into products.controller.js - Rename user.controller.js → users.controller.js, user.routes.js → users.routes.js - Remove dead herowars/herowars.routes.js reference from herowars index - Update all import paths accordingly --- src/controllers/herowars.controller.js | 60 ---- src/controllers/product.controller.js | 266 ------------------ src/controllers/products.controller.js | 168 +++++++++++ ...user.controller.js => users.controller.js} | 0 src/routes/api/cms/index.js | 2 +- .../cms/{user.routes.js => users.routes.js} | 2 +- src/routes/api/ecommerce/product.routes.js | 2 +- src/routes/api/herowars/herowars.routes.js | 13 - src/routes/api/herowars/index.js | 1 - 9 files changed, 171 insertions(+), 343 deletions(-) delete mode 100644 src/controllers/herowars.controller.js delete mode 100644 src/controllers/product.controller.js rename src/controllers/{user.controller.js => users.controller.js} (100%) rename src/routes/api/cms/{user.routes.js => users.routes.js} (94%) delete mode 100644 src/routes/api/herowars/herowars.routes.js diff --git a/src/controllers/herowars.controller.js b/src/controllers/herowars.controller.js deleted file mode 100644 index 3fb23d4..0000000 --- a/src/controllers/herowars.controller.js +++ /dev/null @@ -1,60 +0,0 @@ -const { HeroWarService } = require('../services'); -const asyncHandler = require("../middlewares/asyncHandler"); -const ErrorResponse = require("../utils/errorResponse"); - -module.exports.createMember = asyncHandler(async (req, res, next) => { - try { - const { slug, title, description, body, authorId } = req.body; - const member = await HeroWarService.createMember({ - slug, - title, - description, - body, - authorId - }); - - res.status(201).json({ - message: 'Member created successfully.', - data: member, - }); - } catch (err) { - return next(new ErrorResponse(`Something went wrong while creating member.`, 500, err.message)); - } -}); - -module.exports.getAllMembers = asyncHandler(async (req, res, next) => { - try { - const members = await HeroWarService.getAllMembers(); - - res.status(200).json({ data: members }); - } catch (err) { - return next(new ErrorResponse(`Something went wrong while fetching members.`, 500, err.message)); - } -}); - -module.exports.getMember = asyncHandler(async (req, res, next) => { - try { - let member = await HeroWarService.getMemberById(req.member.id); - if (!member) { - return next(new ErrorResponse("Article not found", 404)); - } - } catch (err) { - return next(new ErrorResponse(`Something went wrong while fetching member.`, 500, err.message)); - } -}); - -module.exports.updateMember = asyncHandler(async (req, res, next) => { - try { - const { loggedUser } = req; - if (!loggedUser.id) { - return next(new ErrorResponse("Unauthorized", 401, "Authentication required.")); - } - - let member = await HeroWarService.getMemberById(req.payload.id); - return member.save().then(function () { - return res.json({ user: member.toAuthJSON() }); - }); - } catch (err) { - return next(new ErrorResponse(`Something went wrong while updating member.`, 500, err.message)); - } -}); diff --git a/src/controllers/product.controller.js b/src/controllers/product.controller.js deleted file mode 100644 index 9be5469..0000000 --- a/src/controllers/product.controller.js +++ /dev/null @@ -1,266 +0,0 @@ -const { ProductService } = require('../services'); -//const { where } = require("sequelize"); -const asyncHandler = require("../middlewares/asyncHandler"); -const Product = require("../database/models/mysql/Product"); -const Tag = require("../database/models/mysql/Tag"); -const User = require("../database/models/mysql/User"); -const ErrorResponse = require("../utils/errorResponse"); -const slug = require("slug"); - -const { - appendFollowers, - appendFavorites, - appendTagList, -} = require("../utils/helpers"); - -const includeOptions = [ - { - model: Tag, - as: "tagLists", - attributes: ["name"], - through: { attributes: [] }, - }, - { model: User, as: "author", attributes: { exclude: ["email", "password"] } }, -]; - -module.exports.getProduct = asyncHandler(async (req, res, next) => { - /* - const { loggedUser } = req; - const { slug } = req.params; - - const product = await Product.findOne({ - where: { slug: slug }, - include: includeOptions, - }); - - if (!product) { - return next(new ErrorResponse("Product not found", 404)); - } - - const productTags = await product.getTagLists(); - appendTagList(productTags, product); - await appendFollowers(loggedUser, product); - await appendFavorites(loggedUser, product); - - res.status(200).json({ product }); - */ - const { slug } = req.params; - - let product = await ProductService.getProductBySlug(slug); - - if (!product) { - return next(new ErrorResponse("Product not found", 404)); - } - let tags = product.productTags; - product = product.toJSONFor(); - product.tags = tags; - - res.status(200).json({ product: product }); -}); - -module.exports.getProducts = asyncHandler(async (req, res) => { - const { tag, author, favorited, limit = 20, offset = 0 } = req.query; - const { loggedUser } = req; - - const searchOptions = { - include: [ - { - model: Tag, - as: "tagLists", - 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"] }, - // where: author ? { username: author } : {}, - ...(author && { where: { username: author } }), - }, - ], - limit: parseInt(limit), - offset: parseInt(offset), - order: [["createdAt", "DESC"]], - distinct: true, - }; - - let products = { rows: [], count: 0 }; - - if (favorited) { - const user = await User.findOne({ where: { username: favorited } }); - - products.rows = await user.getFavorites(searchOptions); - products.count = await user.countFavorites(); - } else { - products = await Product.findAndCountAll(searchOptions); - } - - for (let product of products.rows) { - const productTags = await product.getTagLists(); - // const productTags = product.tagLists; - appendTagList(productTags, product); - await appendFollowers(loggedUser, product); - await appendFavorites(loggedUser, product); - - delete product.dataValues.Favorites; - } - - res - .status(200) - .json({ products: products.rows, productsCount: products.count }); -}); - -module.exports.createProduct = asyncHandler(async (req, res, next) => { - const { loggedUser } = req; - - fieldValidation(req.body.product.title, next); - fieldValidation(req.body.product.description, next); - fieldValidation(req.body.product.body, next); - - const { title, description, body, tagList } = req.body.product; - const slugInDB = await Product.findOne({ where: { slug: slug(`${title}`) } }); - if (slugInDB) { - next(new ErrorResponse("Title already exists", 400)); - } - - const product = await Product.create({ - title: title, - description: description, - body: body, - }); - - for (const tag of tagList) { - const tagInDB = await Tag.findByPk(tag.trim()); - - if (tagInDB) { - await product.addProductTag(tagInDB); - } else if (tag.length > 2) { - const newTag = await Tag.create({ name: tag.trim() }); - await product.addProductTag(newTag); - } - } - delete loggedUser.dataValues.token; - - product.dataValues.tagList = tagList; - product.setAuthor(loggedUser); - product.dataValues.author = loggedUser; - await appendFollowers(loggedUser, loggedUser); - await appendFavorites(loggedUser, product); - - res.status(201).json({ product }); -}); - -module.exports.deleteProduct = asyncHandler(async (req, res, next) => { - const { slug } = req.params; - const { loggedUser } = req; - - const product = await Product.findOne({ - where: { slug: slug }, - include: includeOptions, - }); - - if (!product) { - next(new ErrorResponse("Product not found", 404)); - } - - if (product.authorId !== loggedUser.id) { - return next(new ErrorResponse("Unauthorized", 401, "Authentication required.")); - } - - await product.destroy(); - - res.status(200).json({ product }); -}); - -module.exports.updateProduct = asyncHandler(async (req, res, next) => { - const { slug } = req.params; - const { loggedUser } = req; - - const product = await Product.findOne({ - where: { slug: slug }, - include: includeOptions, - }); - - if (!product) { - next(new ErrorResponse("Product not found", 404)); - } - if (product.authorId !== loggedUser.id) { - return next(new ErrorResponse("Unauthorized", 401, "Authentication required.")); - } - - const { title, description, body } = req.body.product; - const slugInDB = await Product.findOne({ - where: { slug: slug(title ? title : product.title) }, - }); - if (slugInDB && slugInDB.slug !== slug) { - return next(new ErrorResponse("Title already exists", 400)); - } - - await product.update({ - title: title ? title : product.title, - description: description ? description : product.description, - body: body ? body : product.body, - }); - - const productTags = await product.getTagLists(); - appendTagList(productTags, product); - await appendFollowers(loggedUser, product); - await appendFavorites(loggedUser, product); - - res.status(200).json({ product }); -}); - - -module.exports.addFavoriteProduct = asyncHandler(async (req, res, next) => { - const { loggedUser } = req; - const { slug } = req.params; - - const product = await Product.findOne({ - where: { slug: slug }, - include: includeOptions, - }); - - if (!product) { - return next(new ErrorResponse("Product not found", 404)); - } - - await loggedUser.addFavorite(product); - - const productTags = await product.getTagLists(); - appendTagList(productTags, product); - await appendFollowers(loggedUser, product); - await appendFavorites(loggedUser, product); - - res.status(200).json({ product }); -}); - -module.exports.deleteFavoriteProduct = asyncHandler(async (req, res, next) => { - const { loggedUser } = req; - const { slug } = req.params; - - const product = await Product.findOne({ - where: { slug: slug }, - include: includeOptions, - }); - - if (!product) { - return next(new ErrorResponse("Product not found", 404)); - } - - await loggedUser.removeFavorite(product); - - const productTags = await product.getTagLists(); - appendTagList(productTags, product); - await appendFollowers(loggedUser, product); - await appendFavorites(loggedUser, product); - - res.status(200).json({ product }); -}); - -const fieldValidation = (field, next) => { - if (!field) { - return next(new ErrorResponse(`Missing fields`, 400)); - } -}; diff --git a/src/controllers/products.controller.js b/src/controllers/products.controller.js index 7d0e2fc..6aea2ff 100644 --- a/src/controllers/products.controller.js +++ b/src/controllers/products.controller.js @@ -5,6 +5,7 @@ const Product = require("../database/models/mysql/Product"); const Tag = require("../database/models/mysql/Tag"); const User = require("../database/models/mysql/User"); const ErrorResponse = require("../utils/errorResponse"); +const slug = require("slug"); const { appendFollowers, @@ -114,3 +115,170 @@ module.exports.productsFeed = asyncHandler(async (req, res) => { res.json({ products: products.rows, productsCount: products.count }); }); + +module.exports.getProduct = asyncHandler(async (req, res, next) => { + const { slug: productSlug } = req.params; + + let product = await ProductService.getProductBySlug(productSlug); + + if (!product) { + return next(new ErrorResponse("Product not found", 404)); + } + let tags = product.productTags; + product = product.toJSONFor(); + product.tags = tags; + + res.status(200).json({ product: product }); +}); + +module.exports.createProduct = asyncHandler(async (req, res, next) => { + const { loggedUser } = req; + + fieldValidation(req.body.product.title, next); + fieldValidation(req.body.product.description, next); + fieldValidation(req.body.product.body, next); + + const { title, description, body, tagList } = req.body.product; + const slugInDB = await Product.findOne({ where: { slug: slug(`${title}`) } }); + if (slugInDB) { + next(new ErrorResponse("Title already exists", 400)); + } + + const product = await Product.create({ + title: title, + description: description, + body: body, + }); + + for (const tag of tagList) { + const tagInDB = await Tag.findByPk(tag.trim()); + + if (tagInDB) { + await product.addProductTag(tagInDB); + } else if (tag.length > 2) { + const newTag = await Tag.create({ name: tag.trim() }); + await product.addProductTag(newTag); + } + } + delete loggedUser.dataValues.token; + + product.dataValues.tagList = tagList; + product.setAuthor(loggedUser); + product.dataValues.author = loggedUser; + await appendFollowers(loggedUser, loggedUser); + await appendFavorites(loggedUser, product); + + res.status(201).json({ product }); +}); + +module.exports.deleteProduct = asyncHandler(async (req, res, next) => { + const { slug: productSlug } = req.params; + const { loggedUser } = req; + + const product = await Product.findOne({ + where: { slug: productSlug }, + include: includeOptions, + }); + + if (!product) { + next(new ErrorResponse("Product not found", 404)); + } + + if (product.authorId !== loggedUser.id) { + return next(new ErrorResponse("Unauthorized", 401, "Authentication required.")); + } + + await product.destroy(); + + res.status(200).json({ product }); +}); + +module.exports.updateProduct = asyncHandler(async (req, res, next) => { + const { slug: productSlug } = req.params; + const { loggedUser } = req; + + const product = await Product.findOne({ + where: { slug: productSlug }, + include: includeOptions, + }); + + if (!product) { + next(new ErrorResponse("Product not found", 404)); + } + if (product.authorId !== loggedUser.id) { + return next(new ErrorResponse("Unauthorized", 401, "Authentication required.")); + } + + const { title, description, body } = req.body.product; + const slugInDB = await Product.findOne({ + where: { slug: slug(title ? title : product.title) }, + }); + if (slugInDB && slugInDB.slug !== productSlug) { + return next(new ErrorResponse("Title already exists", 400)); + } + + await product.update({ + title: title ? title : product.title, + description: description ? description : product.description, + body: body ? body : product.body, + }); + + const productTags = await product.getTagLists(); + appendTagList(productTags, product); + await appendFollowers(loggedUser, product); + await appendFavorites(loggedUser, product); + + res.status(200).json({ product }); +}); + +module.exports.addFavoriteProduct = asyncHandler(async (req, res, next) => { + const { loggedUser } = req; + const { slug: productSlug } = req.params; + + const product = await Product.findOne({ + where: { slug: productSlug }, + include: includeOptions, + }); + + if (!product) { + return next(new ErrorResponse("Product not found", 404)); + } + + await loggedUser.addFavorite(product); + + const productTags = await product.getTagLists(); + appendTagList(productTags, product); + await appendFollowers(loggedUser, product); + await appendFavorites(loggedUser, product); + + res.status(200).json({ product }); +}); + +module.exports.deleteFavoriteProduct = asyncHandler(async (req, res, next) => { + const { loggedUser } = req; + const { slug: productSlug } = req.params; + + const product = await Product.findOne({ + where: { slug: productSlug }, + include: includeOptions, + }); + + if (!product) { + return next(new ErrorResponse("Product not found", 404)); + } + + await loggedUser.removeFavorite(product); + + const productTags = await product.getTagLists(); + appendTagList(productTags, product); + await appendFollowers(loggedUser, product); + await appendFavorites(loggedUser, product); + + res.status(200).json({ product }); +}); + +const fieldValidation = (field, next) => { + if (!field) { + return next(new ErrorResponse(`Missing fields`, 400)); + } +}; diff --git a/src/controllers/user.controller.js b/src/controllers/users.controller.js similarity index 100% rename from src/controllers/user.controller.js rename to src/controllers/users.controller.js diff --git a/src/routes/api/cms/index.js b/src/routes/api/cms/index.js index 849c527..b998f61 100644 --- a/src/routes/api/cms/index.js +++ b/src/routes/api/cms/index.js @@ -1,6 +1,6 @@ var routes = require('express').Router(); -routes.use('/user', require('./user.routes')); +routes.use('/user', require('./users.routes')); routes.use('/articles', require('./articles.routes')); routes.use('/comments', require('./comments.routes')); routes.use('/profiles', require('./profiles.routes')); diff --git a/src/routes/api/cms/user.routes.js b/src/routes/api/cms/users.routes.js similarity index 94% rename from src/routes/api/cms/user.routes.js rename to src/routes/api/cms/users.routes.js index 2e3743f..531cfa9 100644 --- a/src/routes/api/cms/user.routes.js +++ b/src/routes/api/cms/users.routes.js @@ -2,7 +2,7 @@ const express = require('express'); const { authenticate, createUser, getUser, loginAsUser, updateUser, updateUserEmail, updateUserPassword, updateUserRole, updateUserUsername -} = require('../../../controllers/user.controller'); +} = require('../../../controllers/users.controller'); const auth = require('../../../middlewares/auth'); const userRouter = express.Router(); diff --git a/src/routes/api/ecommerce/product.routes.js b/src/routes/api/ecommerce/product.routes.js index 997aed2..9e51bb1 100644 --- a/src/routes/api/ecommerce/product.routes.js +++ b/src/routes/api/ecommerce/product.routes.js @@ -7,7 +7,7 @@ const { deleteFavoriteProduct, deleteProduct, updateProduct, -} = require('../../../controllers/product.controller'); +} = require('../../../controllers/products.controller'); const auth = require('../../../middlewares/auth'); const productRouter = express.Router(); diff --git a/src/routes/api/herowars/herowars.routes.js b/src/routes/api/herowars/herowars.routes.js deleted file mode 100644 index e027e70..0000000 --- a/src/routes/api/herowars/herowars.routes.js +++ /dev/null @@ -1,13 +0,0 @@ -const express = require('express'); -const { createMember, getAllMembers, getMember, updateMember } = require('../../../controllers/herowars.controller'); -const auth = require('../../../middlewares/auth'); - -const herowarsRouter = express.Router(); - -herowarsRouter.param('member', getMember); -herowarsRouter.get('/:member', auth.required, getMember); -herowarsRouter.get('/', auth.required, getAllMembers); -herowarsRouter.post('/', auth.required, createMember); -herowarsRouter.put('/', auth.required, updateMember); - -module.exports = herowarsRouter; diff --git a/src/routes/api/herowars/index.js b/src/routes/api/herowars/index.js index 48eb572..ffc7dee 100644 --- a/src/routes/api/herowars/index.js +++ b/src/routes/api/herowars/index.js @@ -2,7 +2,6 @@ var routes = require('express').Router(); routes.use('/clans', require('./clans.routes')); routes.use('/members', require('./members.routes')); -routes.use('/herowars', require('./herowars.routes')); routes.use(function (err, req, res, next) { if (err.name === 'ValidationError') {