Skip to content

Commit

Permalink
added deployment, fixed various api issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjandrews committed Apr 3, 2017
1 parent a04bcb1 commit dec960b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ DS_Store
npm-debug.log
build
jest
*.env
codeship.aes
5 changes: 5 additions & 0 deletions codeship-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ services:
environment:
POSTGRES_USER: todoapp
POSTGRES_DB: todos
deploy:
image: codeship/heroku-deployment
encrypted_env_file: deployment.env.encrypted
volumes:
- ./:/deploy
4 changes: 4 additions & 0 deletions codeship-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
- name: tests
service: web
command: bin/ci "npm test -- --forceExit"
- name: deploy
tag: master
service: deploy
command: codeship_heroku deploy /deploy nodejs-express-todoapp
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"clean": "rm -rf build && mkdir build",
"build-server": "babel src --out-dir build -s --ignore src/__tests__/**",
"build": "npm run clean && npm run build-server",
"start": "npm run build && node ./build/index.js",
"prestart": "npm run build && npm run migrate",
"start": "node ./build/index.js",
"lint": "eslint src",
"test": "jest",
"watch-tests": "esw src & jest --watchAll",
"dev": "nodemon --watch ./src --ignore ./src/__tests__ -x \"npm start\"",
"coverage": "jest --coverage",
"migrate": "node node_modules/db-migrate/bin/db-migrate up all"
"migrate": "node ./node_modules/db-migrate/bin/db-migrate up all"
},
"keywords": [
"todo-backend",
Expand Down
42 changes: 21 additions & 21 deletions src/__tests__/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Todo-Backend API', () => {
describe( 'the pre-requisites', () => {
it( 'the api root responds to a GET (i.e. the server is up and accessible, CORS headers are set up)', (done) => {
request(app)
.get('/')
.get('/todos')
.expect(200)
.end((err) => {
expect(err).toBeNull();
Expand All @@ -16,7 +16,7 @@ describe('Todo-Backend API', () => {
});
it( 'the api root responds to a POST with the todo which was posted to it', (done) => {
request(app)
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
Expand All @@ -27,7 +27,7 @@ describe('Todo-Backend API', () => {
});
it( 'the api root responds successfully to a DELETE', (done) => {
request(app)
.delete('/')
.delete('/todos')
.expect(204)
.end((err) => {
expect(err).toBeNull();
Expand All @@ -36,7 +36,7 @@ describe('Todo-Backend API', () => {
});
it( 'after a DELETE the api root responds to a GET with a JSON representation of an empty array', (done) => {
request(app)
.get('/')
.get('/todos')
.expect(200)
.end((err, res) => {
expect(res.body).toEqual([]);
Expand All @@ -49,7 +49,7 @@ describe('Todo-Backend API', () => {

beforeEach((done) => {
request(app)
.delete('/')
.delete('/todos')
.end((err) => {
expect(err).toBeNull();
done();
Expand All @@ -58,7 +58,7 @@ describe('Todo-Backend API', () => {

it('adds a new todo to the list of todos at the root url', (done) => {
request(app)
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
Expand All @@ -69,7 +69,7 @@ describe('Todo-Backend API', () => {
});
it('sets up a new todo as initially not completed', (done) => {
request(app)
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
Expand All @@ -82,7 +82,7 @@ describe('Todo-Backend API', () => {
let currentTodo;
const client = request(app);
client
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
Expand All @@ -91,7 +91,7 @@ describe('Todo-Backend API', () => {
expect(typeof res.body.url).toEqual('string');
currentTodo = res.body;
client
.get(`/${currentTodo.id}`)
.get(`/todos/${currentTodo.id}`)
.end((err, res) => {
expect(err).toBeNull();
expect(res.body.id).toEqual(currentTodo.id);
Expand All @@ -106,14 +106,14 @@ describe('Todo-Backend API', () => {
let currentTodo;
const client = request(app);
client
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
expect(err).toBeNull();
currentTodo = res.body;
client
.patch(`/${currentTodo.id}`)
.patch(`/todos/${currentTodo.id}`)
.send({'title': 'different title now'})
.end((err, res) => {
expect(err).toBeNull();
Expand All @@ -126,15 +126,15 @@ describe('Todo-Backend API', () => {
let currentTodo;
const client = request(app);
client
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
expect(err).toBeNull();
expect(res.body.completed).toEqual(false);
currentTodo = res.body;
client
.patch(`/${currentTodo.id}`)
.patch(`/todos/${currentTodo.id}`)
.send({'completed': true})
.end((err, res) => {
expect(err).toBeNull();
Expand All @@ -147,19 +147,19 @@ describe('Todo-Backend API', () => {
let currentTodo;
const client = request(app);
client
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
expect(err).toBeNull();
currentTodo = res.body;
client
.patch(`/${currentTodo.id}`)
.patch(`/todos/${currentTodo.id}`)
.send({'title': 'new title', 'completed': true})
.end((err) => {
expect(err).toBeNull();
client
.get(`/${currentTodo.id}`)
.get(`/todos/${currentTodo.id}`)
.end((err,res) => {
expect(err).toBeNull();
expect(res.body.completed).toEqual(true);
Expand All @@ -173,14 +173,14 @@ describe('Todo-Backend API', () => {
let currentTodo;
const client = request(app);
client
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
expect(err).toBeNull();
currentTodo = res.body;
client
.delete(`/${currentTodo.id}`)
.delete(`/todos/${currentTodo.id}`)
.expect(204)
.end((err) => {
expect(err).toBeNull();
Expand All @@ -195,19 +195,19 @@ describe('Todo-Backend API', () => {
let currentTodo;
const client = request(app);
client
.post('/')
.post('/todos')
.send(todo)
.expect(201)
.end((err, res) => {
expect(err).toBeNull();
currentTodo = res.body;
client
.patch(`/${currentTodo.id}`)
.patch(`/todos/${currentTodo.id}`)
.send({'order': 123})
.end((err) => {
expect(err).toBeNull();
client
.get(`/${currentTodo.id}`)
.get(`/todos/${currentTodo.id}`)
.end((err,res) => {
expect(err).toBeNull();
expect(res.body.order).toEqual(123);
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import app from './server';

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

app.listen(port, () => {
console.log(`Your app is running on port ${port}`); // eslint-disable-line
Expand Down
4 changes: 2 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import routes from './todos/routes';
const app = express();

// app middleware
app.use(bodyParser.json({type: 'application/json'}));
app.use(cors());
app.use(bodyParser.json({type: 'application/json'}));
app.disable('etag');

app.use('/', routes());
app.use('/todos', routes());

const errorHandler = new ErrorHandler();

Expand Down
2 changes: 1 addition & 1 deletion src/todos/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function cleanUpTodoObject(todo, baseUrl) {
if (value === null || value === undefined) delete todo[key];
}

todo.url = `${baseUrl}/${todo.id}`;
todo.url = `${baseUrl}/todos/${todo.id}`;

return todo;
}
Expand Down
5 changes: 5 additions & 0 deletions src/todos/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default function () {
}

async function createTodo(req, res, next) {
if (req.body.order) {
req.body.position = req.body.order;
delete req.body.order;
}
const todo = await db.create(todoTable, req.body)
.catch((err) => next(err));
res.locals.todo = todo[0];
Expand All @@ -57,6 +61,7 @@ export default function () {
todo.position = todo.order;
delete todo.order;
}

const updatedTodo = await db.update(todoTable, req.params.id, todo)
.catch((err) => next(err));
res.locals.todo = updatedTodo[0];
Expand Down

0 comments on commit dec960b

Please sign in to comment.