Skip to content

Turner App #15

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

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

app.use(cors());
app.use(express.json());
app.use("/songs", songsController);

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

app.get("*", (req, res) => {
res.status(404).send("error route doesnt exist");
});

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

//GET - index
songs.get("/", async (req, res) => {
const allSongs = await getAllSongs();
if (allSongs) {
res.status(200).json(allSongs);
} else {
res.status(404).json({ error: "Array is empty" });
}
});

//GET - show
songs.get("/:id", async (req, res) => {
const { id } = req.params;
const oneSong = await getSong(id);
if (getSong) {
res.status(200).json(oneSong);
} else {
res.status(404).json({ error: "Id not found" });
}
});

//CREATE - post
songs.post("/", async (req, res) => {
const body = req.body;
const newSong = await insertSong(body);
res.status(200).json(newSong);
});

//PUT - update
songs.put("/:id", async (req, res) => {
const { id } = req.params;
const body = req.body;
const updatedSong = await updateSong(id, body);
if (updatedSong) {
res.status(200).json(updatedSong);
} else {
res.status(404).json({ error: "song not found" });
}
});

//DELETE - destroy
songs.delete("/:id", async (req, res) => {
const { id } = req.params;
const deletedSong = await deleteSong(id);
if (deleteSong.id) {
res.status(200).json(deletedSong);
} else {
res.status(400).json({ error: "Song not found" });
}
});
module.exports = songs;
13 changes: 13 additions & 0 deletions back-end/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;
7 changes: 7 additions & 0 deletions back-end/db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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);

7 changes: 7 additions & 0 deletions back-end/db/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\c songs_dev;

INSERT INTO songs (name, artist, album, time, is_favorite) VALUES ('Song 1', 'Artist 1', 'Album 1', '3:45', false),
('Song 2', 'Artist 2', 'Album 2', '4:20', true),
('Song 3', 'Artist 1', 'Album 1', '3:10', false),
('Song 4', 'Artist 3', 'Album 3', '5:00', true),
('Song 5', 'Artist 4', 'Album 4', '2:55', false);
Loading