Compare commits
2 Commits
d4cc887bf5
...
29411d3c87
| Author | SHA1 | Date | |
|---|---|---|---|
| 29411d3c87 | |||
| dc79b7bc0e |
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Launch Program",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"program": "${workspaceFolder}/src/js/app.js"
|
||||
}
|
||||
]
|
||||
}
|
||||
7
docker/docker-compose.yml
Normal file
7
docker/docker-compose.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
version: "3.6"
|
||||
services:
|
||||
mongo:
|
||||
container_name: "mongo"
|
||||
image: "mongo:latest"
|
||||
ports:
|
||||
- "27017:27017"
|
||||
@@ -1,30 +1,13 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const applicationController = require('./controller/applicationCtrl');
|
||||
|
||||
const MongoClient = require('./mongoClient');
|
||||
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
const mongoClient = new MongoClient();
|
||||
const app = express();
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.get('/test', (request, response) => {
|
||||
mongoClient.find('test', {}, results => {
|
||||
response.json(results);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/test2', (request, response) => {
|
||||
// mongoClient.insert('test', {address: 'localhost'}, (result) => {
|
||||
// response.json(result);
|
||||
// });
|
||||
|
||||
// mongoClient.update('test', {_id: '5f54b1c0f4dca82b7ad825bc', address: '127.0.0.1', updated: new Date()}, (result) => {
|
||||
// response.json(result);
|
||||
// });
|
||||
|
||||
mongoClient.delete('test', '5f54b6c52d9355311a4e68a0', () => {
|
||||
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,18 +1,13 @@
|
||||
const mongodb = require('mongodb');
|
||||
const ObjectID = require('mongodb').ObjectID;
|
||||
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);
|
||||
});
|
||||
@@ -45,7 +40,7 @@ class MongoClient {
|
||||
}
|
||||
|
||||
delete(collectionName, entityId, callback) {
|
||||
this.db.collection(collectionName).deleteOne({id: new ObjectID(entityId)}, (error) => {
|
||||
this.db.collection(collectionName).deleteOne({_id: mongodb.ObjectId(entityId)}, (error, result) => {
|
||||
assert.equal(null, error, `Unable to delete ${collectionName} entity with id ${entityId}: ${error}.`);
|
||||
console.log(`Entity ${collectionName} with id ${entityId} deleted.`);
|
||||
callback();
|
||||
@@ -53,4 +48,5 @@ class MongoClient {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MongoClient;
|
||||
const mongoClient = new MongoClient();
|
||||
module.exports = mongoClient;
|
||||
26
src/js/repository/repository.js
Normal file
26
src/js/repository/repository.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const mongoClient = require('./mongoClient');
|
||||
|
||||
class Repository {
|
||||
constructor(collectionName) {
|
||||
this.collectionName = collectionName;
|
||||
this.mongoClient = mongoClient;
|
||||
}
|
||||
|
||||
find(query, callback) {
|
||||
this.mongoClient.find(this.collectionName, query, callback);
|
||||
}
|
||||
|
||||
insert(entity, callback) {
|
||||
this.mongoClient.insert(this.collectionName, entity, callback);
|
||||
}
|
||||
|
||||
update(entity, callback) {
|
||||
this.mongoClient.update(this.collectionName, entity, callback);
|
||||
}
|
||||
|
||||
delete(entityId, callback) {
|
||||
this.mongoClient.delete(this.collectionName, entityId, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Repository;
|
||||
Reference in New Issue
Block a user