Skip to content

Commit

Permalink
added routes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Aug 23, 2017
1 parent 928c27e commit 79d1c1b
Show file tree
Hide file tree
Showing 24 changed files with 867 additions and 1 deletion.
Empty file added .travis.yml
Empty file.
3 changes: 3 additions & 0 deletions _live/node-koa-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules/
npm-debug.log
43 changes: 43 additions & 0 deletions _live/node-koa-api/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"globals": {
"esnext": true,
"jasmine": false,
"spyOn": false,
"it": false,
"console": false,
"describe": false,
"expect": false,
"beforeEach": false,
"afterEach": false,
"waits": false,
"waitsFor": false,
"runs": false,
"$": false,
"confirm": false
},
"esnext": true,
"node" : true,
"browser" : true,
"boss" : false,
"curly": false,
"debug": false,
"devel": false,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": true,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": true,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": true,
"strict": false,
"white": true,
"unused": false
}
21 changes: 21 additions & 0 deletions _live/node-koa-api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Michael Herman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions _live/node-koa-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Building a RESTful API with Koa and Postgres

Project base...

1. Fork/Clone
1. Install dependencies - `npm install`
1. Sanity check - `npm start`
1. Test - `npm test`
22 changes: 22 additions & 0 deletions _live/node-koa-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "node-koa-api",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./src/server/index.js",
"test": "./node_modules/mocha/bin/_mocha"
},
"keywords": [],
"author": "Michael Herman",
"license": "ISC",
"devDependencies": {
"chai": "4.1.1",
"chai-http": "^3.0.0",
"mocha": "3.5.0"
},
"dependencies": {
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.2.1"
}
}
23 changes: 23 additions & 0 deletions _live/node-koa-api/src/server/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"id": 1,
"name": "The Land Before Time",
"genre": "Fantasy",
"rating": 7,
"explicit": false
},
{
"id": 2,
"name": "Jurassic Park",
"genre": "Science Fiction",
"rating": 9,
"explicit": true
},
{
"id": 3,
"name": "Ice Age: Dawn of the Dinosaurs ",
"genre": "Action/Romance",
"rating": 5,
"explicit": false
}
]
18 changes: 18 additions & 0 deletions _live/node-koa-api/src/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');

const indexRoutes = require('./routes/index');
const movieRoutes = require('./routes/movies');

const app = new Koa();
const PORT = process.env.PORT || 1337;

app.use(bodyParser());
app.use(indexRoutes.routes());
app.use(movieRoutes.routes());

const server = app.listen(PORT, () => {
console.log(`Server listening on port: ${PORT}`);
});

module.exports = server;
12 changes: 12 additions & 0 deletions _live/node-koa-api/src/server/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Router = require('koa-router');

const router = new Router();

router.get('/', async (ctx) => {
ctx.body = {
status: 'success',
message: 'hello, world!'
};
})

module.exports = router;
49 changes: 49 additions & 0 deletions _live/node-koa-api/src/server/routes/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const Router = require('koa-router');

const data = require('../data.json');
const router = new Router();
const BASE_URL = `/api/v1/movies`;

router.get(BASE_URL, async (ctx) => {
ctx.body = {
status: 'success',
data: data
};
})

router.get(`${BASE_URL}/:id`, async (ctx) => {
const movie = data.filter((el) => {
return el.id === parseInt(ctx.params.id);
});
ctx.body = {
status: 'success',
data: movie
};
})

router.post(`${BASE_URL}`, async (ctx) => {
const newMovie = ctx.request.body;
newMovie.id = data.length + 1;
data.push(newMovie)
const movie = data.filter((el) => {
return el.id === parseInt(newMovie.id);
});
ctx.status = 201;
ctx.body = {
status: 'success',
data: movie
};
})

router.delete(`${BASE_URL}/:id`, async (ctx) => {
const movie = data.filter((el) => {
return el.id === parseInt(ctx.params.id);
});
data.splice(data.indexOf(movie[0]), 1);
ctx.body = {
status: 'success',
data: movie
};
})

module.exports = router;
24 changes: 24 additions & 0 deletions _live/node-koa-api/test/routes.index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
process.env.NODE_ENV = 'test';

const chai = require('chai');
const should = chai.should();
const chaiHttp = require('chai-http');

chai.use(chaiHttp);
const server = require('../src/server/index');

