From 7b7fb21b7e7751bedb6ff4c670d9efed6ea965df Mon Sep 17 00:00:00 2001 From: alexvndre Date: Tue, 25 Apr 2017 15:16:38 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20CRUD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 +++++ Makefile | 29 +++++++++++--------- package.json | 3 ++- src/app.js | 9 +++++++ src/controller/v1/index.js | 2 ++ src/controller/v1/resource.js | 51 +++++++++++++++++++++++++++++++++++ src/validator/resource.js | 18 +++++++++++++ 7 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 src/controller/v1/resource.js create mode 100644 src/validator/resource.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f7a1a..55da49f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.0.2] - 2017-04-25 +### Added +- CRUD +### Changed +- Import practices in Makefile. + ## [0.0.1] - 2017-04-14 ### Added - Init the project. diff --git a/Makefile b/Makefile index e421ee4..578dcd5 100644 --- a/Makefile +++ b/Makefile @@ -4,38 +4,41 @@ BUILD_DIRECTORY=./build ESLINT=./node_modules/.bin/eslint PORT=3000 -.PHONY: build help lint install start +.PHONY: build clean help lint install start -build: +.DEFAULT_GOAL := help + +build: ## transpile the files from ES6 to JS @$(MAKE) -s lint @echo " > Building the project in $(BUILD_DIRECTORY)" - @rm -rf $(BUILD_DIRECTORY)/* + @$(MAKE) -s clean @$(BABEL) -q ./src -d $(BUILD_DIRECTORY) -help: +clean: ## clean artifacts + @echo " > Cleaning $(BUILD_DIRECTORY)" + @rm -rf $(BUILD_DIRECTORY) + +help: ## provide help to you @echo "Please use \`make ' where is one of" - @echo " build transpile the files from ES6 to JS" - @echo " lint lint the code" - @echo " install install dependencies" - @echo " start start the web server" + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-30s\033[0m %s\n", $$1, $$2}' -lint: +lint: ## check the quality code and ES6 integration @echo " > Linting the source" @$(ESLINT) ./src -install: +install: ## install dependencies @echo " > Installing the project" @npm install -start: +start: ## start the web server @echo " > Starting the project" @$(MAKE) -s build @export PORT=$(PORT) && export NODE_ENV=local && node $(BUILD_DIRECTORY)/index.js -test: +test: ## launch tests @echo " > Testing" @echo "No test available" -test-coverage: +test-coverage: ## launch tests with coverage @echo " > Testing with coverage" @echo "No test available" diff --git a/package.json b/package.json index ebcac6c..55b890a 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "dependencies": { "body-parser": "^1.17.1", "cluster": "^0.7.7", - "express": "^4.15.2" + "express": "^4.15.2", + "express-validator": "^3.2.0" }, "devDependencies": { "babel-cli": "^6.24.0", diff --git a/src/app.js b/src/app.js index f5e4c07..f95c189 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,6 @@ import bodyParser from 'body-parser'; import express from 'express'; +import expressValidator from 'express-validator'; import config from './config/index'; import controllers from './controller/index'; import logger from './helper/logger'; @@ -9,6 +10,14 @@ const app = express(); app.use(bodyParser.json()); // To support URL-encoded bodies app.use(bodyParser.urlencoded({ extended: true })); +// Body validator +app.use(expressValidator({ + errorFormatter: (param, msg) => ({ + code: 400, + message: msg, + }), +})); +// Allow cross domain requests app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'PUT, GET, PATCH, POST, DELETE, OPTIONS'); diff --git a/src/controller/v1/index.js b/src/controller/v1/index.js index d3ceea8..e15af5b 100644 --- a/src/controller/v1/index.js +++ b/src/controller/v1/index.js @@ -1,8 +1,10 @@ import express from 'express'; import hello from './hello'; +import resource from './resource'; const router = express.Router(); router.use('/hello', hello); +router.use('/resources', resource); export default router; diff --git a/src/controller/v1/resource.js b/src/controller/v1/resource.js new file mode 100644 index 0000000..7674b23 --- /dev/null +++ b/src/controller/v1/resource.js @@ -0,0 +1,51 @@ +import express from 'express'; +import validator from './../../validator/resource'; + +const router = express.Router(); + +/** + * ALL /v1/resources + */ +router.route('/') + // GET /v1/resources + .get((req, res) => { + res.status(200).json([ + { + id: 1, + name: 'Test 1', + }, + { + id: 2, + name: 'Test 2', + }, + ]); + }) + // POST /v1/resources + .post(validator.checkBody, (req, res) => { + res.status(201).json({ + id: 1, + name: req.body.name, + }); + }); + +router.route('/:id') + // GET /v1/resources/:id + .get((req, res) => { + res.status(200).json({ + id: req.params.id, + name: 'Test', + }); + }) + // PUT /v1/resources/:id + .put(validator.checkBody, (req, res) => { + res.status(200).json({ + id: req.params.id, + name: req.body.name, + }); + }) + // DELETE /v1/resources/:id + .delete((req, res) => { + res.status(204).json(); + }); + +export default router; diff --git a/src/validator/resource.js b/src/validator/resource.js new file mode 100644 index 0000000..89eaa71 --- /dev/null +++ b/src/validator/resource.js @@ -0,0 +1,18 @@ +class Resource { + static checkBody(req, res, next) { + // Body's rules + req.checkBody('name', 'The field name is required.').notEmpty(); + + req.getValidationResult() + .then((result) => { + if (!result.isEmpty()) { + res.status(400).json(result.array()); + } else { + // Continue your job, all is good + next(); + } + }); + } +} + +export default Resource;