-
Notifications
You must be signed in to change notification settings - Fork 1
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
alexvndre
committed
Apr 25, 2017
1 parent
d0e7f03
commit 7b7fb21
Showing
7 changed files
with
104 additions
and
14 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
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
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
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
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 |
---|---|---|
@@ -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; |
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,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; |
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,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; |