diff --git a/Dockerfile b/Dockerfile index d8bfaf8..6c0e88d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM node:6.9-slim -MAINTAINER Roy Meissner +MAINTAINER Paul Baptista RUN mkdir /nodeApp WORKDIR /nodeApp diff --git a/application/configuration.js b/application/configuration.js index 3b602af..1a5f9c9 100644 --- a/application/configuration.js +++ b/application/configuration.js @@ -7,32 +7,32 @@ let host = 'localhost'; //read mongo URL from /etc/hosts const fs = require('fs'); try { - const lines = fs.readFileSync('/etc/hosts').toString().split('\n'); - lines.filter((line) => line.includes('mongodb')).forEach((line) => { - const entries = line.split(' '); - host = entries[entries.length - 1]; - console.log('Using ' + host + ' as database host.'); - }); + const lines = fs.readFileSync('/etc/hosts').toString().split('\n'); + lines.filter((line) => line.includes('mongodb')).forEach((line) => { + const entries = line.split(' '); + host = entries[entries.length - 1]; + console.log('Using ' + host + ' as database host.'); + }); } catch (e) { - console.log('Exception: Windows or no read rights to read /etc/hosts (bad)'); + console.log('Exception: Windows or no read rights to read /etc/hosts (bad)'); } //read mongo URL from ENV host = (!co.isEmpty(process.env.DATABASE_URL)) ? process.env.DATABASE_URL : host; if(host !== 'localhost') - console.log('Using ' + host + ' as database host.'); + console.log('Using ' + host + ' as database host.'); let port = 27017; //read mongo port from ENV if (!co.isEmpty(process.env.DATABASE_PORT)){ - port = process.env.DATABASE_PORT; - console.log('Using ' + port + ' as database port.'); + port = process.env.DATABASE_PORT; + console.log('Using ' + port + ' as database port.'); } module.exports = { - MongoDB: { - PORT: port, - HOST: host, - NS: 'local', - SLIDEWIKIDATABASE: 'slidewiki' - } + MongoDB: { + PORT: port, + HOST: host, + NS: 'local', + SLIDEWIKIDATABASE: 'slidewiki' + } }; diff --git a/application/controllers/handler.js b/application/controllers/handler.js index d78632c..4dbdf9a 100644 --- a/application/controllers/handler.js +++ b/application/controllers/handler.js @@ -36,6 +36,22 @@ module.exports = { }); }, + //Get a question from database or return NOT FOUND + getRelatedQuestions: function(request, reply) { + questionDB.getAllRelated(request.params.related_object, + request.params.related_object_id + ).then((questions) => { + if (co.isEmpty(question)) + reply(boom.notFound()); + else + reply(co.rewriteID(question)); + }).catch((error) => { + request.log('error', error); + reply(boom.badImplementation()); + }); + }, + + //Create Question with new id and payload or return INTERNAL_SERVER_ERROR newQuestion: function(request, reply) { questionDB.insert(request.payload).then((inserted) => { @@ -64,4 +80,17 @@ module.exports = { }); }, + //Create Question with new id and payload or return INTERNAL_SERVER_ERROR + deleteQuestion: function(request, reply) { + questionDB.remove(request.params.id).then((deleted) => { + if (co.isEmpty(deleted)) + reply(co.rewriteID(deleted)); + else + reply(boom.notFound()); + }).catch((error) => { + request.log('error', error); + reply(boom.badImplementation()); + }); + }, + }; diff --git a/application/database/questionDatabase.js b/application/database/questionDatabase.js index 61028af..633b84d 100644 --- a/application/database/questionDatabase.js +++ b/application/database/questionDatabase.js @@ -20,12 +20,29 @@ module.exports = { })); }, + remove: function (identifier) { + return helper.connectToDatabase() + .then((db) => db.collection('questions')) + .then((col) => col.remove({ + _id: identifier + })); + }, + getAll: function (identifier) { return helper.connectToDatabase() .then((db) => db.collection('questions')) .then((col) => col.find()); }, + getAllRelated: function (relObject, relObjectId) { + return helper.connectToDatabase() + .then((db) => db.collection('questions')) + .then((col) => col.find({ + related_object: relObject, + related_object_id: objectId + })); + }, + insert: function (question) { //TODO check for root and parent deck ids to be existent, otherwise create these return helper.connectToDatabase() diff --git a/application/routes.js b/application/routes.js index ddf97db..d6f8c3b 100644 --- a/application/routes.js +++ b/application/routes.js @@ -91,13 +91,13 @@ module.exports = function(server) { //Get all questions of Deck or Slide with its id and return it (...). server.route({ method: 'GET', - path: '/{related_object}/deck/{id}/questions', - handler: handlers.getDeckQuestions, + path: '/{related_object}/{related_object_id}/questions', + handler: handlers.getRelatedQuestions, config: { validate: { params: { - id: Joi.string().alphanum().lowercase() related_object: Joi.string().valid(['slide','deck']), + related_object_id: Joi.string().alphanum(), } }, tags: ['api'], diff --git a/docker-compose.yml b/docker-compose.yml index efd7dad..0ad1286 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,5 @@ -template: - image: slidewiki/microservicetemplate +questionservice: + image: slidewiki/questionservice:latest-dev restart: on-failure:5 expose: - "80" @@ -9,16 +9,7 @@ template: - APPLICATION_PORT=80 - DATABASE_PORT=27017 - DATABASE_URL=mongodb # use a url or the name, defined in the docker-compose file - - VIRTUAL_HOST=microservicetemplate.experimental.slidewiki.org - - LETSENCRYPT_HOST=microservicetemplate.experimental.slidewiki.org - - LETSENCRYPT_EMAIL=meissner@informatik.uni-leipzig.de - - SERVICE_URL_DECK=http://deckservice.experimental.slidewiki.org - - SERVICE_URL_DISCUSSION=http://discussionservice.experimental.slidewiki.org - - SERVICE_URL_ACTIVITIES=http://activitiesservice.experimental.slidewiki.org - - SERVICE_URL_NOTIFICATION=http://notificationservice.experimental.slidewiki.org - - SERVICE_URL_USER=http://userservice.experimental.slidewiki.org - - SERVICE_URL_SEARCH=http://searchservice.experimental.slidewiki.org - - SERVICE_URL_IMAGE=http://imageservice.experimental.slidewiki.org - - SERVICE_URL_FILE=http://fileservice.experimental.slidewiki.org - - SERVICE_URL_PDF=http://pdfservice.experimental.slidewiki.org - - SERVICE_URL_IMPORT=http://importservice.experimental.slidewiki.org + - VIRTUAL_HOST=questionservice.experimental.slidewiki.org + - LETSENCRYPT_HOST=questionservice.experimental.slidewiki.org + - LETSENCRYPT_EMAIL=pbaptist@uni-bonn.de + - LETSENCRYPT_TEST=true diff --git a/mongodb/Dockerfile b/mongodb/Dockerfile index 1c44191..17b26e7 100644 --- a/mongodb/Dockerfile +++ b/mongodb/Dockerfile @@ -1,4 +1,4 @@ -FROM mongo:latest +FROM mongo:3.4 MAINTAINER Kurt Junghanns # ---------------- # diff --git a/travis_scripts/dockerhub.sh b/travis_scripts/dockerhub.sh index 6cd93eb..af380c0 100755 --- a/travis_scripts/dockerhub.sh +++ b/travis_scripts/dockerhub.sh @@ -1,5 +1,5 @@ #!/bin/bash docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" -docker build -t slidewiki/microservicetemplate ./ -docker push slidewiki/microservicetemplate +docker build -t slidewiki/questionservice:latest-dev . +docker push slidewiki/questionservice:latest-dev