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

Updating - #6

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
validations ✅, controller set for id, server up ✅
  • Loading branch information
anickacodes committed Nov 2, 2023
commit d455e1a5d107413294fb272f56a326e332ca9f66
20 changes: 18 additions & 2 deletions back-end/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// Import express and initialize an instance of the express server
const express = require('express')
const app = express()


//Import cors
const cors = require('cors')
const songsController = require('./controllers/songController')
// Middleware
app.use(cors())
app.use(express.json())
// app.use(cors())

app.use('/songs', songsController)

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

app.get('*', (req, res)=> {
res.redirect()
})

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

songs.get("/", async (req, res) => {
const allSongs = await getAllSongs();
if (allSongs[0]) {
res.status(200).json(allSongs);
} else {
res.status(500).json({ error: "No songs available" });
}
});


songs.get("/:id", async (req, res) => {
const id = req.params.id
const oneSong = await getOneSong(id);
if (oneSong) {
res.status(200).json(oneSong);
} else {
res.status(404).json({ error: "This song is not available" });
}
});


module.exports = songs;
17 changes: 17 additions & 0 deletions back-end/db/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const pgp = require("pg-promise")();
// connection to database

require("dotenv").config();

// tells pg(postgres) which database we're connecting to
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);

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

\c tunes_app;

CREATE TABLE songs (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
artist TEXT NOT NULL,
-- album TEXT NOT NULL,
-- time TEXT NOT NULL,
is_favorite BOOLEAN
);
15 changes: 15 additions & 0 deletions back-end/db/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

\c tunes_app;

INSERT INTO songs ( name, artist, is_favorite) VALUES ('Dj NovaK', 'sCARED oF nUTHIN (promo)', true),
('Ke$ha', 'Praying', true),
('Meek', 'Lyrically Easy', true),
('Dante Bowe', 'Fire', false),
('Rihanna', 'Love on the Brain', true),
('A Boogie Wit da Hoodie', 'Did Me Wrong', true),
('Alicia Creti', 'Congratulations', false),
('Prince', 'Diamonds & Pearls', true),
('Grace', 'Peace & Love', false),
('Sk', 'SK Banga', true);

--psql -U postgres -f db/seed. sal.
21 changes: 21 additions & 0 deletions back-end/models/songs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const db = require("../db/dbConfig");

const getAllSongs = async () => {
try {
const allSongs = await db.any("SELECT * FROM songs");
return allSongs;
} catch (error) {
return error;
}
};

const getOneSong = async (id) => {
try {
const oneSong = await db.one(" SELECT * FROM songs WHERE id=$1", id);
return oneSong;
} catch (error) {
return error;
}
};

module.exports = { getAllSongs, getOneSong };
Loading