Last active
August 28, 2019 04:14
-
-
Save ptgamr/9e649a2fb59a14b3251cc2f9466fad02 to your computer and use it in GitHub Desktop.
Ember training gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const faker = require('faker'); | |
const db = { | |
comments: [], | |
users: [] | |
} | |
db.users = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(id => { | |
return { | |
id, | |
first_name: faker.name.firstName(), | |
last_name: faker.name.lastName(), | |
email: faker.internet.email(), | |
} | |
}); | |
db.comments.push(createComment()) | |
db.comments.push(createComment()) | |
db.comments.push(createReply(1)) | |
db.comments.push(createReply(3)) | |
db.comments.push(createReply(1)) | |
db.insertComment = function(comment) { | |
const newComment = Object.assign( | |
comment, | |
{id: _getNextId(db.comments)} | |
) | |
db.comments.push(newComment) | |
return newComment | |
} | |
db.createUser = function(userPayload) { | |
return { | |
id: _getNextId(db.users), | |
first_name: userPayload.first_name, | |
last_name: userPayload.last_name, | |
email: userPayload.email, | |
} | |
} | |
db.updateUser = function(userPayload) { | |
const id = userPayload.id | |
const idx = db.users.findIndex(user => user.id === id) | |
if (idx < 0) { | |
throw new Error('user not found!') | |
} | |
db.users[idx] = userPayload | |
} | |
function createComment() { | |
return { | |
id: _getNextId(db.comments), | |
content: faker.lorem.paragraph(), | |
author: db.users[faker.random.number(db.users.length - 1)], | |
} | |
} | |
function createReply(parentId) { | |
return { | |
id: _getNextId(db.comments), | |
content: faker.lorem.sentence(), | |
author: db.users[faker.random.number(db.users.length - 1)], | |
parentComment: parentId, | |
} | |
} | |
// get the next ID for a collection | |
function _getNextId(collection) { | |
const count = collection.length | |
if (!count) { | |
return 1 | |
} | |
const allIds = collection.map(item => item.id) | |
const maxId = Math.max(...allIds) | |
return maxId + 1 | |
} | |
module.exports = db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = function(app) { | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const db = require('../db'); | |
const jsonParser = bodyParser.json() | |
let usersRouter = express.Router(); | |
usersRouter.get('/', function(req, res) { | |
res.send({ | |
'users': db.users | |
}); | |
}); | |
usersRouter.post('/', jsonParser, function(req, res) { | |
const user = db.createUser(req.body.user) | |
res.status(201).json({ | |
users: user | |
}); | |
}); | |
usersRouter.get('/:id', function(req, res) { | |
const user = db.users.find(user => user.id === +req.params.id) | |
res.json({ | |
users: user | |
}); | |
}); | |
usersRouter.put('/:id', function(req, res) { | |
const user = db.updateUser(req.body.user) | |
res.status(201).json({ | |
users: user | |
}); | |
}); | |
usersRouter.delete('/:id', function(req, res) { | |
res.status(204).end(); | |
}); | |
// The POST and PUT call will not contain a request body | |
// because the body-parser is not included by default. | |
// To use req.body, run: | |
// npm install --save-dev body-parser | |
// After installing, you need to `use` the body-parser for | |
// this mock uncommenting the following line: | |
// | |
//app.use('/api/users', require('body-parser').json()); | |
app.use('/api/users', usersRouter); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = function(app) { | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const db = require('../db'); | |
const jsonParser = bodyParser.json() | |
let commentsRouter = express.Router(); | |
commentsRouter.get('/', function(req, res) { | |
res.send({ | |
'comments': db.comments | |
}); | |
}); | |
commentsRouter.post('/', jsonParser, function(req, res) { | |
const newComment = db.insertComment(req.body.comment) | |
res.status(201).json({ | |
comments: newComment | |
}); | |
}); | |
commentsRouter.get('/:id', function(req, res) { | |
res.send({ | |
'comments': { | |
id: req.params.id | |
} | |
}); | |
}); | |
commentsRouter.put('/:id', function(req, res) { | |
res.send({ | |
'comments': { | |
id: req.params.id | |
} | |
}); | |
}); | |
commentsRouter.delete('/:id', function(req, res) { | |
res.status(204).end(); | |
}); | |
// The POST and PUT call will not contain a request body | |
// because the body-parser is not included by default. | |
// To use req.body, run: | |
// npm install --save-dev body-parser | |
// After installing, you need to `use` the body-parser for | |
// this mock uncommenting the following line: | |
// | |
//app.use('/api/comments', require('body-parser').json()); | |
app.use('/api/comments', commentsRouter); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment