Clean code.

This commit is contained in:
2020-09-06 19:36:58 +02:00
parent dc79b7bc0e
commit 29411d3c87
5 changed files with 76 additions and 56 deletions

View File

@@ -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
View File

@@ -0,0 +1,8 @@
module.exports = {
database: {
url: 'mongodb://localhost:27017',
username: 'express-user',
password: 'P@ssword1',
database: 'express-test'
}
};

View 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;

View File

@@ -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;

View File

@@ -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) {
@@ -20,3 +22,5 @@ module.exports = class Repository {
this.mongoClient.delete(this.collectionName, entityId, callback);
}
};
module.exports = Repository;