Skip to content
Open

mvp #506

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
Empty file modified README.md
100644 → 100755
Empty file.
57 changes: 57 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const express = require('express');

const Games = require('../games/gamesModel.js');
const db = require('../data/dbConfig.js');

const server = express();

server.use(express.json());

server.get('/', (req, res) => {
res.status(200).json({ api: 'up and running!' });
});

server.get('/games', (req, res) => {
Games.getAll()
.then(games => {
res.status(200).json(games);
})
.catch(error => {
res.status(500).json(error);
});
});

server.get('/games/:id', async (req, res) => {
try {
const game = await Games.getById(req.params.id);
if(game) {
res.status(200).json(game);
} else {
res.status(500).json({error: error, message: `Game with requested id (${req.params.id}) is not found`});
}
} catch (error) {
res.status(500).json(error);
}
});

server.post('/games', (req, res) => {
const { title, genre, releaseYear } = req.body;
if (!title || !genre) {
return res.status(422).json({ error: 'title and genre are required' });
} else {
const newGame = { title, genre, releaseYear };
if (!Games.getGameByFilter({title: newGame.title})) {
res.status(405).json({ error: 'Need a unique title' });
} else {
Games.insert(newGame)
.then(games => {
res.status(201).json(games);
})
.catch(error => {
res.status(500).json(error);
});
}
}
});

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

const server =require('./server.js');
const db = require('../data/dbConfig.js');


describe('server.js', () => {
it('should set the test env', () => {
expect(process.env.DB_ENV).toBe('testing');
});
});

describe('GET /', () => {
afterEach(async () => {
await db('games').truncate();
});

it('should return 200', async () => {
const res = await request(server).get('/');
expect(res.status).toBe(200);
});

it('should return JSON', async () => {
const res = await request(server).get('/');
expect(res.type).toBe('application/json');
});

it('should return api: up', async () => {
const res = await request(server).get('/');
expect(res.body).toEqual({ api: 'up and running!'});
});
});

describe('GET /games', () => {

afterEach(async () => {
await db('games').truncate();
});

it('should return 200', async () => {
const res = await request(server).get('/games');
expect(res.status).toBe(200);
});

it('should return games', async () => {
const res = await request(server).get('/games');
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
});

it('should return all games in db', async () => {
// await db('games').truncate();
const games = [
{
id: 1,
title: "Pacman",
genre: "Arcade",
releaseYear: 1980
}
];

await db('games').insert(games);

const res = await request(server).get('/games');
expect(res.status).toBe(200);
expect(res.body).toEqual(games);
});
});
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.js');

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

module.exports = knex(config[environment]);
Binary file added data/games.db3
Binary file not shown.
14 changes: 14 additions & 0 deletions data/migrations/20190721155159_games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.up = function(knex, Promise) {
return knex.schema.createTable('games', tbl => {
tbl.increments();

tbl.string('title', 255).notNullable().unique();
tbl.string('genre', 255).notNullable();
tbl.integer('releaseYear');
});
};

exports.down = function(knex, Promise) {
// undo the operation in up
return knex.schema.dropTableIfExists('games');
};
5 changes: 5 additions & 0 deletions data/seeds/000-cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const cleaner = require('knex-cleaner');

exports.seed = function(knex) {
return cleaner.clean(knex);
};
6 changes: 6 additions & 0 deletions data/seeds/001-games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

exports.seed = function(knex) {
return knex('games').insert([
// {title: "GTA V", genre: "Action-adventure", releaseYear: 2013 },
]);
};
Binary file added data/test.db3
Binary file not shown.
25 changes: 25 additions & 0 deletions games/gamesModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const db = require('../data/dbConfig.js');

module.exports = {
getAll,
insert,
getGameByFilter,
getById
};

function getAll() {
return db('games');
}

async function insert(game) {
const [id] = await db('games').insert(game);
return getById(id);
}

