Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab pt 1 #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ dist

# Misc
.DS_Store

#
.env
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"marquee.widgets.npm-stats.packageNames": ["tuner-mono-repo"]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Tuner Full Stack Application

Frontend repo : https://github.com/Shaik-Kamil/tuner-frontend
Let's make our own music playlist app!

![](https://media4.giphy.com/media/4T7zBzdeNEtjThYDWn/giphy.gif?cid=790b76114ee03ef7f860492a9083d77f86191a7bf340002c&rid=giphy.gif&ct=g)
Expand Down
33 changes: 33 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Dependencies
const cors = require('cors');
const express = require('express');
const songsController = require('./controllers/songController');
const playlistController = require('./controllers/playlistController');

//! Congfig
const app = express();

//! Middleware

app.use(cors());
app.use(express.json());

//! Songs Routes
app.use('/songs', songsController);

//! playlist route
app.use('/playlists', playlistController);

//! Routes

app.get('/', (req, res) => {
res.send('Welcome to the Songs App');
});

//! 404
app.get('*', (req, res) => {
res.status(404).send('Page not found');
});

//! Export
module.exports = app;
69 changes: 69 additions & 0 deletions controllers/playlistController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const express = require('express');
const playlists = express.Router();
const {
getAllPlaylists,
getPlaylist,
makePlaylist,
deletePlaylist,
updatePlaylist,
} = require('../queries/playlist');
const songController = require('./songController');

//! middleware
playlists.use('/:playlistId/songs', songController);

//! Index page

playlists.get('/', async (req, res) => {
try {
const allPlaylists = await getAllPlaylists();
res.status(200).json(allPlaylists);
} catch (error) {
res.status(500).json({ error: error });
}
});

//! Show

playlists.get('/:id', async (req, res) => {
const { id } = req.params;
const onePlaylist = await getPlaylist(id);
if (!onePlaylist.message) res.status(200).json(onePlaylist);
else res.status(404).json({ error: 'Not Found' });
});

//! Create
playlists.post('/', async (req, res) => {
try {
const playlist = await makePlaylist(req.body);
res.status(200).json(playlist);
} catch (error) {
res.status(500).json({ error: error });
}
});

//! Delete

playlists.delete('/:id', async (req, res) => {
try {
const { id } = req.params;
const deletedPlaylist = await deletePlaylist(id);
res.status(200).json(deletedPlaylist);
} catch (error) {
res.status(404).json({ error: 'Id not found' });
}
});

//! update playlist

playlists.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const updatedPlaylist = await updatePlaylist(id, req.body);
res.status(200).json(updatedPlaylist);
} catch (error) {
res.status(404).json({ error: 'playlist not found!' });
}
});

module.exports = playlists;
68 changes: 68 additions & 0 deletions controllers/songController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const express = require('express');
const songs = express.Router({ mergeParams: true });
const {
getAllSongs,
getSong,
createSong,
deleteSong,
updateSong,
} = require('../queries/songs');
const {
checkName,
checkArtist,
checkBoolean,
} = require('../validations/checkSongs');

// const playlists = require('./playlistController');

//! index
songs.get('/', async (req, res) => {
const { playlistId } = req.params;
try {
const allSongs = await getAllSongs(playlistId);
res.status(200).json(allSongs);
} catch (error) {
res.status(500).json({ error: 'Not found ' });
}
});

//! Show

songs.get('/:id', async (req, res) => {
const { id } = req.params;
try {
const song = await getSong(id);
res.status(200).json(song);
} catch (error) {
res.status(500).json({ error: 'Not found' });
}
});

//! Create

songs.post('/', checkName, checkArtist, checkBoolean, async (req, res) => {
try {
const song = await createSong(req.body);
res.status(200).json(song);
} catch (error) {
res.status(400).json({ error: error });
}
});

//! Delete

songs.delete('/:id', async (req, res) => {
const { id } = req.params;
const deletedSong = await deleteSong(id);
if (deletedSong.id) res.status(200).json(deletedSong);
else res.status(404).json('Song not found');
});

//! Update
songs.put('/:id', checkName, checkBoolean, checkArtist, async (req, res) => {
const { id } = req.params;
const updatedSong = await updateSong(id, req.body);
res.status(200).json(updatedSong);
});

module.exports = songs;
13 changes: 13 additions & 0 deletions db/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const pgp = require('pg-promise')();
require('dotenv').config();

const cn = {
host: process.env.PG_HOST,
port: process.env.PG_PORT,
database: process.env.PG_DATABASE,
user: process.env.PG_USER,
};

const db = pgp(cn);

module.exports = db;
23 changes: 23 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DROP DATABASE IF EXISTS tuner;
CREATE DATABASE tuner;

\c tuner;

DROP TABLE IF EXISTS playlist;
CREATE TABLE playlist (
id SERIAL PRIMARY KEY,
title TEXT
);

CREATE TABLE songs(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
artist TEXT,
album TEXT,
time TEXT,
is_favorite BOOLEAN,
playlist_id INTEGER REFERENCES playlist (id) ON DELETE CASCADE
);



13 changes: 13 additions & 0 deletions db/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\c tuner;
INSERT INTO playlist(title)
VALUES
('Travel Playlist'),
('Tamil Playlist'),
('English Playlist');

INSERT INTO songs (playlist_id,name, artist, album, time, is_favorite) VALUES
('3','Hit em up', 'Tupac', 'Death Row Greatest Hit','3:00', true),
('3','Ayo', '50 Cent', 'Miscellaneous','4:15', true);



Loading