Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago de Souza committed Feb 20, 2019
0 parents commit e957dc4
Show file tree
Hide file tree
Showing 15 changed files with 3,182 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
.vscode/

.editorconfig
.eslintrc.json
yarn.lock
9 changes: 9 additions & 0 deletions .editorconfig
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
16 changes: 16 additions & 0 deletions .eslintrc.json
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": {}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.vscode/
10 changes: 10 additions & 0 deletions dockerfile
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"]
23 changes: 23 additions & 0 deletions package.json
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"
}
}
30 changes: 30 additions & 0 deletions src/app.js
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
32 changes: 32 additions & 0 deletions src/app/controllers/AuthorController.js
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()
38 changes: 38 additions & 0 deletions src/app/controllers/BookController.js
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()
24 changes: 24 additions & 0 deletions src/app/models/Author.js
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')
30 changes: 30 additions & 0 deletions src/app/models/Book.js
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')
8 changes: 8 additions & 0 deletions src/config.json
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"
}
}
21 changes: 21 additions & 0 deletions src/router.js
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
7 changes: 7 additions & 0 deletions src/server.js
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}...`)
})
Loading

0 comments on commit e957dc4

Please sign in to comment.