backend: GET, POST, DELETE /favorites/:functionId

This commit is contained in:
Divlo 2020-04-10 00:01:39 +02:00
parent 9152313110
commit 903590fb08
6 changed files with 93 additions and 12 deletions

View File

@ -54,9 +54,9 @@ Favorites.belongsTo(Functions, { constraints: false });
// Users can post comments on functions // Users can post comments on functions
Users.hasMany(Comments); Users.hasMany(Comments);
Comments.belongsTo(Users); Comments.belongsTo(Users, { constraints: false });
Functions.hasMany(Comments); Functions.hasMany(Comments);
Comments.belongsTo(Functions); Comments.belongsTo(Functions, { constraints: false });
/* Server */ /* Server */
// sequelize.sync({ force: true }) // sequelize.sync({ force: true })

View File

@ -43,7 +43,7 @@ exports.deleteFunction = async (req, res, next) => {
const filePath = path.join(__dirname, '..', 'assets', result.image); const filePath = path.join(__dirname, '..', 'assets', result.image);
fs.unlinkSync(filePath); // supprime le fichier fs.unlinkSync(filePath); // supprime le fichier
} }
await Functions.destroy({ where: { id } }); await result.destroy();
res.status(200).json({ message: "La fonction a été correctement supprimé!"}); res.status(200).json({ message: "La fonction a été correctement supprimé!"});
} catch (error) { } catch (error) {
console.log(error); console.log(error);

View File

@ -1,5 +1,6 @@
const errorHandling = require('../assets/utils/errorHandling'); const errorHandling = require('../assets/utils/errorHandling');
const Categories = require('../models/categories'); const Categories = require('../models/categories');
const { serverError } = require('../assets/config/errors');
exports.getCategories = (_req, res, next) => { exports.getCategories = (_req, res, next) => {
Categories.findAll() Categories.findAll()

View File

@ -3,6 +3,7 @@ const Comments = require('../models/comments');
const Users = require('../models/users'); const Users = require('../models/users');
const Functions = require('../models/functions'); const Functions = require('../models/functions');
const helperQueryNumber = require('../assets/utils/helperQueryNumber'); const helperQueryNumber = require('../assets/utils/helperQueryNumber');
const { serverError } = require('../assets/config/errors');
exports.getCommentsByFunctionId = (req, res, next) => { exports.getCommentsByFunctionId = (req, res, next) => {
const { functionId } = req.params; const { functionId } = req.params;

View File

@ -1,2 +1,74 @@
const errorHandling = require('../assets/utils/errorHandling'); const errorHandling = require('../assets/utils/errorHandling');
const Favorites = require('../models/categories'); const { serverError } = require('../assets/config/errors');
const Favorites = require('../models/favorites');
const Functions = require('../models/functions');
exports.getFavoriteByFunctionId = async (req, res, next) => {
const { functionId } = req.params;
const { userId } = req;
try {
const favorite = await Favorites.findOne({
where: {
userId,
functionId
}
});
if (!favorite) {
return res.status(200).json({ isFavorite: false });
}
return res.status(200).json({ isFavorite: true });
} catch (error) {
console.log(error);
return errorHandling(next, serverError);
}
}
exports.postFavoriteByFunctionId = async (req, res, next) => {
const { functionId } = req.params;
const { userId } = req;
try {
const resultFunction = await Functions.findOne({ where: { id: functionId } });
if (!resultFunction) {
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
}
const favorite = await Favorites.findOne({
where: {
userId,
functionId
}
});
if (!favorite) {
await Favorites.create({ userId, functionId });
return res.status(201).json({ result: "Le favoris a bien été ajouté!" });
}
return errorHandling(next, { message: "La fonction est déjà en favoris.", statusCode: 400 });
} catch (error) {
console.log(error);
return errorHandling(next, serverError);
}
}
exports.deleteFavoriteByFunctionId = async (req, res, next) => {
const { functionId } = req.params;
const { userId } = req;
try {
const resultFunction = await Functions.findOne({ where: { id: functionId } });
if (!resultFunction) {
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
}
const favorite = await Favorites.findOne({
where: {
userId,
functionId
}
});
if (!favorite) {
return errorHandling(next, { message: "Le fonction n'est pas en favoris.", statusCode: 400 });
}
await favorite.destroy();
return res.status(200).json({ message: "Le fonction a bien été supprimé des favoris." });
} catch (error) {
console.log(error);
return errorHandling(next, serverError);
}
}

View File

@ -1,11 +1,18 @@
const { Router } = require('express'); const { Router } = require('express');
const favoritesController = require('../controllers/categories'); const favoritesController = require('../controllers/favorites');
const isAuth = require('../middlewares/isAuth');
const FavoritesRouter = Router(); const FavoritesRouter = Router();
// FavoritesRouter.route('/') FavoritesRouter.route('/:functionId')
// // Récupère les catégories // Récupère si une fonction est en favoris (d'un utilisateur)
// .get(favoritesController.getCategories); .get(isAuth, favoritesController.getFavoriteByFunctionId)
// Permet à un utilisateur d'ajouter une fonction aux favoris
.post(isAuth, favoritesController.postFavoriteByFunctionId)
// Supprime une fonction des favoris d'un utilisateur
.delete(isAuth, favoritesController.deleteFavoriteByFunctionId);
module.exports = FavoritesRouter; module.exports = FavoritesRouter;