Skip to content

Commit

Permalink
one to many is done
Browse files Browse the repository at this point in the history
  • Loading branch information
Jameel914 committed Nov 13, 2023
1 parent 7923a01 commit 932f1bd
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 324 deletions.
6 changes: 4 additions & 2 deletions back-end/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const express = require("express");
const app = express();
const cors = require("cors");

const songController = require("./controllers/songController.js");
//const songController = require("./controllers/songController.js");
const albumController = require("./controllers/albumController.js");

app.use(cors());
app.use(express.json());
app.use("/songs", songController);
//app.use("/songs", songController);
app.use("/albums", albumController);

app.get("/", (req, res) => {
res.status(200).send("Welcome to Tuner");
Expand Down
63 changes: 63 additions & 0 deletions back-end/controllers/albumController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const express = require("express");
const albums = express.Router();

const songController = require("./songController.js");
albums.use("/:albums_id/songs", songController);

const {
getAllAlbums,
getAlbum,
createAlbum,
deleteAlbum,
updateAlbum,
} = require("../queries/album.js");
const { checkName, checkBoolean } = require("../validations/checkAlbums.js");

albums.get("/", async (req, res) => {
const allAlbums = await getAllAlbums();
console.log(allAlbums);
if (allAlbums[0]) {
res.status(200).json(allAlbums);
} else {
res.status(500).json({ error: "server error" });
}
});

albums.get("/:id", async (req, res) => {
const { id } = req.params;
const oneAlbum = await getAlbum(id);
if (oneAlbum) {
res.status(200).json(oneAlbum);
} else {
res.status(404).json({ error: "Not Found" });
}
});

albums.post("/", checkName, checkBoolean, async (req, res) => {
const body = req.body;
const album = await createAlbum(body);
res.status(200).json(album);
});

albums.delete("/:id", async (req, res) => {
const { id } = req.params;
const deletedAlbum = await deleteAlbum(id);
if (deletedAlbum.id) {
res.status(200).json(deletedAlbum);
} else {
res.status(404).json({ error: "Song Not Found" });
}
});

albums.put("/:id", checkName, checkBoolean, async (req, res) => {
const { id } = req.params;
const body = req.body;
const updatedAlbum = await updateAlbum(id, body);
if (updatedAlbum.id) {
res.status(200).json(updatedAlbum);
} else {
res.status(404).json({ error: "Song Not Found" });
}
});

module.exports = albums;
39 changes: 20 additions & 19 deletions back-end/controllers/songController.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
const express = require("express");
const songs = express.Router();
const songs = express.Router({ mergeParams: true });

const { getAlbum } = require("../queries/album.js");

const {
getAllSongs,
getSong,
createSong,
deleteSong,
updateSong,
} = require("../queries/song.js");
const {
checkName,
checkArtist,
checkBoolean,
} = require("../validations/checkSongs.js");

songs.get("/", async (req, res) => {
const allSongs = await getAllSongs();
console.log(allSongs);
if (allSongs[0]) {
res.status(200).json(allSongs);
const { albums_id } = req.params;
const allSongs = await getAllSongs(albums_id);
const albumIndex = await getAlbum(albums_id);

if (albumIndex.id) {
res.status(200).json({ ...albumIndex, allSongs });
} else {
res.status(500).json({ error: "server error" });
}
});

songs.get("/:id", async (req, res) => {
const { id } = req.params;
const { albums_id, id } = req.params;
const oneSong = await getSong(id);
const albumIndex = await getAlbum(albums_id);
if (oneSong) {
res.status(200).json(oneSong);
res.status(200).json({ ...albumIndex, oneSong });
} else {
res.status(404).json({ error: "Not Found" });
}
});

songs.post("/", checkName, checkArtist, checkBoolean, async (req, res) => {
const body = req.body;
const song = await createSong(body);
songs.post("/", async (req, res) => {
const { albums_id } = req.params;
const song = await createSong({ albums_id, ...req.body });
res.status(200).json(song);
});

Expand All @@ -49,10 +50,10 @@ songs.delete("/:id", async (req, res) => {
}
});

songs.put("/:id", checkName, checkArtist, checkBoolean, async (req, res) => {
const { id } = req.params;
const body = req.body;
const updatedSong = await updateSong(id, body);
songs.put("/:id", async (req, res) => {
const { id, albums_id } = req.params;
//const body = req.body;
const updatedSong = await updateSong({ albums_id, id, ...req.body });
if (updatedSong.id) {
res.status(200).json(updatedSong);
} else {
Expand Down
Loading

0 comments on commit 932f1bd

Please sign in to comment.