Clean code.
This commit is contained in:
@@ -1,49 +1,13 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const ObjectId = require('mongodb').ObjectId;
|
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
|
const applicationController = require('./controller/applicationCtrl');
|
||||||
|
|
||||||
const MongoClient = require('./mongoClient');
|
const port = 3000;
|
||||||
const Repository = require('./repository');
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
const port = 3000;
|
|
||||||
|
|
||||||
const mongoClient = new MongoClient();
|
app.use('/apps', applicationController);
|
||||||
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.listen(port, () => console.log('Mock is listening at port ', port, '\n'));
|
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 mongodb = require('mongodb');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
const mongoConfig = {
|
const configuration = require('../configuration');
|
||||||
url: 'mongodb://localhost:27017',
|
const mongoConfig = configuration.database;
|
||||||
username: 'express-user',
|
|
||||||
password: 'P@ssword1',
|
|
||||||
database: 'express-test'
|
|
||||||
};
|
|
||||||
|
|
||||||
class MongoClient {
|
class MongoClient {
|
||||||
constructor() {
|
constructor() {
|
||||||
mongodb.MongoClient.connect(mongoConfig.url, (err, client) => {
|
mongodb.MongoClient.connect(mongoConfig.url, (error, client) => {
|
||||||
assert.equal(null, err);
|
assert.equal(null, error, `Unable to connect to mongodb: ${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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
find(collectionName, query, successCallback, errorCallback) {
|
find(collectionName, query, callback) {
|
||||||
this.db.collection(collectionName).find(query).toArray()
|
this.db.collection(collectionName).find(query).toArray()
|
||||||
.then(results => {
|
.then(results => {
|
||||||
console.log(`Entities ${collectionName} founded.`);
|
console.log(`Entities ${collectionName} founded.`);
|
||||||
successCallback(results);
|
callback(results);
|
||||||
})
|
})
|
||||||
.catch(errorCallback);
|
.catch(error => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
insert(collectionName, entity, callback) {
|
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 {
|
const mongoClient = require('./mongoClient');
|
||||||
constructor(collectionName, mongoClient) {
|
|
||||||
|
class Repository {
|
||||||
|
constructor(collectionName) {
|
||||||
this.collectionName = collectionName;
|
this.collectionName = collectionName;
|
||||||
this.mongoClient = mongoClient;
|
this.mongoClient = mongoClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
find(query, successCallback, errorCallback) {
|
find(query, callback) {
|
||||||
this.mongoClient.find(this.collectionName, query, successCallback, errorCallback);
|
this.mongoClient.find(this.collectionName, query, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
insert(entity, callback) {
|
insert(entity, callback) {
|
||||||
@@ -19,4 +21,6 @@ module.exports = class Repository {
|
|||||||
delete(entityId, callback) {
|
delete(entityId, callback) {
|
||||||
this.mongoClient.delete(this.collectionName, entityId, callback);
|
this.mongoClient.delete(this.collectionName, entityId, callback);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = Repository;
|
||||||
Reference in New Issue
Block a user