-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thiago de Souza
committed
Feb 20, 2019
0 parents
commit e957dc4
Showing
15 changed files
with
3,182 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules/ | ||
.vscode/ | ||
|
||
.editorconfig | ||
.eslintrc.json | ||
yarn.lock |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_size = 2 | ||
indent_style = space | ||
trim_trailing_whitespace = true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"env": { | ||
"commonjs": true, | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "standard", | ||
"globals": { | ||
"Atomics": "readable", | ||
"SharedArrayBuffer": "readable" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"rules": {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
.vscode/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM node | ||
|
||
WORKDIR /app | ||
COPY package*.json ./ | ||
|
||
RUN npm install --production --remove-dev | ||
|
||
COPY . . | ||
|
||
CMD ["npm", "start:prod"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "node-books-api", | ||
"version": "1.0.0", | ||
"main": "src/server.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "nodemon src/server.js", | ||
"start:prod": "node src/server.js" | ||
}, | ||
"dependencies": { | ||
"express": "^4.16.4", | ||
"mongoose": "^5.4.13" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^5.14.1", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"nodemon": "^1.18.10" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const express = require('express') | ||
const mongoose = require('mongoose') | ||
|
||
const config = require('./config.json') | ||
|
||
class App { | ||
constructor () { | ||
this.environment = process.env.NODE_ENV || 'development' | ||
this.express = express() | ||
this.configureDbConnection() | ||
this.middlewares() | ||
this.routes() | ||
} | ||
|
||
middlewares () { | ||
this.express.use(express.json()) | ||
} | ||
|
||
routes () { | ||
this.express.use(require('./router')) | ||
} | ||
|
||
configureDbConnection () { | ||
mongoose.connect(config[this.environment].connectionString, { | ||
useNewUrlParser: true | ||
}) | ||
} | ||
} | ||
|
||
module.exports = new App().express |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const AuthorModel = require('../models/Author') | ||
|
||
class AuthorController { | ||
async select (req, res) { | ||
const result = await AuthorModel.find({}) | ||
res.status(200).json(result) | ||
} | ||
|
||
async selectOne (req, res) { | ||
const result = await AuthorModel.findOne({ _id: req.params.id }) | ||
|
||
if (result === null) return res.sendStatus(404) | ||
return res.status(200).json(result) | ||
} | ||
|
||
async create (req, res) { | ||
const result = await AuthorModel.create(req.body) | ||
res.status(201).json(result) | ||
} | ||
|
||
async update (req, res) { | ||
await AuthorModel.updateOne({ _id: req.params.id }, req.body) | ||
res.sendStatus(204) | ||
} | ||
|
||
async delete (req, res) { | ||
await AuthorModel.deleteOne({ _id: req.params.id }) | ||
res.sendStatus(204) | ||
} | ||
} | ||
|
||
module.exports = new AuthorController() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const BookModel = require('../models/Book') | ||
|
||
class AuthorController { | ||
async select (req, res) { | ||
const result = await BookModel.find({}).populate({ | ||
path: 'author', | ||
model: 'author' | ||
}) | ||
res.status(200).json(result) | ||
} | ||
|
||
async selectOne (req, res) { | ||
const result = await BookModel.findOne({ _id: req.params.id }).populate({ | ||
path: 'author', | ||
model: 'author' | ||
}) | ||
|
||
if (result === null) return res.sendStatus(404) | ||
return res.status(200).json(result) | ||
} | ||
|
||
async create (req, res) { | ||
const result = await BookModel.create(req.body) | ||
res.status(201).json(result) | ||
} | ||
|
||
async update (req, res) { | ||
await BookModel.updateOne({ _id: req.params.id }, req.body) | ||
res.sendStatus(204) | ||
} | ||
|
||
async delete (req, res) { | ||
await BookModel.deleteOne({ _id: req.params.id }) | ||
res.sendStatus(204) | ||
} | ||
} | ||
|
||
module.exports = new AuthorController() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const AuthorSchema = new mongoose.Schema( | ||
{ | ||
name: { type: String, required: [true, 'The name must be informed'] }, | ||
createAt: { type: Date, required: true, default: Date.now } | ||
}, | ||
{ | ||
versionKey: false | ||
} | ||
) | ||
|
||
AuthorSchema.virtual('id').get(function () { | ||
return this._id.toHexString() | ||
}) | ||
|
||
AuthorSchema.set('toJSON', { | ||
virtuals: true, | ||
transform: function (doc, ret) { | ||
delete ret._id | ||
} | ||
}) | ||
|
||
module.exports = mongoose.model('author', AuthorSchema, 'authors') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const BookSchema = new mongoose.Schema( | ||
{ | ||
name: { type: String, required: [true, 'The name must be informed'] }, | ||
year: { type: Number, required: [true, 'The year must be informed'] }, | ||
author: { | ||
type: mongoose.Types.ObjectId, | ||
ref: 'author', | ||
required: [true, 'The author must be informed'] | ||
}, | ||
createAt: { type: Date, required: true, default: Date.now } | ||
}, | ||
{ | ||
versionKey: false | ||
} | ||
) | ||
|
||
BookSchema.virtual('id').get(function () { | ||
return this._id.toHexString() | ||
}) | ||
|
||
BookSchema.set('toJSON', { | ||
virtuals: true, | ||
transform: function (doc, ret) { | ||
delete ret._id | ||
} | ||
}) | ||
|
||
module.exports = mongoose.model('book', BookSchema, 'books') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"development": { | ||
"connectionString": "mongodb://<user>:<pass>@<server>.mlab.com:<port>/books-db" | ||
}, | ||
"production": { | ||
"connectionString": "mongodb://<user>:<pass>@<server>.mlab.com:<port>/books-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { Router } = require('express') | ||
const routes = Router() | ||
|
||
const AuthorController = require('./app/controllers/AuthorController') | ||
const BookController = require('./app/controllers/BookController') | ||
|
||
// authors | ||
routes.get('/authors', AuthorController.select) | ||
routes.get('/authors/:id', AuthorController.selectOne) | ||
routes.post('/authors', AuthorController.create) | ||
routes.put('/authors/:id', AuthorController.update) | ||
routes.delete('/authors/:id', AuthorController.delete) | ||
|
||
// books | ||
routes.get('/books', BookController.select) | ||
routes.get('/books/:id', BookController.selectOne) | ||
routes.post('/books', BookController.create) | ||
routes.put('/books/:id', BookController.update) | ||
routes.delete('/books/:id', BookController.delete) | ||
|
||
module.exports = routes |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const app = require('./app') | ||
|
||
const PORT = process.env.PORT || 3000 | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}...`) | ||
}) |
Oops, something went wrong.