Skip to content

Commit

Permalink
refactoring to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrOndrusek authored and tanja3981 committed May 21, 2019
1 parent 72f02fc commit 5443c5c
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 146 deletions.
21 changes: 14 additions & 7 deletions lib/api3/authBuilder.js → lib/api3/authorizerBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
var moment = require('moment')
, apiConst = require('./const.json')

function configure (app, env, ctx) {
/**
* Builder of authorization middleware functions
*/
function AuthorizerBuilder (app, env, ctx) {

function checkDateHeader (req, res) {
var self = this

self.checkDateHeader = function checkDateHeader (req, res) {

var dateHeader = req.header('Date');
if (!dateHeader) {
Expand All @@ -26,7 +31,11 @@ function configure (app, env, ctx) {
return true;
}

function authBuilder (permission) {
/**
* Create authorization middleware function which demands the permission
* @param {string} permission - permission to demand
*/
self.authorizerFor = function authorizerFor (permission) {

return function authorize (req, res, next) {

Expand All @@ -38,7 +47,7 @@ function configure (app, env, ctx) {
return res.sendJSONStatus(res, apiConst.HTTP.FORBIDDEN, apiConst.MSG.HTTP_403_NOT_USING_HTTPS);
}

if (checkDateHeader(req, res) !== true) {
if (self.checkDateHeader(req, res) !== true) {
return;
}

Expand All @@ -62,8 +71,6 @@ function configure (app, env, ctx) {
});
}
}

return authBuilder;
}

module.exports = configure;
module.exports = AuthorizerBuilder;
70 changes: 0 additions & 70 deletions lib/api3/colBuilder.js

This file was deleted.

69 changes: 69 additions & 0 deletions lib/api3/collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

/**
* Generic collection (abstraction over each collection specifics)
* @param {string} colName - name of the collection inside the storage system
*/
function Collection (ctx, env, app, authBuilder, colName, fallbackGetDate) {
var self = this
, CollectionStorage = require('./storage/mongoCollection')
, SearchOperation = require('./operation/search')
, CreateOperation = require('./operation/create')
, ReadOperation = require('./operation/read')
, UpdateOperation = require('./operation/update')
, PatchOperation = require('./operation/patch')
, DeleteOperation = require('./operation/delete')
, HistoryOperation = require('./operation/history')

self.colName = colName;
self.authBuilder = authBuilder;
self.fallbackGetDate = fallbackGetDate;
self.storage = new CollectionStorage(ctx, env, colName);

self.opSearch = new SearchOperation(ctx, env, app, self);
self.opCreate = new CreateOperation(ctx, env, app, self);
self.opRead = new ReadOperation(ctx, env, app, self);
self.opUpdate = new UpdateOperation(ctx, env, app, self);
self.opPatch = new PatchOperation(ctx, env, app, self);
self.opDelete = new DeleteOperation(ctx, env, app, self);
self.opHistory = new HistoryOperation(ctx, env, app, self);

self.mapRoutes = function mapRoutes () {
var prefix = '/' + colName;
var prefixId = prefix + '/:identifier';

// GET /{collection}
app.get(prefix, self.opSearch.operation);

// POST /{collection}
app.post(prefix, self.opCreate.operation);

// GET /{collection}/history
app.get(prefixId, self.opHistory.operation);

// GET /{collection}/{identifier}
app.get(prefixId, self.opRead.operation);

// PUT /{collection}/{identifier}
app.put(prefixId, self.opUpdate.operation);

// PATCH /{collection}/{identifier}
app.patch(prefixId, self.opPatch.operation);

// DELETE /{collection}/{identifier}
app.delete(prefixId, self.opDelete.operation);
}


/**
* Floor date to whole seconds (cut off milliseconds)
* @param {Date} date
*/
self.floorSeconds = function floorSeconds (date) {
var ms = date.getTime();
ms -= ms % 1000;
return new Date(ms);
}
}

module.exports = Collection;
12 changes: 6 additions & 6 deletions lib/api3/operation/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* CREATE: Inserts a new document into the collection
*/
function configure (ctx, env, app, col) {
function CreateOperation (ctx, env, app, col) {

var obj = { }
var self = this

obj.operation = function operation (req, res, next) {
self.col = col;

self.operation = function operation (req, res, next) {
next();
}

return obj;
}

module.exports = configure;
module.exports = CreateOperation;
14 changes: 6 additions & 8 deletions lib/api3/operation/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/**
* DELETE: Deletes a document from the collection
*/
function configure (ctx, env, app, col) {
function DeleteOperation (ctx, env, app, col) {

var obj = { }
, authBuilder = require('../authBuilder')(app, env, ctx)
var self = this
, apiConst = require('../const.json')

self.col = col;

obj.operation = function operation (req, res) {
self.operation = function operation (req, res) {

function deletePermanently () {
col.storage.deleteOne(req.params.identifier, function deleteOneDone (err, result) {
Expand Down Expand Up @@ -46,7 +46,7 @@ function configure (ctx, env, app, col) {
}


authBuilder('api:' + col.colName + ':delete')(req, res, function() {
col.authBuilder.authorizerFor('api:' + col.colName + ':delete')(req, res, function() {

if (req.query.permanent && req.query.permanent === "true") {
deletePermanently();
Expand All @@ -56,8 +56,6 @@ function configure (ctx, env, app, col) {
}
});
}

return obj;
}

module.exports = configure;
module.exports = DeleteOperation;
12 changes: 6 additions & 6 deletions lib/api3/operation/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* HISTORY: Retrieves incremental changes since timestamp
*/
function configure (ctx, env, app, col) {
function HistoryOperation (ctx, env, app, col) {

var obj = { }
var self = this

obj.operation = function operation (req, res, next) {
self.col = col;

self.operation = function operation (req, res, next) {
next();
}

return obj;
}

module.exports = configure;
module.exports = HistoryOperation;
12 changes: 6 additions & 6 deletions lib/api3/operation/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* PATCH: Partially updates document in the collection
*/
function configure (ctx, env, app, col) {
function PatchOperation (ctx, env, app, col) {

var obj = { }
var self = this

obj.operation = function operation (req, res, next) {
self.col = col;

self.operation = function operation (req, res, next) {
next();
}

return obj;
}

module.exports = configure;
module.exports = PatchOperation;
18 changes: 8 additions & 10 deletions lib/api3/operation/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
/**
* READ: Retrieves a single document from the collection
*/
function configure (ctx, env, app, col) {
function ReadOperation (ctx, env, app, col) {

var obj = { }
, authBuilder = require('../authBuilder')(app, env, ctx)
var self = this
, apiConst = require('../const.json')
, FieldsProjector = require('../fieldsProjector')

self.col = col;

/**
* Fetches modified date from document (with possible fallback and back-fill to srvModified/srvCreated)
* @param {any} doc - document loaded from database
*/
obj.resolveDates = function resolveDates (doc, fallbackGetDate) {
self.resolveDates = function resolveDates (doc, fallbackGetDate) {
var modifiedDate;
try
{
Expand All @@ -41,7 +41,7 @@ function configure (ctx, env, app, col) {
}


obj.operation = function operation (req, res) {
self.operation = function operation (req, res) {

function loadFromDb () {
var fieldsProjector = new FieldsProjector(req.query.fields);
Expand All @@ -65,7 +65,7 @@ function configure (ctx, env, app, col) {
}


var modifiedDate = obj.resolveDates(doc, col.fallbackGetDate);
var modifiedDate = self.resolveDates(doc, col.fallbackGetDate);
if (modifiedDate) {
res.setHeader('Last-Modified', modifiedDate.toUTCString())

Expand All @@ -83,12 +83,10 @@ function configure (ctx, env, app, col) {
});
}

authBuilder('api:' + col.colName + ':read')(req, res, function() {
col.authBuilder.authorizerFor('api:' + col.colName + ':read')(req, res, function() {
loadFromDb();
});
}

return obj;
}

module.exports = configure;
module.exports = ReadOperation;
12 changes: 6 additions & 6 deletions lib/api3/operation/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* SEARCH: Search documents from the collection
*/
function configure (ctx, env, app, col) {
function SearchOperation (ctx, env, app, col) {

var obj = { }
var self = this

obj.operation = function operation (req, res, next) {
self.col = col;

self.operation = function operation (req, res, next) {
next();
}

return obj;
}

module.exports = configure;
module.exports = SearchOperation;
Loading

0 comments on commit 5443c5c

Please sign in to comment.