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 finished with full crud #10

Closed
wants to merge 10 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
33 changes: 33 additions & 0 deletions back-end/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const cors = require("cors");
const express = require("express");
const morgan = require("morgan");
const songController = require('./controllers/songController')
//Configuration
const app = express();

//middleware for parsing JSON request bodies

app.use(express.json());
app.use(cors());
app.use(morgan("tiny"));
app.use('/songs', songController);

//show route to get an individual song by ID

// ROUTES
app.get("/", (req, res) => {
res.send("Welcome to My Song App");
});




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

// EXPORT


module.exports = app;
74 changes: 74 additions & 0 deletions back-end/controllers/reviewController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const express = require("express");
// child model gets mergerParams
const reviews = express.Router({mergeParams:true});

const {getSong} = require("../queries/song.js");

//queries
const {
getAllReviews,
getReview,
newReview,
deleteReview,
updateReview,
} = require("../queries/reviews.js");


//Index
reviews.get("/", async (req, res) => {
const { songs_id } = req.params;
const allReviews = await getAllReviews(songs_id);

const songs = await getSong(songs_id)

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

//Show
reviews.get("/:id", async (req, res) => {
const {songs_id, id} = req.params;
const review = await getReview(id);
const songs = await getSong(songs_id);

if (review) {
res.json({...songs, review});
} else {
res.status(404).json({error: "not found"});
}
});

//Update
reviews.put("/:id", async (req,res) => {
const{id, songs_id} = req.params;
const updatedReview = await updateReview({songs_id, id, ...req.body});
if (updatedReview.id){
res.status(200).json(updatedReview)
} else {
res.status(404).json("Review not found");
}
});

reviews.post("/", async (req, res) => {
const { songs_id } = req.params;
const review = await newReview({songs_id,...req.body});
res.status(200).json(review);
});


//Delete

reviews.delete("/:id", async (req, res) => {
const { id } = req.params;
const deletedReview = await deleteReview(id);
if (deletedReview.id) {
res.status(200).json(deletedReview);
} else {
res.status(404).json({ error: "Review not found" });
}
});

module.exports = reviews;
57 changes: 57 additions & 0 deletions back-end/controllers/songController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const express = require("express");
const songs = express.Router();
const {getAllSongs, getSong, createSong, deleteSong, updateSong} = require("../queries/song");
const {checkName, checkBoolean, checkArtist, checkTime ,checkAlbum,} = require('../validations/checkSongs')
const reviewController = require('./reviewController.js')
songs.use('./:songs_id/reviews', reviewController);


//get all songs
//localhost:3345/songs/
songs.get('/', async (req, res) => {
const allSongs = await getAllSongs()
if (allSongs[0]) {
res.status(200).json(allSongs);
} else {
res.status(500).json({ error: "server error" });
}
});
songs.get('/:id', async (req, res) => {
//const id = req.params.id
const {id} = req.params;
const oneSong = await getSong(id);
if (oneSong) {
res.status(200).json(oneSong);
} else {
res.status(404).json({ error: 'Not Found' });
}
})

//POST a new song
//localhost:4005/songs/
songs.post('/',checkName,checkBoolean,checkArtist, checkTime ,checkAlbum, async (req, res) => {
const body = req.body
const song = await createSong(body);
res.status(200).json(song)
})
songs.delete('/:id', async (req, res) => {
const {id} = req.params;
const deletedSong = await deleteSong(id);
if (deletedSong) {
res.status(200).json(deletedSong)
} else {
res.status(404).json({ error: 'Song Not Found' });
}
});
songs.put('/:id', checkName, checkBoolean,checkArtist, checkTime ,checkAlbum, async (req, res) => {
const {id} = req.params;
const body = req.body
const updatedSong = await updateSong(id, body);
if (updatedSong.id) {
res.status(200).json(updatedSong)
} else {
res.status(404).json({ error: 'Song Not Found' });
}
});

module.exports = songs;
27 changes: 27 additions & 0 deletions back-end/db/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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,
};

//connect to colors_dev_db
const db = pgp(cn);

db.connect()
.then((cn) => {
const { user, host, port, database } = cn.client;
console.log(
"\x1b[90m" +
`Postgres connection established with user:${user}, host:${host}, port:${port}, database:${database}` +
"\x1b[0m"
);
cn.done();
})
.catch((error) => console.log("database connection error", error));

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

\c songs_dev;



CREATE TABLE songs (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
artist TEXT NOT NULL,
album TEXT,
time TEXT,
is_favorite BOOLEAN
);

DROP TABLE IF EXISTS reviews;

CREATE TABLE reviews (
id SERIAL PRIMARY KEY,
reviewer TEXT,
title TEXT,
content TEXT,
rating NUMERIC,
CHECK (rating >= 0 AND rating <= 5),
songs_id INTEGER REFERENCES songs (id)
ON DELETE CASCADE
);
22 changes: 22 additions & 0 deletions back-end/db/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
\c songs_dev;


-- Insert sample songs into the "songs" table

INSERT INTO songs (name, artist, album, time, is_favorite)
VALUES
('Blinding Ligit', 'TheWeeknd','After Hours', '3:23', true),
('To Be Loved', 'Adele','30', '6:43', false),
('Save Your Tears', 'TheWeeknd','After Hours', '3:35', true);

INSERT INTO reviews (songs_id, reviewer, title,content,rating)
VALUES
('1', 'Amy', 'My Favorite', 'Best song for twenty twenty', 3),
('2', 'Louis', 'My Favorite', 'This is awesome', 3),
('3', 'John', 'My Least Favorite', 'Im stressed out', 5),
('2', 'Juliana', 'It is the best', 'I love Adele', 5),
('2', 'Sabrina', 'Cool Site', 'It keeps me activate in the shower', 1),
('2', 'Mr. Henneberry', 'Ehh', 'This song displease me', 3),
('2', 'Vincent', 'A lifesaver!','Helped me get my stove cleaner than I ever imagiend possible!', 4),
('3', 'Giuliana', 'Insert Confetti Emoji Here', 'I survived 6 hours at the DMV!', 4),
('3', 'Gabby', 'My Friend Giuliana', 'Gets a discount if I leave a positive review, so here it is',5);
Loading