diff --git a/client/src/components/Card.jsx b/client/src/components/Card.jsx
index 49686c3..18ec056 100644
--- a/client/src/components/Card.jsx
+++ b/client/src/components/Card.jsx
@@ -1,8 +1,36 @@
import React from "react";
+import axios from "axios";
const API_IMG = "https://image.tmdb.org/t/p/w500/";
-const MovieCard = ({ movies }) => {
+const MovieCard = ({ movies, canDelete = false, playlistName = "" }) => {
+ const handleDelete = async (e, movieId, playlistName) => {
+ e.preventDefault();
+
+ const config = {
+ headers: {
+ "Content-Type": "application/json",
+ authorization: `Bearer ${localStorage.getItem("authToken")}`,
+ },
+ };
+
+ const body = {
+ playlistName,
+ movieId,
+ };
+
+ try {
+ const { data } = await axios.put(
+ `${global.API_ENDPOINT}/api/private/playlists/delete-movie`,
+ body,
+ config
+ );
+ window.location.reload();
+ } catch (error) {
+ console.log(error);
+ }
+ };
+
return (
{movies &&
@@ -12,6 +40,14 @@ const MovieCard = ({ movies }) => {
key={movie.movieId}
className="border rounded-lg overflow-hidden shadow-md"
>
+ {canDelete && (
+
handleDelete(e, movie.movieId, playlistName)}
+ >
+ Supprimer
+
+ )}
{
{playlist.name}
{playlist.movies && playlist.movies.length > 0 ? (
-
+
) : (
diff --git a/client/src/pages/private/Account.jsx b/client/src/pages/private/Account.jsx
index 0075100..8d8d086 100644
--- a/client/src/pages/private/Account.jsx
+++ b/client/src/pages/private/Account.jsx
@@ -43,7 +43,7 @@ const CreatePlaylist = () => {
config
);
setName("");
- console.log(data);
+ window.location.reload();
} catch (error) {
console.log(error);
}
@@ -57,7 +57,7 @@ const CreatePlaylist = () => {
config
);
setName("");
- console.log(data);
+ window.location.reload();
} catch (error) {
console.log(error);
}
diff --git a/server/controllers/private.js b/server/controllers/private.js
index ba33884..672fb6a 100644
--- a/server/controllers/private.js
+++ b/server/controllers/private.js
@@ -259,3 +259,61 @@ exports.addMovieToPlaylist = async (req, res, next) => {
});
}
};
+
+exports.deleteMovieFromPlaylist = async (req, res, next) => {
+ const { playlistName, movieId } = req.body;
+
+ const authToken = getAuthToken(req);
+
+ try {
+ if (!authToken) {
+ return res.status(401).json({
+ message:
+ "Accès non autorisé. Veuillez fournir un jeton d'authentification.",
+ });
+ }
+
+ const decoded = jwt.verify(authToken, process.env.JWT_SECRET);
+ const foundUser = await User.findById(decoded.userId);
+
+ if (!foundUser) {
+ return res.status(404).json({ message: "Utilisateur non trouvé" });
+ }
+
+ const existingPlaylistIndex = foundUser.playlists.findIndex(
+ (playlist) => playlist.name === playlistName
+ );
+
+ if (existingPlaylistIndex === -1) {
+ return res.status(400).json({
+ message: "Une playlist avec ce nom n'existe pas",
+ });
+ }
+
+ const existingMovieIndex = foundUser.playlists[
+ existingPlaylistIndex
+ ].movies.findIndex((movie) => movie.movieId === movieId);
+
+ if (existingMovieIndex === -1) {
+ return res.status(400).json({
+ message: "Ce film n'est pas dans la playlist",
+ });
+ }
+
+ foundUser.playlists[existingPlaylistIndex].movies.splice(
+ existingMovieIndex,
+ 1
+ );
+ await foundUser.save();
+
+ return res.status(200).json({
+ message: "ID de film supprimé avec succès de la playlist",
+ });
+ } catch (err) {
+ console.error(err);
+ return res.status(500).json({
+ message:
+ "Une erreur est survenue lors de la suppression du film de la playlist",
+ });
+ }
+};
diff --git a/server/routes/private.js b/server/routes/private.js
index d14dbf4..e71ca6a 100644
--- a/server/routes/private.js
+++ b/server/routes/private.js
@@ -7,6 +7,7 @@ const {
playlists,
deletePlaylist,
addMovieToPlaylist,
+ deleteMovieFromPlaylist,
} = require("../controllers/private");
const { protect } = require("../middleware/auth");
@@ -16,5 +17,8 @@ router.route("/private/alreadySeen").put(protect, addAlreadySeen);
router.route("/private/playlists").put(protect, playlists);
router.route("/private/playlists/:name").delete(protect, deletePlaylist);
router.route("/private/playlists/add-movie").put(protect, addMovieToPlaylist);
+router
+ .route("/private/playlists/delete-movie")
+ .put(protect, deleteMovieFromPlaylist);
module.exports = router;