function getGameByFilter(filter) {
return db('games').where(filter).first();
}

function getById(id) {
return db('games'). where({ id }).first();
}
78 changes: 78 additions & 0 deletions games/gamesModel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const request = require('supertest');
const server = require('../api/server.js');

const db = require('../data/dbConfig.js');
const Games = require('./gamesModel.js');

describe('the games model', () => {
describe('insert()', () => {
// cleanup for db
afterEach(async () => {
await db('games').truncate();
});

it('should insert games into the db', async () => {
await db('games').truncate();
await Games.insert([
{ id: 1, title: "Agricola", genre: "board game", releaseYear: 2007 },
{ id: 2, title: "The Legend of Zelda", genre: "Action-adventure", releaseYear: 1986 }
]);

const games = await db('games');

expect(games).toHaveLength(2);
expect(games[0].title).toBe('Agricola');
});

it('should return a status code of 201', async () => {
let response = await request(server).post('/games').send({ title: 'Pacman', genre: 'Arcade', releaseYear: 1980 });

expect(response.status).toBe(201);
});

it('should return the new game on insert', async () => {
const game = await Games.insert({ title: "The Legend of Zelda", genre: "Action-adventure", releaseYear: 1986 });

expect(game).toEqual({ id: 1, title: "The Legend of Zelda", genre: "Action-adventure", releaseYear: 1986 });
});

it('should return a `422` status code if title and/or genre are not included inside the body', async () => {
let response = await request(server).post('/games').send({ releaseYear: 1980 });

expect(response.status).toBe(422);
});

xit('should return a `405` status code, if client tries to create a duplicate game', async () => {

await Games.insert({ title: "Agricola", genre: "board game", releaseYear: 2007 });

let response = await request(server).post('/games').send({ title: "Agricola", genre: "board game", releaseYear: 2007 });

expect(response.status).toBe(405);
});
});

describe('getById()', () => {
// cleanup for db
afterEach(async () => {
await db('games').truncate();
});

it('finds a game by id', async () => {
await db('games').insert([
{ id: 1, title: "Agricola", genre: "board game", releaseYear: 2007 },
{ id: 2, title: "The Legend of Zelda", genre: "Action-adventure", releaseYear: 1986 }
]);

const game = await Games.getById(2);

expect(game.title).toEqual("The Legend of Zelda");
});

it('returns undefined of invalid id', async () => {
const game = await Games.getById(2);

expect(game).toBeUndefined();
});
});
});
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('dotenv').config();

const server = require('./api/server.js');

const port = process.env.PORT || 5000;
server.listen(port, () => console.log(`\n**** Server running on port ${port} ****\n`));
30 changes: 30 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Update with your config settings.

module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './data/games.db3',
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations',
},
seeds: {
directory: './data/seeds',
},
},
testing: {
client: 'sqlite3',
connection: {
filename: './data/test.db3',
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations',
},
seeds: {
directory: './data/seeds',
},
},
};
11 changes: 9 additions & 2 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
"description": "Testing Sprint Challenge",
"main": "index.js",
"scripts": {
"test": "jest --watch --verbose",
"test": "cross-env DB_ENV=testing jest --watchAll --verbose",
"start": "nodemon index.js"
},
"jest": {
"testEnvironment": "node"
},
"repository": {
"type": "git",
"url": "git+https://github.com/LambdaSchool/Sprint-Challenge--Testing.git"
Expand All @@ -19,10 +22,14 @@
},
"homepage": "https://github.com/LambdaSchool/Sprint-Challenge--Testing#readme",
"dependencies": {
"express": "^4.16.4"
"express": "^4.17.1",
"knex": "^0.19.0",
"sqlite3": "^4.0.9"
},
"devDependencies": {
"cross-env": "^5.2.0",
"jest": "^23.6.0",
"knex-cleaner": "^1.2.1",
"supertest": "^3.3.0"
}
}
Loading