Skip to content
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
125 changes: 125 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Mac Cruft
.DS_Store

# VSCode
.vscode

# databases
*.db3
42 changes: 42 additions & 0 deletions api/bands/bands-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const db = require('../../data/dbConfig');

const getAll = () => {
return db('bands');
};

const getById = async (band_id) => {
const band = await db('bands')
.where({ band_id })
.first();
return band;
};

const add = async (band) => {
const [id] = await db('bands').insert(band);
return getById(id);
};

const update = async (band_id, payload) => {
await db('bands')
.where('band_id', band_id)
.update(payload);

const updatedBand = await getById(band_id);
return updatedBand;
};

const remove = async (band_id) => {
const deletedBand = await db('bands')
.where({ band_id })
.del();

return deletedBand;
};

module.exports = {
getAll,
getById,
add,
update,
remove,
}
54 changes: 54 additions & 0 deletions api/bands/bands-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const router = require('express').Router();
const Bands = require('./bands-model');

router.get('/', (req, res) => {
Bands.getAll()
.then((bands) => {
res.json(bands);
})
.catch((error) => {
res.status(500).json({
message: 'Something went wrong getting bands',
error: error,
});
});
});

router.get('/:band_id', async (req, res) => {
try {
const band = await Bands.getById(req.params.band_id);
res.json(band);
} catch (error) {
res.status(404).json({
message: `A band with ${req.params.band_id} could not be found`,
error: error,
});
}
});

router.put('/:band_id', async (req, res) => {
const bandId = req.params.band_id;
try {
const theUpdatedBandIs = await Bands.update(bandId, req.body);
res.json(theUpdatedBandIs);
} catch (error) {
res.status(500).json({
message: `There was an issue updating ${bandId}, please check your payload and try again.`,
error: error,
});
}
});

router.post('/', async (req, res) => {
try {
const newBand = await Bands.add(req.body);
res.status(201).json(newBand);
} catch (error) {
res.status(500).json({
message: `Something went wrong adding the band, ${req.body.band_name}`,
error: error,
});
}
});

module.exports = router;
12 changes: 12 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const server = express();
const bandsRouter = require('./bands/bands-router');

server.use(express.json());
server.use('/api/bands', bandsRouter);

server.get('/', (req, res) => {
res.send('The API is working!');
});

module.exports = server;
33 changes: 33 additions & 0 deletions api/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const request = require('supertest');
const server = require('./server');

describe('GET to /api/bands', () => {
test('API responds with the correct friends list', async () => {
const response = await request(server).get('/api/bands');
expect(response.body).toHaveLength(5);
});
});

describe('GET to /api/bands/2', () => {
test('API responds with the correct friend for an ID of 2', async () => {
const band = {band_id: 2, band_name: 'TesseracT', genre: 'Progressive Metal'};
const response = await request(server).get('/api/bands/2');
expect(response.body).toMatchObject(band);
});
});

describe('PUT to /api/bands/1', () => {
test('API successfully updates Band with the ID of 1', async () => {
const band = { band_name: 'Opeth', genre: 'Progressive and Death Metal' };
const response = await request(server).put('/api/bands/1').send(band);
expect(response.status).toBe(200);
});
});

describe('POST to /api/bands', () => {
test('API successfully creates a new band', async () => {
const band = { band_name: 'Anderson Paak', genre: 'Multiple' };
const response = await request(server).post('/api/bands').send(band);
expect(response.status).toBe(201);
});
});
6 changes: 6 additions & 0 deletions data/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const knex = require('knex');
const config = require('../knexfile');

const dbEnv = process.env.DB_ENV || 'development';

module.exports = knex(config[dbEnv]);
12 changes: 12 additions & 0 deletions data/migrations/20240908214615_create_bands_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex) {
return knex.schema.createTable('bands', bands => {
bands.increments('band_id');
bands.string('band_name', 255).notNullable().unique();
bands.string('genre', 128).notNullable();
})
};

exports.down = function(knex) {
return knex.schema.dropTableIfExists('bands');
};
15 changes: 15 additions & 0 deletions data/seeds/001-bands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const bands = [
{band_id: 1, band_name: 'Opeth', genre: 'Progressive Metal'},
{band_id: 2, band_name: 'TesseracT', genre: 'Progressive Metal'},
{band_id: 3, band_name: 'Umphreys McGee', genre: 'Jam Band'},
{band_id: 4, band_name: 'Consider the Source', genre: 'Jazz fusion'},
{band_id: 5, band_name: 'Emancipator', genre: 'downtempo'},
]

exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('bands').truncate()
.then(function () {
return knex('bands').insert(bands)
})
};
28 changes: 28 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import globals from 'globals';
import pluginJs from '@eslint/js';
import pluginJest from 'eslint-plugin-jest';

export default [
{
files: ['**/*.js'],
languageOptions: {
sourceType: 'commonjs',
},
},
{
files: ['**/*.test.js'], // Apply Jest settings only to test files
languageOptions: {
globals: {
...globals.browser,
...globals.jest, // Include Jest globals
},
},
plugins: {
jest: pluginJest,
},
rules: {
...pluginJest.configs.recommended.rules, // Apply recommended Jest rules
},
},
pluginJs.configs.recommended,
];
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('dotenv').config();
const server = require('./api/server');

const PORT = process.env.PORT || 3000;

server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Loading