-
Notifications
You must be signed in to change notification settings - Fork 12
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
0 parents
commit 3a68742
Showing
17 changed files
with
2,586 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,15 @@ | ||
{ | ||
"extends": "airbnb-base", | ||
"rules": { | ||
// windows linebreaks when not in production environment | ||
"linebreak-style": ["error", "windows"], | ||
"indent": 0, | ||
"no-underscore-dangle": 0, | ||
"class-methods-use-this": 0, | ||
"consistent-return": 0, | ||
"no-cond-assign": 0, | ||
"no-param-reassign": 0, | ||
"object-curly-newline": 0, | ||
"eqeqeq": 0 | ||
} | ||
} |
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,4 @@ | ||
node_modules | ||
*.log | ||
.idea | ||
*.log.* |
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,25 @@ | ||
const bodyParser = require('body-parser'); | ||
const config = require('config'); | ||
const express = require('express'); | ||
const Router = require('./routing/router'); | ||
|
||
class API { | ||
constructor() { | ||
this.app = express(); | ||
this.router = new Router(this.app); | ||
|
||
this.port = config.get('api.port'); | ||
this.host = config.get('api.host'); | ||
} | ||
|
||
start() { | ||
this.app.use(bodyParser.urlencoded({ extended: true })); | ||
this.app.use(bodyParser.json()); | ||
this.router.registerRoutes(); | ||
this.app.listen(this.port, this.host); | ||
|
||
console.log(`RESTful API server started on: ${this.port}`); | ||
} | ||
} | ||
|
||
module.exports = API; |
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 @@ | ||
{ | ||
"debug": true, | ||
"api": { | ||
"host": "", | ||
"port": 3000, | ||
"security": { | ||
"jwtSecret": "secret" | ||
} | ||
} | ||
} |
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,59 @@ | ||
const halson = require('halson'); | ||
const ControllerBase = require('./controllerBase'); | ||
|
||
class BooksListController extends ControllerBase { | ||
async getBook() { | ||
try { | ||
const { id } = this.params; | ||
|
||
const resource = halson({ id }); | ||
|
||
const removeURI = await this.uriGenerator.getURI( | ||
this.controllers.BooksListController.removeBook, | ||
{ id }, | ||
); | ||
if (removeURI) { | ||
resource.addLink(removeURI.id, removeURI); | ||
} | ||
|
||
const rateURI = await this.uriGenerator.getURI( | ||
this.controllers.BooksListController.rateBook, | ||
{ id }, | ||
); | ||
if (rateURI) { | ||
resource.addLink(rateURI.id, rateURI); | ||
} | ||
|
||
this.ok(resource); | ||
} catch (err) { | ||
this.error(err); | ||
} | ||
} | ||
|
||
async rateBook() { | ||
try { | ||
const { id } = this.params; | ||
const { rating } = this.body; | ||
|
||
const resource = halson({ id, rating }); | ||
|
||
this.ok(resource); | ||
} catch (err) { | ||
this.error(err); | ||
} | ||
} | ||
|
||
async removeBook() { | ||
try { | ||
const { id } = this.params; | ||
|
||
const resource = halson({ id }); | ||
|
||
this.ok(resource); | ||
} catch (err) { | ||
this.error(err); | ||
} | ||
} | ||
} | ||
|
||
module.exports = BooksListController; |
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,35 @@ | ||
class ControllerBase { | ||
constructor({ uriGenerator, controllers, req, res }) { | ||
this.uriGenerator = uriGenerator; | ||
this.controllers = controllers; | ||
this.req = req; | ||
this.res = res; | ||
this.params = this.req.params; | ||
this.query = this.req.query; | ||
this.body = this.req.body; | ||
} | ||
|
||
error(err) { | ||
const status = err.statusCode || err.status; | ||
const statusCode = status || 500; | ||
this.res.status(statusCode).send(err); | ||
} | ||
|
||
created(location, data) { | ||
if (location) { | ||
this.res.location(location); | ||
} | ||
|
||
this.res.status(201).send(data); | ||
} | ||
|
||
ok(data) { | ||
this.res.status(200).send(data); | ||
} | ||
|
||
noContent() { | ||
this.res.status(204).send(); | ||
} | ||
} | ||
|
||
module.exports = ControllerBase; |
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,39 @@ | ||
const halson = require('halson'); | ||
const ControllerBase = require('./controllerBase'); | ||
|
||
class IndexController extends ControllerBase { | ||
async index() { | ||
const getBookURI = this.uriGenerator.getURI( | ||
this.controllers.BooksListController.getBook, | ||
); | ||
|
||
const rateBookURI = this.uriGenerator.getURI( | ||
this.controllers.BooksListController.rateBook, | ||
); | ||
|
||
const removeBookURI = this.uriGenerator.getURI( | ||
this.controllers.BooksListController.removeBook, | ||
); | ||
|
||
const resource = halson({ api: 'api v1' }); | ||
try { | ||
const links = await Promise.all([ | ||
getBookURI, | ||
rateBookURI, | ||
removeBookURI, | ||
]); | ||
|
||
links.forEach((link) => { | ||
if (link) { | ||
resource.addLink(link.id, link); | ||
} | ||
}); | ||
|
||
super.ok(resource); | ||
} catch (err) { | ||
super.error(err); | ||
} | ||
} | ||
} | ||
|
||
module.exports = IndexController; |
Oops, something went wrong.