Skip to content

Commit

Permalink
Fixed favoritestatus
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse-Chong committed Nov 13, 2023
1 parent 9111f87 commit dc93158
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 3 additions & 0 deletions back-end/controllers/playlistController.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ playlistController.post("/", async (req, res) => {
playlistController.delete("/:id", async (req, res) => {
try {
const { id } = req.params;
console.log("Deleting playlist with ID:", id);
const deletedPlaylist = await deletePlaylist(id);
if (deletedPlaylist.id) {
res.status(200).json(deletedPlaylist);
} else {
console.log("Playlist not found:", id);
res.status(404).json({ error: "Playlist not found" });
}
} catch (error) {
console.error("Error deleting playlist:", error);
res.status(500).json({ error: "Server error" });
}
});
Expand Down
23 changes: 22 additions & 1 deletion back-end/controllers/songController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const {
createSong,
updateSong,
deleteSong,
updateFavoriteStatus
} = require("../queries/song");
const {
checkName,
checkArtist,
checkBoolean,
checkAlbum,
checkTime,
checkTime
} = require("../validations/checkSongs.js");

const playlistController = require("./playlistController.js");
Expand All @@ -21,6 +22,7 @@ songs.use("/:song_id/playlists", playlistController);
// INDEX
songs.get("/", async (req, res) => {
const allSongs = await getAllSongs();
console.log("allSongs:", allSongs)
if (allSongs[0]) {
res.status(200).json(allSongs);
} else {
Expand Down Expand Up @@ -79,4 +81,23 @@ songs.put(
}
);

songs.put("/:id/favorite", async (req, res) => {
const { id } = req.params;
try {
const song = await getSong(Number(id));
if (!song) {
return res.status(404).json("Song not found");
}
const updatedSong = await updateFavoriteStatus(id, !song.is_favorite);
if (updatedSong.id) {
res.status(200).json(updatedSong);
} else {
res.status(500).json("Server error: Could not update favorite status.");
}
} catch (error) {
console.error("Server error:", error);
res.status(500).json("Server error: Could not update favorite status.");
}
});

module.exports = songs;
3 changes: 2 additions & 1 deletion back-end/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ CREATE TABLE songs (
album TEXT NOT NULL,
time INT,
is_favorite BOOLEAN,
playlist_id INTEGER REFERENCES playlists(id) ON DELETE CASCADE
playlist_id INTEGER REFERENCES playlists(id)
ON DELETE CASCADE
);
4 changes: 3 additions & 1 deletion back-end/queries/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ const deletePlaylist = async (id) => {
try {
const deletedPlaylist = await db.one(
"DELETE FROM playlists WHERE id = $1 RETURNING *",
id
[id]
);
console.log("Deleted playlist:", deletedPlaylist); // Add this log
return deletedPlaylist;
} catch (error) {
console.error("Error deleting playlist:", error);
return error;
}
};
Expand Down

0 comments on commit dc93158

Please sign in to comment.