Skip to content

Commit

Permalink
Create driver
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmccombe committed Apr 20, 2017
1 parent ea74cf3 commit 84c259d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routes = require('./routes/routes');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://172.17.0.3/muber');

const app = express();

app.use(bodyParser.json());
routes(app);

module.exports = app;
8 changes: 8 additions & 0 deletions controllers/drivers_controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const Driver = require('../models/driver');

module.exports = {
greeting(req, res) {
res.send({ hi: 'there' });
},
create(req, res) {
const driverProps = req.body;

Driver.create(driverProps)
.then(driver => res.send(driver));
}
};
17 changes: 17 additions & 0 deletions models/driver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const DriverSchema = new Schema({
email: {
type: String,
required: true
},
driving: {
type: Boolean,
default: false
}
});

const Driver = mongoose.model('driver', DriverSchema);

module.exports = Driver;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "nodemon --exec 'mocha --recursive -R min'"
"test": "nodemon --exec 'mocha --recursive'"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"mocha": "^3.2.0",
"mongoose": "^4.9.5",
Expand Down
1 change: 1 addition & 0 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ const DriversController = require('../controllers/drivers_controller');

module.exports = (app) => {
app.get('/api', DriversController.greeting);
app.post('/api/drivers', DriversController.create)
};
22 changes: 22 additions & 0 deletions test/controllers/drivers_controller_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require('mongoose');
const assert = require('assert');
const request = require('supertest');
const app = require('../../app');

const Driver = mongoose.model('driver');

describe('Drivers controller', () => {
it('creates a new driver', (done) => {
Driver.count().then(count => {
request(app)
.post('/api/drivers')
.send({ email: '[email protected]' })
.end((err, res) => {
Driver.count().then(newCount => {
assert(newCount === count + 1);
done();
})
});
})
})
});

0 comments on commit 84c259d

Please sign in to comment.