Update repository functions.
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
const ObjectId = require('mongodb').ObjectId;
|
|
||||||
const Repository = require('../repository/repository');
|
const Repository = require('../repository/repository');
|
||||||
const router = require('express').Router();
|
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) => {
|
router.post('/', (request, response) => {
|
||||||
applicationRepository.insert(request.body, (result) => {
|
applicationRepository.insert(request.body, (result) => {
|
||||||
response.json(result);
|
response.json(result);
|
||||||
@@ -17,7 +22,7 @@ router.post('/', (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.put('/:applicationId', (request, response) => {
|
router.put('/:applicationId', (request, response) => {
|
||||||
const applicationId = ObjectId(request.params.applicationId);
|
const applicationId = request.params.applicationId;
|
||||||
applicationRepository.find({_id: applicationId}, entity => {
|
applicationRepository.find({_id: applicationId}, entity => {
|
||||||
if (entity.length === 0) {
|
if (entity.length === 0) {
|
||||||
response.status(404).send();
|
response.status(404).send();
|
||||||
@@ -32,7 +37,7 @@ router.put('/:applicationId', (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.delete('/:applicationId', (request, response) => {
|
router.delete('/:applicationId', (request, response) => {
|
||||||
const applicationId = ObjectId(request.params.applicationId);
|
const applicationId = request.params.applicationId;
|
||||||
applicationRepository.find({_id: applicationId}, entity => {
|
applicationRepository.find({_id: applicationId}, entity => {
|
||||||
if (entity.length === 0) {
|
if (entity.length === 0) {
|
||||||
response.status(404).send();
|
response.status(404).send();
|
||||||
|
|||||||
6
src/js/controller/templateCtrl.js
Normal file
6
src/js/controller/templateCtrl.js
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
const Repository = require('../repository/repository');
|
||||||
|
const router = require('express').Router();
|
||||||
|
|
||||||
|
// Develop routes here
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
const mongodb = require('mongodb');
|
const mongodb = require('mongodb');
|
||||||
const assert = require('assert');
|
|
||||||
|
|
||||||
const configuration = require('../configuration');
|
const configuration = require('../configuration');
|
||||||
const mongoConfig = configuration.database;
|
const mongoConfig = configuration.database;
|
||||||
|
|
||||||
class MongoClient {
|
class MongoClient {
|
||||||
constructor() {
|
constructor() {
|
||||||
mongodb.MongoClient.connect(mongoConfig.url, (error, client) => {
|
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');
|
console.log('Connected successfuly to mongodb');
|
||||||
this.db = client.db(mongoConfig.database);
|
this.db = client.db(mongoConfig.database);
|
||||||
});
|
});
|
||||||
@@ -19,12 +21,17 @@ class MongoClient {
|
|||||||
console.log(`Entities ${collectionName} founded.`);
|
console.log(`Entities ${collectionName} founded.`);
|
||||||
callback(results);
|
callback(results);
|
||||||
})
|
})
|
||||||
.catch(error => console.error(error));
|
.catch(error => {
|
||||||
|
throw new Error(`Unable to find entities in collection ${collectionName}: ${error}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
insert(collectionName, entity, callback) {
|
insert(collectionName, entity, callback) {
|
||||||
this.db.collection(collectionName).insert(entity, (error, result) => {
|
this.db.collection(collectionName).insertOne(entity, (error, result) => {
|
||||||
assert.equal(null, error, `Unable to insert ${collectionName} entity: ${error}.`);
|
if (error !== null) {
|
||||||
|
throw new Error(`Unable to insert ${collectionName} entity: ${error}`);
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`Entity ${collectionName} inserted.`);
|
console.log(`Entity ${collectionName} inserted.`);
|
||||||
// Return only the inserted document.
|
// Return only the inserted document.
|
||||||
callback(result.ops[0]);
|
callback(result.ops[0]);
|
||||||
@@ -32,8 +39,11 @@ class MongoClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update(collectionName, entity, callback) {
|
update(collectionName, entity, callback) {
|
||||||
this.db.collection(collectionName).save(entity, (error) => {
|
this.db.collection(collectionName).updateOne({_id: mongodb.ObjectId(entity._id)}, {$set: entity}, {upsert: true}, (error) => {
|
||||||
assert.equal(null, error, `Unable to update ${collectionName} entity: ${error}.`);
|
if (error !== null) {
|
||||||
|
throw new Error(`Unable to update ${collectionName} entity: ${error}`);
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`Entity ${collectionName} updated.`);
|
console.log(`Entity ${collectionName} updated.`);
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
@@ -41,12 +51,16 @@ class MongoClient {
|
|||||||
|
|
||||||
delete(collectionName, entityId, callback) {
|
delete(collectionName, entityId, callback) {
|
||||||
this.db.collection(collectionName).deleteOne({_id: mongodb.ObjectId(entityId)}, (error) => {
|
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.`);
|
console.log(`Entity ${collectionName} with id ${entityId} deleted.`);
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Define a singleton of class "MongoClient".
|
||||||
const mongoClient = new MongoClient();
|
const mongoClient = new MongoClient();
|
||||||
module.exports = mongoClient;
|
module.exports = mongoClient;
|
||||||
@@ -1,26 +1,63 @@
|
|||||||
const mongoClient = require('./mongoClient');
|
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) {
|
constructor(collectionName) {
|
||||||
this.collectionName = collectionName;
|
this.collectionName = collectionName;
|
||||||
this.mongoClient = mongoClient;
|
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) {
|
find(query, callback) {
|
||||||
|
convertIdToMongodbFormat(query);
|
||||||
this.mongoClient.find(this.collectionName, query, callback);
|
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) {
|
insert(entity, callback) {
|
||||||
this.mongoClient.insert(this.collectionName, 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) {
|
update(entity, callback) {
|
||||||
|
convertIdToMongodbFormat(entity);
|
||||||
this.mongoClient.update(this.collectionName, entity, callback);
|
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) {
|
delete(entityId, callback) {
|
||||||
|
if (!(entityId instanceof ObjectId)) {
|
||||||
|
entityId = ObjectId(entityId);
|
||||||
|
}
|
||||||
this.mongoClient.delete(this.collectionName, entityId, callback);
|
this.mongoClient.delete(this.collectionName, entityId, callback);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = Repository;
|
|
||||||
Reference in New Issue
Block a user