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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

node_modules/
106 changes: 106 additions & 0 deletions api/shows.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const request = require('supertest')
const db = require('../data/dbConfig')
const server = require('../server.js')
const Show = require('./showsModel.js')

const show1 = {show_name:'Sweet Tooth', streaming_service: 'Netflix'}
const show2 = {show_name:'Slow Horses', streaming_service: 'Apple'}

beforeAll(async () => {
await db.migrate.rollback()
await db.migrate.latest()
})

beforeEach(async () => {
await db('shows').truncate()
})

afterAll(async () => {
await db.destroy()
})


it('correct env', () => {
expect(process.env.DB_ENV).toBe('testing')
})

describe('shows model functions', () => {
describe('create show', () => {
it('adds a show to the db', async () => {
let shows
await Show.createShow(show1)
shows = await db('shows')
expect(shows).toHaveLength(1)

await Show.createShow(show2)
shows = await db('shows')
expect(shows).toHaveLength(2)
})
it('inserted show and service', async () => {
const show = await Show.createShow(show1)
expect(show).toMatchObject({show_id:1,...show})
})
})
describe('[DELETE] / -deletes show', () => {
it('removes show from db', async () => {
const [show_id] = await db('shows').insert(show1)
let show = await db('shows').where({show_id}).first()
expect(show).toBeTruthy()
await request(server).delete('/shows/'+ show_id)
show = await db('shows').where({show_id}).first()
expect(show).toBeFalsy()
})
it('responds with the deleted show', async () => {
await db('shows').insert(show1)
let show = await request(server).delete('/shows/1')
expect(show.body).toMatchObject(show1)
})
})
describe("[GET] / get show by ID", () => {
test("get show by ID", async () => {
const [show_id] = await db("shows").insert(show1);
const response = await request(server).get(`/shows/${show_id}`);
expect(response.status).toBe(200);
expect(response.body).toMatchObject(show1);
});
test("get show by non-existent ID", async () => {
const response = await request(server).get("/shows/999");
expect(response.status).toBe(404);
expect(response.body).toEqual({ message: "show not found" });
});
});

describe("[PUT] / update show", () => {
test("update show in database", async () => {
const [show_id] = await db("shows").insert(show1);
const updates = { show_name:'', streaming_service:''};
const response = await request(server)
.put(`/shows/${show_id}`)
.send(updates);
expect(response.status).toBe(200);
expect(response.body).toMatchObject(updates);
const updatedshow = await db("shows")
.where("show_id", show_id)
.first();
expect(updatedshow).toMatchObject(updates);
});
test("respond with the updated show", async () => {
const [show_id] = await db("shows").insert(show1);
const updates = { show_name:'', streaming_service:''};
const response = await request(server)
.put(`/shows/${show_id}`)
.send(updates);
expect(response.body).toMatchObject(updates);
});
test("respond with 404 for non-existent show", async () => {
const nonExistentId = 999;
const updates = { show_name:'', streaming_service:''};
const response = await request(server)
.put(`/shows/${nonExistentId}`)
.send(updates);

expect(response.status).toBe(404);
expect(response.body).toEqual({ message: "show not found" });
});
})
})
33 changes: 33 additions & 0 deletions api/showsModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const db = require('../data/dbConfig')

async function createShow(show){
const [id] = await db('shows').insert(show)
return db('shows').where('show_id',id).first()
}

async function deleteShow(id) {
const show = await db('shows').where('show_id',id).first()
await db('shows').where('show_id',id).del()
return show
}

async function getAll() {
return await db("shows");
}

async function getById(id) {
return await db("shows").where("show_id", id).first();
}

async function updatedShow(id, updates) {
await db("shows").where("show_id", id).update(updates);
return db("shows").where("show_id", id).first();
}

module.exports = {
createShow,
deleteShow,
updatedShow,
getAll,
getById
}
45 changes: 45 additions & 0 deletions api/showsRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const express = require('express')
const router = express.Router()
const Show = require('./showsModel.js')
const db = require('../data/dbConfig.js')

router.get("/", async (req, res) => {
const shows = await Show.getAll();
res.status(200).json(shows);
});

router.get("/:id", async (req, res) => {
const { id } = req.params;
const show = await Show.getById(id);

if (show) {
res.status(200).json(show);
} else {
res.status(404).json({ message: "show not found" });
}
});

router.put("/:id", async (req, res) => {
const { id } = req.params;
const updates = req.body;

const existingshow = await db("shows")
.where("show_id", id)
.first();

if (!existingshow) {
return res.status(404).json({ message: "show not found" });
}
const updatedShow = await Show.updatedShow(id, updates);
res.status(200).json(updatedShow);
});



router.delete('/:id', async (req,res) => {
const id = req.params.id
const delShow = await Show.deleteShow(id)
res.status(200).json(delShow)
})

module.exports = router
5 changes: 5 additions & 0 deletions data/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const knex = require('knex')
const configs = require('../knexfile.js')
const env = process.env.DB_ENV || 'development'

module.exports = knex(configs[env])
13 changes: 13 additions & 0 deletions data/migrations/20240207062326_initial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function(knex) {
return knex.schema
.createTable('shows', tbl => {
tbl.increments('show_id')
tbl.text('show_name').notNullable()
tbl.text('streaming_service').notNullable()
})
};

exports.down = function(knex) {
return knex.schema
.dropTableIfExists('shows')
};
12 changes: 12 additions & 0 deletions data/seeds/shows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.seed = function(knex) {
return knex('shows').truncate()
.then(function () {
return knex('shows').insert([
{ show_name: 'Criminal Minds', streaming_service: 'Hulu' },
{ show_name: 'Handmades Tale', streaming_service: 'Hulu' },
{ show_name: 'Reacher', streaming_service: 'Prime' },
{ show_name: 'For All Mankind', streaming_service: 'Apple' },
{ show_name: 'The Last of Us', streaming_service: 'Max' },
])
})
}
Binary file added data/shows.db3
Binary file not shown.
Binary file added data/test.db3
Binary file not shown.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const server = require('./server.js')

server.listen(1234, () => {
console.log("Port running on 1234")
})
31 changes: 31 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Update with your config settings.

module.exports = {

development: {
client: 'sqlite3',
useNullAsDefault: true,
connection: {
filename: './data/shows.db3'
},
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
}
},
testing: {
client: 'sqlite3',
useNullAsDefault: true,
connection: {
filename: './data/test.db3'
},
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
}
}
};
Loading