backend: GET, POST, DELETE /favorites/:functionId
This commit is contained in:
		| @@ -54,9 +54,9 @@ Favorites.belongsTo(Functions, { constraints: false }); | ||||
|  | ||||
| // Users can post comments on functions | ||||
| Users.hasMany(Comments); | ||||
| Comments.belongsTo(Users); | ||||
| Comments.belongsTo(Users, { constraints: false }); | ||||
| Functions.hasMany(Comments); | ||||
| Comments.belongsTo(Functions); | ||||
| Comments.belongsTo(Functions, { constraints: false }); | ||||
|  | ||||
| /* Server */ | ||||
| // sequelize.sync({ force: true }) | ||||
|   | ||||
| @@ -43,7 +43,7 @@ exports.deleteFunction = async (req, res, next) => { | ||||
|             const filePath = path.join(__dirname, '..', 'assets', result.image); | ||||
|             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é!"}); | ||||
|     } catch (error) { | ||||
|         console.log(error); | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| const errorHandling = require('../assets/utils/errorHandling'); | ||||
| const Categories    = require('../models/categories'); | ||||
| const errorHandling   = require('../assets/utils/errorHandling'); | ||||
| const Categories      = require('../models/categories'); | ||||
| const { serverError } = require('../assets/config/errors'); | ||||
|  | ||||
| exports.getCategories = (_req, res, next) => { | ||||
|     Categories.findAll() | ||||
|   | ||||
| @@ -3,6 +3,7 @@ const Comments          = require('../models/comments'); | ||||
| const Users             = require('../models/users'); | ||||
| const Functions         = require('../models/functions'); | ||||
| const helperQueryNumber = require('../assets/utils/helperQueryNumber'); | ||||
| const { serverError }   = require('../assets/config/errors'); | ||||
|  | ||||
| exports.getCommentsByFunctionId = (req, res, next) => { | ||||
|     const { functionId } = req.params; | ||||
|   | ||||
| @@ -1,2 +1,74 @@ | ||||
| const errorHandling = require('../assets/utils/errorHandling'); | ||||
| const Favorites     = require('../models/categories'); | ||||
| const errorHandling   = require('../assets/utils/errorHandling'); | ||||
| 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); | ||||
|     } | ||||
| } | ||||
| @@ -1,11 +1,18 @@ | ||||
| const { Router }           = require('express'); | ||||
| const favoritesController = require('../controllers/categories'); | ||||
| const { Router }          = require('express'); | ||||
| const favoritesController = require('../controllers/favorites'); | ||||
| const isAuth              = require('../middlewares/isAuth'); | ||||
|  | ||||
| const FavoritesRouter = Router(); | ||||
|  | ||||
| // FavoritesRouter.route('/') | ||||
| FavoritesRouter.route('/:functionId') | ||||
|  | ||||
| //     // Récupère les catégories | ||||
| //     .get(favoritesController.getCategories); | ||||
|     // Récupère si une fonction est en favoris (d'un utilisateur) | ||||
|     .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; | ||||
		Reference in New Issue
	
	Block a user