From 903590fb081929560c89caeb330a289acdb81876 Mon Sep 17 00:00:00 2001 From: Divlo Date: Fri, 10 Apr 2020 00:01:39 +0200 Subject: [PATCH] backend: GET, POST, DELETE /favorites/:functionId --- api/app.js | 4 +- api/controllers/admin.js | 2 +- api/controllers/categories.js | 5 ++- api/controllers/comments.js | 1 + api/controllers/favorites.js | 76 ++++++++++++++++++++++++++++++++++- api/routes/favorites.js | 17 +++++--- 6 files changed, 93 insertions(+), 12 deletions(-) diff --git a/api/app.js b/api/app.js index c320e8e..ee5b6c0 100644 --- a/api/app.js +++ b/api/app.js @@ -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 }) diff --git a/api/controllers/admin.js b/api/controllers/admin.js index 38eeb7e..b369b16 100644 --- a/api/controllers/admin.js +++ b/api/controllers/admin.js @@ -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); diff --git a/api/controllers/categories.js b/api/controllers/categories.js index dec7714..ba24954 100644 --- a/api/controllers/categories.js +++ b/api/controllers/categories.js @@ -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() diff --git a/api/controllers/comments.js b/api/controllers/comments.js index 8dfc9a5..af3a14a 100644 --- a/api/controllers/comments.js +++ b/api/controllers/comments.js @@ -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; diff --git a/api/controllers/favorites.js b/api/controllers/favorites.js index 0881997..3e29619 100644 --- a/api/controllers/favorites.js +++ b/api/controllers/favorites.js @@ -1,2 +1,74 @@ -const errorHandling = require('../assets/utils/errorHandling'); -const Favorites = require('../models/categories'); \ No newline at end of file +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); + } +} \ No newline at end of file diff --git a/api/routes/favorites.js b/api/routes/favorites.js index 3de7110..af2799f 100644 --- a/api/routes/favorites.js +++ b/api/routes/favorites.js @@ -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; \ No newline at end of file