diff --git a/src/js/app.js b/src/js/app.js index f74b60d..ac90af5 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -1,49 +1,13 @@ const express = require('express'); -const ObjectId = require('mongodb').ObjectId; const bodyParser = require('body-parser'); +const applicationController = require('./controller/applicationCtrl'); -const MongoClient = require('./mongoClient'); -const Repository = require('./repository'); +const port = 3000; const app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); -const port = 3000; -const mongoClient = new MongoClient(); -const applicationRepository = new Repository('applications', mongoClient); - -app.get('/apps', (request, response) => { - applicationRepository.find({}, results => { - response.json(results); - }); -}); - -app.post('/apps', (request, response) => { - applicationRepository.insert(request.body, (result) => { - response.json(result); - }); -}); - -app.put('/apps/:applicationId', (request, response) => { - const applicationId = ObjectId(request.params.applicationId); - applicationRepository.find({_id: applicationId}, entity => { - if (entity.length === 0) { - response.status(404).send(); - } else { - const applicationToUpdate = request.body; - applicationToUpdate._id = applicationId; - applicationRepository.update(applicationToUpdate, () => { - response.status(204).send(); - }); - } - }); -}); - -app.delete('/apps/:applicationId', (request, response) => { - applicationRepository.delete(request.params.applicationId, () => { - response.status(200).send('Deleted'); - }); -}); +app.use('/apps', applicationController); app.listen(port, () => console.log('Mock is listening at port ', port, '\n')); \ No newline at end of file diff --git a/src/js/configuration.js b/src/js/configuration.js new file mode 100644 index 0000000..0c9c75d --- /dev/null +++ b/src/js/configuration.js @@ -0,0 +1,8 @@ +module.exports = { + database: { + url: 'mongodb://localhost:27017', + username: 'express-user', + password: 'P@ssword1', + database: 'express-test' + } +}; \ No newline at end of file diff --git a/src/js/controller/applicationCtrl.js b/src/js/controller/applicationCtrl.js new file mode 100644 index 0000000..92e3f2c --- /dev/null +++ b/src/js/controller/applicationCtrl.js @@ -0,0 +1,47 @@ +const ObjectId = require('mongodb').ObjectId; +const Repository = require('../repository/repository'); +const router = require('express').Router(); + +const applicationRepository = new Repository('applications'); + +router.get('/', (request, response) => { + applicationRepository.find({}, results => { + response.json(results); + }); +}); + +router.post('/', (request, response) => { + applicationRepository.insert(request.body, (result) => { + response.json(result); + }); +}); + +router.put('/:applicationId', (request, response) => { + const applicationId = ObjectId(request.params.applicationId); + applicationRepository.find({_id: applicationId}, entity => { + if (entity.length === 0) { + response.status(404).send(); + } else { + const applicationToUpdate = request.body; + applicationToUpdate._id = applicationId; + applicationRepository.update(applicationToUpdate, () => { + response.status(204).send(); + }); + } + }); +}); + +router.delete('/:applicationId', (request, response) => { + const applicationId = ObjectId(request.params.applicationId); + applicationRepository.find({_id: applicationId}, entity => { + if (entity.length === 0) { + response.status(404).send(); + } else { + applicationRepository.delete(applicationId, () => { + response.status(204).send(); + }); + } + }); +}); + +module.exports = router; \ No newline at end of file diff --git a/src/js/mongoClient.js b/src/js/repository/mongoClient.js similarity index 77% rename from src/js/mongoClient.js rename to src/js/repository/mongoClient.js index 93cdd62..c043964 100644 --- a/src/js/mongoClient.js +++ b/src/js/repository/mongoClient.js @@ -1,29 +1,25 @@ const mongodb = require('mongodb'); const assert = require('assert'); -const mongoConfig = { - url: 'mongodb://localhost:27017', - username: 'express-user', - password: 'P@ssword1', - database: 'express-test' -}; +const configuration = require('../configuration'); +const mongoConfig = configuration.database; class MongoClient { constructor() { - mongodb.MongoClient.connect(mongoConfig.url, (err, client) => { - assert.equal(null, err); + mongodb.MongoClient.connect(mongoConfig.url, (error, client) => { + assert.equal(null, error, `Unable to connect to mongodb: ${error}.`); console.log('Connected successfuly to mongodb'); this.db = client.db(mongoConfig.database); }); } - find(collectionName, query, successCallback, errorCallback) { + find(collectionName, query, callback) { this.db.collection(collectionName).find(query).toArray() .then(results => { console.log(`Entities ${collectionName} founded.`); - successCallback(results); + callback(results); }) - .catch(errorCallback); + .catch(error => console.error(error)); } insert(collectionName, entity, callback) { @@ -52,4 +48,5 @@ class MongoClient { } } -module.exports = MongoClient; \ No newline at end of file +const mongoClient = new MongoClient(); +module.exports = mongoClient; \ No newline at end of file diff --git a/src/js/repository.js b/src/js/repository/repository.js similarity index 65% rename from src/js/repository.js rename to src/js/repository/repository.js index 0ced10e..275df5d 100644 --- a/src/js/repository.js +++ b/src/js/repository/repository.js @@ -1,11 +1,13 @@ -module.exports = class Repository { - constructor(collectionName, mongoClient) { +const mongoClient = require('./mongoClient'); + +class Repository { + constructor(collectionName) { this.collectionName = collectionName; this.mongoClient = mongoClient; } - find(query, successCallback, errorCallback) { - this.mongoClient.find(this.collectionName, query, successCallback, errorCallback); + find(query, callback) { + this.mongoClient.find(this.collectionName, query, callback); } insert(entity, callback) { @@ -19,4 +21,6 @@ module.exports = class Repository { delete(entityId, callback) { this.mongoClient.delete(this.collectionName, entityId, callback); } -}; \ No newline at end of file +}; + +module.exports = Repository; \ No newline at end of file