describe('routes : index', () => {
describe('GET /', () => {
it('should return json', (done) => {
chai.request(server)
.get('/')
.end((err, res) => {
res.status.should.eql(200);
res.type.should.eql('application/json');
res.body.status.should.eql('success');
res.body.message.should.eql('hello, world!');
done();
});
});
});
});
87 changes: 87 additions & 0 deletions _live/node-koa-api/test/routes.movies.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
process.env.NODE_ENV = 'test';

const chai = require('chai');
const should = chai.should();
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

const server = require('../src/server/index');

describe('routes : movies', () => {

describe('GET /api/v1/movies', () => {
it('should return all movies', (done) => {
chai.request(server)
.get('/api/v1/movies')
.end((err, res) => {
should.not.exist(err);
res.status.should.equal(200);
res.type.should.equal('application/json');
res.body.status.should.eql('success');
res.body.data.length.should.eql(3);
res.body.data[0].should.include.keys(
'id', 'name', 'genre', 'rating', 'explicit'
);
done();
});
});
});

describe('GET /api/v1/movies/:id', () => {
it('should respond with a single movie', (done) => {
chai.request(server)
.get('/api/v1/movies/1')
.end((err, res) => {
should.not.exist(err);
res.status.should.equal(200);
res.type.should.equal('application/json');
res.body.status.should.eql('success');
res.body.data[0].should.include.keys(
'id', 'name', 'genre', 'rating', 'explicit'
);
done();
});
});
});

describe('POST /api/v1/movies', () => {
it('should return the movie that was added', (done) => {
chai.request(server)
.post('/api/v1/movies')
.send({
name: 'Titanic',
genre: 'Drama',
rating: 8,
explicit: true
})
.end((err, res) => {
should.not.exist(err);
res.status.should.equal(201);
res.type.should.equal('application/json');
res.body.status.should.eql('success');
res.body.data[0].should.include.keys(
'id', 'name', 'genre', 'rating', 'explicit'
);
done();
});
});
});

describe('DELETE /api/v1/movies/:id', () => {
it('should return the movie that was deleted', (done) => {
chai.request(server)
.delete('/api/v1/movies/1')
.end((err, res) => {
should.not.exist(err);
res.status.should.equal(200);
res.type.should.equal('application/json');
res.body.status.should.eql('success');
res.body.data[0].should.include.keys(
'id', 'name', 'genre', 'rating', 'explicit'
);
done();
});
});
});

});
13 changes: 13 additions & 0 deletions _live/node-koa-api/test/sample.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
process.env.NODE_ENV = 'test';

const chai = require('chai');
const should = chai.should();

describe('Sample Test', () => {
it('should pass', (done) => {
const sum = 1 + 2;
sum.should.eql(3);
sum.should.not.eql(4);
done();
});
});
26 changes: 26 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require('path');

const BASE_PATH = path.join(__dirname, 'src', 'server', 'db');

module.exports = {
test: {
client: 'pg',
connection: 'postgres://localhost:5432/koa_api_test',
migrations: {
directory: path.join(BASE_PATH, 'migrations')
},
seeds: {
directory: path.join(BASE_PATH, 'seeds')
}
},
development: {
client: 'pg',
connection: 'postgres://localhost:5432/koa_api',
migrations: {
directory: path.join(BASE_PATH, 'migrations')
},
seeds: {
directory: path.join(BASE_PATH, 'seeds')
}
}
};
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
"license": "ISC",
"devDependencies": {
"chai": "4.1.1",
"chai-http": "^3.0.0",
"mocha": "3.5.0"
},
"dependencies": {
"knex": "^0.13.0",
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.2.1",
"pg": "^7.1.2"
}
}
4 changes: 4 additions & 0 deletions src/server/db/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const environment = process.env.NODE_ENV || 'development';
const config = require('../../../knexfile.js')[environment];

module.exports = require('knex')(config);
13 changes: 13 additions & 0 deletions src/server/db/migrations/20170817152841_movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = (knex, Promise) => {
return knex.schema.createTable('movies', (table) => {
table.increments();
table.string('name').notNullable().unique();
table.string('genre').notNullable();
table.integer('rating').notNullable();
table.boolean('explicit').notNullable();
});
};

exports.down = (knex, Promise) => {
return knex.schema.dropTable('movies');
};
Loading

0 comments on commit 79d1c1b

Please sign in to comment.