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 #7

Open
wants to merge 3 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
27 changes: 27 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// DEPENDENCIES / IMPORTS
const cors = require("cors");
const express = require("express");
const songController = require("./controllers/songController")

// CONFIGURATION
const app = express();

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

// Songs ROUTES
app.use("/songs", songController);

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

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

// EXPORT
module.exports = app;
58 changes: 58 additions & 0 deletions controllers/songController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const express = require("express");
const songs = express.Router();
const { getAllSongs, getSong, createSong, deleteSong, updateSong } = require('../queries/songs.js')

// INDEX
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" });
}
});

// GET ONE SONG / SHOW
songs.get("/:id", async (req, res) => {
const { id } = req.params
const song = await getSong(id)
if (song) {
res.json(song)
} else {
res.status(404).json({ error: "not found"})
}
})

// ADD SONG
songs.post("/", async (req, res) => {
try {
const song = await createSong(req.body);
res.json(song);
} catch (error) {
res.status(400).json({ error: error})
}
})

// DELETE SONG
songs.delete("/:id", async (req, res) => {
try {
const { id } = req.params;
const deletedSong = await deleteSong(id)
res.status(200).json(deletedSong)
} catch (error) {
res.status(404).json({ error: "id not found" })
}
})

// UPDATE SONG
songs.put("/:id", async (req, res) => {
try {
const { id } = req.params
const updatedSong = await updateSong(id, req.body)
res.status(200).json(updatedSong)
} catch (error) {
res.status(404).json({ error: "song not found" })
}
})

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;
13 changes: 13 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
);
8 changes: 8 additions & 0 deletions db/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
\c songs_dev;

INSERT INTO songs (name, artist, album, time, is_favorite) VALUES
('Fireflies', 'Owl City', 'Ocean Eyes (Deluxe Edition)', '3:48', true),
('Jane', 'Jefferson Starship', 'Freedom at Point Zero', '4:00', true),
('Prodigal Daughter', 'LIGHTS', 'PEP', '2:55', false),
('Part II', 'Paramore', 'Paramore (Deluxe Edition)', '4:41', false),
('blackpill', 'Kala', 'blackpill', '3:11', true);
Loading