Skip to content

Commit

Permalink
add delete movie in playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
wassb92 committed Jul 30, 2023
1 parent 3cffa11 commit ae2d101
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
44 changes: 42 additions & 2 deletions client/src/components/Card.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
{movies &&
Expand All @@ -12,6 +40,14 @@ const MovieCard = ({ movies }) => {
key={movie.movieId}
className="border rounded-lg overflow-hidden shadow-md"
>
{canDelete && (
<div
className="bg-red-600 text-white text-center p-1 cursor-pointer"
onClick={(e) => handleDelete(e, movie.movieId, playlistName)}
>
Supprimer
</div>
)}
<img
src={API_IMG + movie.poster_path}
alt={movie.title}
Expand All @@ -38,7 +74,11 @@ const PlaylistCard = ({ playlists }) => {
<h2 className="text-xl font-semibold p-4">{playlist.name}</h2>
{playlist.movies && playlist.movies.length > 0 ? (
<div key={playlist.name} className="p-4">
<MovieCard movies={playlist.movies} />
<MovieCard
movies={playlist.movies}
canDelete
playlistName={playlist.name}
/>
</div>
) : (
<div className="col-span-full text-center text-gray-600">
Expand Down
4 changes: 2 additions & 2 deletions client/src/pages/private/Account.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const CreatePlaylist = () => {
config
);
setName("");
console.log(data);
window.location.reload();
} catch (error) {
console.log(error);
}
Expand All @@ -57,7 +57,7 @@ const CreatePlaylist = () => {
config
);
setName("");
console.log(data);
window.location.reload();
} catch (error) {
console.log(error);
}
Expand Down
58 changes: 58 additions & 0 deletions server/controllers/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
}
};
4 changes: 4 additions & 0 deletions server/routes/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
playlists,
deletePlaylist,
addMovieToPlaylist,
deleteMovieFromPlaylist,
} = require("../controllers/private");
const { protect } = require("../middleware/auth");

Expand All @@ -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;

0 comments on commit ae2d101

Please sign in to comment.