Skip to content

Commit

Permalink
✨ Add CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvndre committed Apr 25, 2017
1 parent d0e7f03 commit 7b7fb21
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
29 changes: 16 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 <target>' where <target> 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"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions src/controller/v1/index.js
Original file line number Diff line number Diff line change
@@ -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;
51 changes: 51 additions & 0 deletions src/controller/v1/resource.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions src/validator/resource.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7b7fb21

Please sign in to comment.