Skip to content

Commit

Permalink
Merge pull request #1 from slidewiki/SWIK-706_description_field_matching
Browse files Browse the repository at this point in the history
Swik 706 description field matching
  • Loading branch information
kadevgraaf committed Oct 20, 2016
2 parents 75668f6 + 6368144 commit f309a31
Show file tree
Hide file tree
Showing 18 changed files with 756 additions and 709 deletions.
2 changes: 1 addition & 1 deletion application/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"no-console": 0,
"arrow-parens": [2, "always"],
"eqeqeq": [2, "smart"],
"indent": [2, 2, {SwitchCase: 1}],
"indent": [2, 4, {SwitchCase: 1}],
"quotes": [2, 'single'],
"linebreak-style": [2, "unix"],
"prefer-arrow-callback": 1,
Expand Down
69 changes: 37 additions & 32 deletions application/controllers/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,47 @@ Handles the requests by executing stuff and replying to the client. Uses promise
'use strict';

const boom = require('boom'), //Boom gives us some predefined http codes and proper responses
solrClient = require('../solr/solrClient'),
helper = require('../solr/helper');
solrClient = require('../solr/solrClient'),
helper = require('../solr/helper'),
initIndex = require('../solr/initIndex');

module.exports = {

// Get query results from SOLR or return INTERNAL_SERVER_ERROR
getResults: function(request, reply){
// Get query results from SOLR or return INTERNAL_SERVER_ERROR
getResults: function(request, reply){

// parse query params
helper.parse(request.params.queryparams).then( (queryparams) => {
// fetch results from SOLR
solrClient.get(queryparams).then( (results) => {
reply(results);
}).catch( (error) => {
reply(boom.badImplementation());
});
}).catch( (error) => {
reply(boom.badImplementation());
});
},
// parse query params
helper.parse(request.params.queryparams).then( (queryparams) => {
// fetch results from SOLR
solrClient.get(queryparams).then( (results) => {
reply(results);
}).catch( (error) => {
request.log('solrClient.get', error);
reply(boom.badImplementation());
});
}).catch( (error) => {
request.log('helper.parse', error);
reply(boom.badImplementation());
});
},

// index all objects from DB to SOLR
indexAll: function(){
helper.indexAll().then( (res) => {
reply(res);
}).catch( (err) => {
reply(boom.badImplementation());
});
},
// index all objects from DB to SOLR
indexAll: function(request, reply){
initIndex.indexAll().then( (res) => {
reply(res);
}).catch( (error) => {
request.log('initIndex.indexAll', error);
reply(boom.badImplementation());
});
},

// delete all documents from SOLR
deleteAll: function(){
helper.deleteAll().then( (res) => {
reply(res);
}).catch( (err) => {
reply(boom.badImplementation());
});
}
// delete all documents from SOLR
deleteAll: function(request, reply){
solrClient.deleteAll().then( (res) => {
reply(res);
}).catch( (error) => {
request.log('solrClient.deleteAll', error);
reply(boom.badImplementation());
});
}
};
12 changes: 6 additions & 6 deletions application/database/databaseConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Controller for handling mongodb and the data model slide while providing CRUD'is
const helper = require('./helper');

module.exports = {
getAllFromCollection: function(collectionName) {
return helper.connectToDatabase()
.then((db) => db.collection(collectionName))
.then((col) => col.find())
.then((stream) => stream.toArray());
}
getAllFromCollection: function(collectionName) {
return helper.connectToDatabase()
.then((db) => db.collection(collectionName))
.then((col) => col.find())
.then((stream) => stream.toArray());
}
};
54 changes: 27 additions & 27 deletions application/database/helper.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
'use strict';

const Db = require('mongodb').Db,
Server = require('mongodb').Server,
MongoClient = require('mongodb').MongoClient,
config = require('../configuration.js').mongoConfig,
co = require('../common');
Server = require('mongodb').Server,
MongoClient = require('mongodb').MongoClient,
config = require('../configuration.js').mongoConfig,
co = require('../common');

let dbConnection = undefined;

function testDbName(name) {
return typeof name !== 'undefined' ? name : config.SLIDEWIKIDATABASE;
return typeof name !== 'undefined' ? name : config.SLIDEWIKIDATABASE;
}

