Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
msevera committed Aug 11, 2018
0 parents commit 3a68742
Show file tree
Hide file tree
Showing 17 changed files with 2,586 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .eslintrc
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
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
*.log
.idea
*.log.*
25 changes: 25 additions & 0 deletions api.js
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;
10 changes: 10 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"debug": true,
"api": {
"host": "",
"port": 3000,
"security": {
"jwtSecret": "secret"
}
}
}
59 changes: 59 additions & 0 deletions controllers/booksListController.js
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;
35 changes: 35 additions & 0 deletions controllers/controllerBase.js
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;
39 changes: 39 additions & 0 deletions controllers/indexController.js
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;
Loading

0 comments on commit 3a68742

Please sign in to comment.