diff --git a/src/js/controller/applicationCtrl.js b/src/js/controller/applicationCtrl.js index 92e3f2c..d366b1c 100644 --- a/src/js/controller/applicationCtrl.js +++ b/src/js/controller/applicationCtrl.js @@ -1,4 +1,3 @@ -const ObjectId = require('mongodb').ObjectId; const Repository = require('../repository/repository'); const router = require('express').Router(); @@ -10,6 +9,12 @@ router.get('/', (request, response) => { }); }); +router.get('/:applicationId', (request, response) => { + applicationRepository.find({_id: request.params.applicationId}, (result) => { + response.json(result[0]); + }); +}); + router.post('/', (request, response) => { applicationRepository.insert(request.body, (result) => { response.json(result); @@ -17,7 +22,7 @@ router.post('/', (request, response) => { }); router.put('/:applicationId', (request, response) => { - const applicationId = ObjectId(request.params.applicationId); + const applicationId = request.params.applicationId; applicationRepository.find({_id: applicationId}, entity => { if (entity.length === 0) { response.status(404).send(); @@ -32,7 +37,7 @@ router.put('/:applicationId', (request, response) => { }); router.delete('/:applicationId', (request, response) => { - const applicationId = ObjectId(request.params.applicationId); + const applicationId = request.params.applicationId; applicationRepository.find({_id: applicationId}, entity => { if (entity.length === 0) { response.status(404).send(); diff --git a/src/js/controller/templateCtrl.js b/src/js/controller/templateCtrl.js new file mode 100644 index 0000000..6ea9355 --- /dev/null +++ b/src/js/controller/templateCtrl.js @@ -0,0 +1,6 @@ +const Repository = require('../repository/repository'); +const router = require('express').Router(); + +// Develop routes here + +module.exports = router; \ No newline at end of file diff --git a/src/js/repository/mongoClient.js b/src/js/repository/mongoClient.js index cb20689..dddbb54 100644 --- a/src/js/repository/mongoClient.js +++ b/src/js/repository/mongoClient.js @@ -1,13 +1,15 @@ const mongodb = require('mongodb'); -const assert = require('assert'); - const configuration = require('../configuration'); const mongoConfig = configuration.database; class MongoClient { constructor() { mongodb.MongoClient.connect(mongoConfig.url, (error, client) => { - assert.equal(null, error, `Unable to connect to mongodb: ${error}.`); + console.log(error); + if (error !== null) { + throw new Error(`Unable de connect to Mongo database: ${error}`); + } + console.log('Connected successfuly to mongodb'); this.db = client.db(mongoConfig.database); }); @@ -19,12 +21,17 @@ class MongoClient { console.log(`Entities ${collectionName} founded.`); callback(results); }) - .catch(error => console.error(error)); + .catch(error => { + throw new Error(`Unable to find entities in collection ${collectionName}: ${error}`); + }); } insert(collectionName, entity, callback) { - this.db.collection(collectionName).insert(entity, (error, result) => { - assert.equal(null, error, `Unable to insert ${collectionName} entity: ${error}.`); + this.db.collection(collectionName).insertOne(entity, (error, result) => { + if (error !== null) { + throw new Error(`Unable to insert ${collectionName} entity: ${error}`); + } + console.log(`Entity ${collectionName} inserted.`); // Return only the inserted document. callback(result.ops[0]); @@ -32,8 +39,11 @@ class MongoClient { } update(collectionName, entity, callback) { - this.db.collection(collectionName).save(entity, (error) => { - assert.equal(null, error, `Unable to update ${collectionName} entity: ${error}.`); + this.db.collection(collectionName).updateOne({_id: mongodb.ObjectId(entity._id)}, {$set: entity}, {upsert: true}, (error) => { + if (error !== null) { + throw new Error(`Unable to update ${collectionName} entity: ${error}`); + } + console.log(`Entity ${collectionName} updated.`); callback(); }); @@ -41,12 +51,16 @@ class MongoClient { delete(collectionName, entityId, callback) { this.db.collection(collectionName).deleteOne({_id: mongodb.ObjectId(entityId)}, (error) => { - assert.equal(null, error, `Unable to delete ${collectionName} entity with id ${entityId}: ${error}.`); + if (error !== null) { + throw new Error(`Unable to delete ${collectionName} entity with id ${entityId}: ${error}`); + } + console.log(`Entity ${collectionName} with id ${entityId} deleted.`); callback(); }); } } +// Define a singleton of class "MongoClient". const mongoClient = new MongoClient(); module.exports = mongoClient; \ No newline at end of file diff --git a/src/js/repository/repository.js b/src/js/repository/repository.js index 06e7320..f17bf90 100644 --- a/src/js/repository/repository.js +++ b/src/js/repository/repository.js @@ -1,26 +1,63 @@ const mongoClient = require('./mongoClient'); +const ObjectId = require('mongodb').ObjectId; -class Repository { +/** + * If entity in parameters has an attribute named "_id", this function converts it into a MongoDB {@code ObjectId}. + */ +function convertIdToMongodbFormat(entity) { + if (!!entity._id && !(entity._id instanceof ObjectId)) { + entity._id = ObjectId(entity._id); + } +} + +module.exports = class Repository { + /** + * Creates a new repository which read and write into the {@code collectionName} collection in database. + * @param {*} collectionName + */ constructor(collectionName) { this.collectionName = collectionName; this.mongoClient = mongoClient; } + /** + * Returns the entities that matches criteria in {@code query}. + * @param {*} query The query which contains criteria to find some entities. + * @param {*} callback The function to execute after getting entities. + */ find(query, callback) { + convertIdToMongodbFormat(query); this.mongoClient.find(this.collectionName, query, callback); } + /** + * Insert entity in database. + * @param {*} entity The entity to insert into database. + * @param {*} callback The function to execute after inserting entity. + */ insert(entity, callback) { this.mongoClient.insert(this.collectionName, entity, callback); } + /** + * Update the whole entity in database. + * @param {*} entity The entity to update into database. + * @param {*} callback The function to execute after updating entity. + */ update(entity, callback) { + convertIdToMongodbFormat(entity); this.mongoClient.update(this.collectionName, entity, callback); } + /** + * Delete entity in database. + * @param {*} entityId Entity to delete id. + * @param {*} callback The function to execute after deleting entity. + */ delete(entityId, callback) { + if (!(entityId instanceof ObjectId)) { + entityId = ObjectId(entityId); + } this.mongoClient.delete(this.collectionName, entityId, callback); } -} - -module.exports = Repository; \ No newline at end of file +}; \ No newline at end of file