function testConnection(dbname) {
if (!co.isEmpty(dbConnection)) { //TODO test for alive
if (dbConnection.s.databaseName === dbname)
return true;
else {
dbConnection.close();
dbConnection = undefined;
return false;
if (!co.isEmpty(dbConnection)) { //TODO test for alive
if (dbConnection.s.databaseName === dbname)
return true;
else {
dbConnection.close();
dbConnection = undefined;
return false;
}
}
}
return false;
return false;
}

module.exports = {
connectToDatabase: function(dbname) {
dbname = testDbName(dbname);
connectToDatabase: function(dbname) {
dbname = testDbName(dbname);

if (testConnection(dbname))
return Promise.resolve(dbConnection);
else
return MongoClient.connect('mongodb://' + config.HOST + ':' + config.PORT + '/' + dbname)
.then((db) => {
if (db.s.databaseName !== dbname)
throw new 'Wrong Database!';
dbConnection = db;
return db;
});
}
if (testConnection(dbname))
return Promise.resolve(dbConnection);
else
return MongoClient.connect('mongodb://' + config.HOST + ':' + config.PORT + '/' + dbname)
.then((db) => {
if (db.s.databaseName !== dbname)
throw new 'Wrong Database!';
dbConnection = db;
return db;
});
}
};
40 changes: 20 additions & 20 deletions application/microservices/microservicesConnection.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
'use strict';

const request = require('request'),
microservicesConf = require('../configuration').microservices;
microservicesConf = require('../configuration').microservices;


module.exports = {

getUsername: function(user_id){
let promise = new Promise( (resolve, reject) => {
request({
uri: microservicesConf.userserviceURI + '/user/' + user_id,
method: 'GET'
}, (err, response, body) => {
getUsername: function(user_id){
let promise = new Promise( (resolve) => {
request({
uri: microservicesConf.userserviceURI + '/user/' + user_id,
method: 'GET'
}, (err, response, body) => {

if(err){
resolve('unknown');
}
else if(response.statusCode !== 200){
resolve('unknown');
}
else{
resolve(JSON.parse(body).username);
}
});
});
return promise;
}
if(err){
resolve('unknown');
}
else if(response.statusCode !== 200){
resolve('unknown');
}
else{
resolve(JSON.parse(body).username);
}
});
});
return promise;
}
};
56 changes: 0 additions & 56 deletions application/mongoListener.js

This file was deleted.

33 changes: 33 additions & 0 deletions application/mongoListeners/decksListener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const MongoStream = require('mongo-trigger'),
//solr = require('./solr/solrClient'),
decks = require('../solr/objectCollections/decks'),
mongoConfig = require('../configuration').mongoConfig;;

module.exports = {
listen: function(){

// init data stream
let decksStream = new MongoStream({
format: 'pretty',
host: mongoConfig.HOST,
port: mongoConfig.PORT,
});

// watch decks collection
let deckCollection = mongoConfig.SLIDEWIKIDATABASE + '.decks';
decksStream.watch(deckCollection, (event) => {
console.log('\ndeck ' + JSON.stringify(event));

switch(event.operation){
case 'insert':
decks.newDeck(event.data);
break;
case 'update':
decks.updateDeck(event);
break;
}
});
}
};
33 changes: 33 additions & 0 deletions application/mongoListeners/slidesListener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const MongoStream = require('mongo-trigger'),
//solr = require('./solr/solrClient'),
slides = require('../solr/objectCollections/slides'),
mongoConfig = require('../configuration').mongoConfig;;

module.exports = {
listen: function(){

// init data stream
let slidesStream = new MongoStream({
format: 'pretty',
host: mongoConfig.HOST,
port: mongoConfig.PORT,
});

// watch slides collection
let slideCollection = mongoConfig.SLIDEWIKIDATABASE + '.slides';
slidesStream.watch(slideCollection, (event) => {
console.log('\nslide ' + JSON.stringify(event));

switch(event.operation){
case 'insert':
slides.newSlide(event.data);
break;
case 'update':
slides.updateSlide(event);
break;
}
});
}
};
3 changes: 2 additions & 1 deletion application/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"vision": "^4.1.0",
"solr-client": "^0.6.0",
"mongo-trigger": "^0.1.5",
"request": "^2.74.0"
"request": "^2.74.0",
"html-to-text": "^2.1.3"
},
"engines": {
"node": ">=6.0.0"
Expand Down
6 changes: 4 additions & 2 deletions application/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ This application demonstrates a service which returns previously inserted data f
//This is our webserver framework (instead of express)
const hapi = require('hapi'),
co = require('./common'),
mongoListener = require('./mongoListener');
decksListener = require('./mongoListeners/decksListener'),
slidesListener = require('./mongoListeners/slidesListener');

//Initiate the webserver with standard or given port
const server = new hapi.Server({ connections: {routes: {validate: { options: {convert : false}}}}});
Expand Down Expand Up @@ -59,7 +60,8 @@ let plugins = [
];

// start listening to mongo changes
mongoListener.listen();
decksListener.listen();
slidesListener.listen();

//Register plugins and start webserver
server.register(plugins, (err) => {
Expand Down
Loading

0 comments on commit f309a31

Please sign in to comment.