refactor(naming): harmonize controller and route file names
- 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
This commit is contained in:
@@ -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));
|
||||
}
|
||||
});
|
||||
@@ -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));
|
||||
}
|
||||
};
|
||||
@@ -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));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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'));
|
||||
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user