Skip to content

Commit

Permalink
One style change to rule them all. (GoogleCloudPlatform#47)
Browse files Browse the repository at this point in the history
* Switched from JSHint to Semistandard. Upgraded some dependencies I missed in my last pass.

* Remove Node.js 0.10 from .travis.yml
  • Loading branch information
jmdobry committed Apr 8, 2016
1 parent a3a1dbb commit bf56e5d
Show file tree
Hide file tree
Showing 61 changed files with 1,032 additions and 799 deletions.
22 changes: 0 additions & 22 deletions .jshintrc

This file was deleted.

1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ language: node_js
node_js:
- "stable"
- "0.12"
- "0.10"

cache:
directories:
Expand Down
1 change: 0 additions & 1 deletion 1-hello-world/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var express = require('express');

var app = express();


// [START hello_world]
// Say hello!
app.get('/', function (req, res) {
Expand Down
2 changes: 1 addition & 1 deletion 1-hello-world/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var path = require('path');

module.exports = {
test: '1-hello-world',
path: path.resolve(path.join(__dirname, '../')),
path: path.resolve(path.join(__dirname, '../')),
cmd: 'node',
args: ['app.js'],
msg: 'Hello, world!'
Expand Down
33 changes: 21 additions & 12 deletions 2-structured-data/books/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var express = require('express');
var bodyParser = require('body-parser');

module.exports = function (model) {

var router = express.Router();

// Automatically parse request body as JSON
Expand All @@ -28,9 +27,11 @@ module.exports = function (model) {
*
* Retrieve a page of books (up to ten at a time).
*/
router.get('/', function list(req, res, next) {
router.get('/', function list (req, res, next) {
model.list(10, req.query.pageToken, function (err, entities, cursor) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.json({
items: entities,
nextPageToken: cursor
Expand All @@ -43,9 +44,11 @@ module.exports = function (model) {
*
* Create a new book.
*/
router.post('/', function insert(req, res, next) {
router.post('/', function insert (req, res, next) {
model.create(req.body, function (err, entity) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.json(entity);
});
});
Expand All @@ -55,9 +58,11 @@ module.exports = function (model) {
*
* Retrieve a book.
*/
router.get('/:book', function get(req, res, next) {
router.get('/:book', function get (req, res, next) {
model.read(req.params.book, function (err, entity) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.json(entity);
});
});
Expand All @@ -67,9 +72,11 @@ module.exports = function (model) {
*
* Update a book.
*/
router.put('/:book', function update(req, res, next) {
router.put('/:book', function update (req, res, next) {
model.update(req.params.book, req.body, function (err, entity) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.json(entity);
});
});
Expand All @@ -79,17 +86,19 @@ module.exports = function (model) {
*
* Delete a book.
*/
router.delete('/:book', function _delete(req, res, next) {
router.delete('/:book', function _delete (req, res, next) {
model.delete(req.params.book, function (err) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.status(200).send('OK');
});
});

/**
* Errors on "/api/books/*" routes.
*/
router.use(function handleRpcError(err, req, res, next) {
router.use(function handleRpcError (err, req, res, next) {
// Format error and forward to generic error handler for logging and
// responding to the request
err.response = {
Expand Down
43 changes: 27 additions & 16 deletions 2-structured-data/books/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ var express = require('express');
var bodyParser = require('body-parser');

module.exports = function (model) {

var router = express.Router();

// Automatically parse request body as form data
router.use(bodyParser.urlencoded({ extended: false }));

// Set Content-Type for all responses for these routes
router.use(function (req, res, next){
router.use(function (req, res, next) {
res.set('Content-Type', 'text/html');
next();
});
Expand All @@ -34,9 +33,11 @@ module.exports = function (model) {
*
* Display a page of books (up to ten at a time).
*/
router.get('/', function list(req, res, next) {
router.get('/', function list (req, res, next) {
model.list(10, req.query.pageToken, function (err, entities, cursor) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.render('books/list.jade', {
books: entities,
nextPageToken: cursor
Expand All @@ -50,7 +51,7 @@ module.exports = function (model) {
* Display a form for creating a book.
*/
// [START add_get]
router.get('/add', function addForm(req, res) {
router.get('/add', function addForm (req, res) {
res.render('books/form.jade', {
book: {},
action: 'Add'
Expand All @@ -64,12 +65,14 @@ module.exports = function (model) {
* Create a book.
*/
// [START add_post]
router.post('/add', function insert(req, res, next) {
router.post('/add', function insert (req, res, next) {
var data = req.body;

// Save the data to the database.
model.create(data, function (err, savedData) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.redirect(req.baseUrl + '/' + savedData.id);
});
});
Expand All @@ -80,9 +83,11 @@ module.exports = function (model) {
*
* Display a book for editing.
*/
router.get('/:book/edit', function editForm(req, res, next) {
router.get('/:book/edit', function editForm (req, res, next) {
model.read(req.params.book, function (err, entity) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.render('books/form.jade', {
book: entity,
action: 'Edit'
Expand All @@ -95,11 +100,13 @@ module.exports = function (model) {
*
* Update a book.
*/
router.post('/:book/edit', function update(req, res, next) {
router.post('/:book/edit', function update (req, res, next) {
var data = req.body;

model.update(req.params.book, data, function (err, savedData) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.redirect(req.baseUrl + '/' + savedData.id);
});
});
Expand All @@ -109,9 +116,11 @@ module.exports = function (model) {
*
* Display a book.
*/
router.get('/:book', function get(req, res, next) {
router.get('/:book', function get (req, res, next) {
model.read(req.params.book, function (err, entity) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.render('books/view.jade', {
book: entity
});
Expand All @@ -123,17 +132,19 @@ module.exports = function (model) {
*
* Delete a book.
*/
router.get('/:book/delete', function _delete(req, res, next) {
router.get('/:book/delete', function _delete (req, res, next) {
model.delete(req.params.book, function (err) {
if (err) { return next(err); }
if (err) {
return next(err);
}
res.redirect(req.baseUrl);
});
});

/**
* Errors on "/books/*" routes.
*/
router.use(function handleRpcError(err, req, res, next) {
router.use(function handleRpcError (err, req, res, next) {
// Format error and forward to generic error handler for logging and
// responding to the request
err.response = err.message;
Expand Down
41 changes: 26 additions & 15 deletions 2-structured-data/books/model-cloudsql.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ var extend = require('lodash').assign;
var mysql = require('mysql');

module.exports = function (config) {

function getConnection() {
function getConnection () {
return mysql.createConnection(extend({
database: 'library'
}, config.mysql));
}

// [START list]
function list(limit, token, cb) {
function list (limit, token, cb) {
token = token ? parseInt(token, 10) : 0;
var connection = getConnection();
connection.query(
'SELECT * FROM `books` LIMIT ? OFFSET ?', [limit, token],
function (err, results) {
if (err) { return cb(err); }
if (err) {
return cb(err);
}
var hasMore = results.length === limit ? token + results.length : false;
cb(null, results, hasMore);
}
Expand All @@ -41,21 +42,25 @@ module.exports = function (config) {
// [END list]

// [START create]
function create(data, cb) {
function create (data, cb) {
var connection = getConnection();
connection.query('INSERT INTO `books` SET ?', data, function (err, res) {
if (err) { return cb(err); }
if (err) {
return cb(err);
}
read(res.insertId, cb);
});
connection.end();
}
// [END create]

function read(id, cb) {
function read (id, cb) {
var connection = getConnection();
connection.query(
'SELECT * FROM `books` WHERE `id` = ?', id, function (err, results) {
if (err) { return cb(err); }
if (err) {
return cb(err);
}
if (!results.length) {
return cb({
code: 404,
Expand All @@ -68,18 +73,20 @@ module.exports = function (config) {
}

// [START update]
function update(id, data, cb) {
function update (id, data, cb) {
var connection = getConnection();
connection.query(
'UPDATE `books` SET ? WHERE `id` = ?', [data, id], function (err) {
if (err) { return cb(err); }
if (err) {
return cb(err);
}
read(id, cb);
});
connection.end();
}
// [END update]

function _delete(id, cb) {
function _delete (id, cb) {
var connection = getConnection();
connection.query('DELETE FROM `books` WHERE `id` = ?', id, cb);
connection.end();
Expand All @@ -95,7 +102,7 @@ module.exports = function (config) {
};
};

if (!module.parent) {
if (module === require.main) {
var prompt = require('prompt');
prompt.start();

Expand All @@ -104,12 +111,14 @@ if (!module.parent) {
'database.\n This script will not modify any existing tables.\n');

prompt.get(['host', 'user', 'password'], function (err, result) {
if (err) { return; }
if (err) {
return;
}
createSchema(result);
});
}

function createSchema(config) {
function createSchema (config) {
var connection = mysql.createConnection(extend({
multipleStatements: true
}, config));
Expand All @@ -129,7 +138,9 @@ function createSchema(config) {
'`createdById` VARCHAR(255) NULL, ' +
'PRIMARY KEY (`id`));',
function (err) {
if (err) { throw err; }
if (err) {
throw err;
}
console.log('Successfully created schema');
connection.end();
}
Expand Down
Loading

0 comments on commit bf56e5d

Please sign in to comment.