Clean code.
This commit is contained in:
@@ -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'));
|
||||
8
src/js/configuration.js
Normal file
8
src/js/configuration.js
Normal file
@@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
database: {
|
||||
url: 'mongodb://localhost:27017',
|
||||
username: 'express-user',
|
||||
password: 'P@ssword1',
|
||||
database: 'express-test'
|
||||
}
|
||||
};
|
||||
47
src/js/controller/applicationCtrl.js
Normal file
47
src/js/controller/applicationCtrl.js
Normal file
@@ -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;
|
||||
@@ -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;
|
||||
const mongoClient = new MongoClient();
|
||||
module.exports = mongoClient;
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = Repository;
|
||||
Reference in New Issue
Block a user