Compare commits
8 Commits
feature/in
...
29411d3c87
| Author | SHA1 | Date | |
|---|---|---|---|
| 29411d3c87 | |||
| dc79b7bc0e | |||
|
|
d4cc887bf5 | ||
|
|
14da8bf0f1 | ||
|
|
c618675a9a | ||
|
|
80253cabf8 | ||
|
|
2c424ac5d6 | ||
|
|
a558f09fb4 |
46
.eslintrc.json
Normal file
46
.eslintrc.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"root": true,
|
||||
"env": {
|
||||
"node": true,
|
||||
"browser": true,
|
||||
"es6": true
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended"
|
||||
],
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly",
|
||||
"$": "readonly",
|
||||
"google": "readonly",
|
||||
"exampleGlobalVariable": true
|
||||
},
|
||||
"plugins": [
|
||||
// "jquery"
|
||||
],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"rules": {
|
||||
"semi": ["error", "always"],
|
||||
"no-inner-declarations": 0,
|
||||
"indent": ["error", 4],
|
||||
"eqeqeq": ["warn", "always"],
|
||||
"curly": "error",
|
||||
"default-case": "error",
|
||||
"no-var": "error",
|
||||
"no-multi-spaces": "error",
|
||||
"no-useless-constructor": "error",
|
||||
"prefer-const": "error",
|
||||
"prefer-rest-params": "error",
|
||||
"prefer-spread": "error",
|
||||
"no-console": "off",
|
||||
"prefer-template": "warn",
|
||||
"quotes": [
|
||||
"warn",
|
||||
"single",
|
||||
{ "avoidEscape": true }
|
||||
]
|
||||
}
|
||||
}
|
||||
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"
|
||||
}
|
||||
]
|
||||
}
|
||||
12
app.js
12
app.js
@@ -1,12 +0,0 @@
|
||||
const express = require('express');
|
||||
const Mongo = require('./mongo');
|
||||
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
|
||||
app.get('/test', (request, response) => {
|
||||
const mongoClient = new Mongo();
|
||||
})
|
||||
|
||||
app.listen(port, () => console.log('Mock is listening at port ', port, '\n'));
|
||||
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"
|
||||
27
mongo.js
27
mongo.js
@@ -1,27 +0,0 @@
|
||||
const mongodb = require('mongodb');
|
||||
const assert = require('assert');
|
||||
|
||||
const mongoConfig = {
|
||||
url: 'mongodb://localhost:27017',
|
||||
username: 'express-user',
|
||||
password: 'P@ssword1',
|
||||
database: 'express-test'
|
||||
}
|
||||
|
||||
class Mongo {
|
||||
constructor() {
|
||||
mongodb.MongoClient.connect(mongoConfig.url, (err, client) => {
|
||||
assert.equal(null, err);
|
||||
console.log('Connected successfuly to mongodb');
|
||||
this.db = client.db(mongoConfig.database);
|
||||
|
||||
this.db.collection('test').find({}, (err, results) => {
|
||||
results.forEach(console.log);
|
||||
});
|
||||
|
||||
client.close();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Mongo;
|
||||
34
package-lock.json
generated
34
package-lock.json
generated
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "Mock-SDS",
|
||||
"name": "ExpressJS",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
@@ -179,7 +179,6 @@
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/bl/-/bl-2.2.1.tgz",
|
||||
"integrity": "sha512-6Pesp1w0DEX1N550i/uGV/TqucVL4AM/pgThFSN/Qq9si1/DF9aIHs1BxD8V/QU0HoeHO6cQRTAuYnLPKq1e4g==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"readable-stream": "^2.3.5",
|
||||
"safe-buffer": "^5.1.1"
|
||||
@@ -315,8 +314,7 @@
|
||||
"bson": {
|
||||
"version": "1.1.5",
|
||||
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.5.tgz",
|
||||
"integrity": "sha512-kDuEzldR21lHciPQAIulLs1LZlCXdLziXI6Mb/TDkwXhb//UORJNPXgcRs2CuO4H0DcMkpfT3/ySsP3unoZjBg==",
|
||||
"dev": true
|
||||
"integrity": "sha512-kDuEzldR21lHciPQAIulLs1LZlCXdLziXI6Mb/TDkwXhb//UORJNPXgcRs2CuO4H0DcMkpfT3/ySsP3unoZjBg=="
|
||||
},
|
||||
"bytes": {
|
||||
"version": "3.1.0",
|
||||
@@ -517,8 +515,7 @@
|
||||
"core-util-is": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
|
||||
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=",
|
||||
"dev": true
|
||||
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
|
||||
},
|
||||
"cross-spawn": {
|
||||
"version": "7.0.3",
|
||||
@@ -575,8 +572,7 @@
|
||||
"denque": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz",
|
||||
"integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ=="
|
||||
},
|
||||
"depd": {
|
||||
"version": "1.1.2",
|
||||
@@ -1241,8 +1237,7 @@
|
||||
"isarray": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
|
||||
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=",
|
||||
"dev": true
|
||||
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
|
||||
},
|
||||
"isexe": {
|
||||
"version": "2.0.0",
|
||||
@@ -1350,7 +1345,6 @@
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
|
||||
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
|
||||
"dev": true,
|
||||
"optional": true
|
||||
},
|
||||
"merge-descriptors": {
|
||||
@@ -1415,7 +1409,6 @@
|
||||
"version": "3.6.1",
|
||||
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.1.tgz",
|
||||
"integrity": "sha512-uH76Zzr5wPptnjEKJRQnwTsomtFOU/kQEU8a9hKHr2M7y9qVk7Q4Pkv0EQVp88742z9+RwvsdTw6dRjDZCNu1g==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"bl": "^2.2.0",
|
||||
"bson": "^1.1.4",
|
||||
@@ -1618,8 +1611,7 @@
|
||||
"process-nextick-args": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
||||
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==",
|
||||
"dev": true
|
||||
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
|
||||
},
|
||||
"progress": {
|
||||
"version": "2.0.3",
|
||||
@@ -1712,7 +1704,6 @@
|
||||
"version": "2.3.7",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
|
||||
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"core-util-is": "~1.0.0",
|
||||
"inherits": "~2.0.3",
|
||||
@@ -1760,7 +1751,6 @@
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz",
|
||||
"integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"resolve-from": "^2.0.0",
|
||||
"semver": "^5.1.0"
|
||||
@@ -1769,14 +1759,12 @@
|
||||
"resolve-from": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz",
|
||||
"integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=",
|
||||
"dev": true
|
||||
"integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c="
|
||||
},
|
||||
"semver": {
|
||||
"version": "5.7.1",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
|
||||
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ=="
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -1827,7 +1815,6 @@
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz",
|
||||
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"sparse-bitfield": "^3.0.3"
|
||||
@@ -1935,7 +1922,6 @@
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
|
||||
"integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=",
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"memory-pager": "^1.0.2"
|
||||
@@ -1984,7 +1970,6 @@
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
|
||||
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"safe-buffer": "~5.1.0"
|
||||
}
|
||||
@@ -2216,8 +2201,7 @@
|
||||
"util-deprecate": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
|
||||
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=",
|
||||
"dev": true
|
||||
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
|
||||
},
|
||||
"utils-merge": {
|
||||
"version": "1.0.1",
|
||||
|
||||
@@ -4,19 +4,19 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "nodemon ./app.js localhost 3000",
|
||||
"start": "nodemon ./src/js/app.js localhost 3000",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"express": "^4.17.1"
|
||||
"express": "^4.17.1",
|
||||
"mongodb": "^3.6.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^7.4.0",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"nodemon": "^2.0.4",
|
||||
"mongodb": "^3.6.1"
|
||||
"nodemon": "^2.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
13
src/js/app.js
Normal file
13
src/js/app.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const applicationController = require('./controller/applicationCtrl');
|
||||
|
||||
const port = 3000;
|
||||
|
||||
const app = express();
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
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;
|
||||
52
src/js/repository/mongoClient.js
Normal file
52
src/js/repository/mongoClient.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const mongodb = require('mongodb');
|
||||
const assert = require('assert');
|
||||
|
||||
const configuration = require('../configuration');
|
||||
const mongoConfig = configuration.database;
|
||||
|
||||
class MongoClient {
|
||||
constructor() {
|
||||
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, callback) {
|
||||
this.db.collection(collectionName).find(query).toArray()
|
||||
.then(results => {
|
||||
console.log(`Entities ${collectionName} founded.`);
|
||||
callback(results);
|
||||
})
|
||||
.catch(error => console.error(error));
|
||||
}
|
||||
|
||||
insert(collectionName, entity, callback) {
|
||||
this.db.collection(collectionName).insert(entity, (error, result) => {
|
||||
assert.equal(null, error, `Unable to insert ${collectionName} entity: ${error}.`);
|
||||
console.log(`Entity ${collectionName} inserted.`);
|
||||
// Return only the inserted document.
|
||||
callback(result.ops[0]);
|
||||
});
|
||||
}
|
||||
|
||||
update(collectionName, entity, callback) {
|
||||
this.db.collection(collectionName).save(entity, (error) => {
|
||||
assert.equal(null, error, `Unable to update ${collectionName} entity: ${error}.`);
|
||||
console.log(`Entity ${collectionName} updated.`);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
delete(collectionName, entityId, callback) {
|
||||
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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
14
src/mongodb/initDatabase.js
Normal file
14
src/mongodb/initDatabase.js
Normal file
@@ -0,0 +1,14 @@
|
||||
use express-test;
|
||||
|
||||
db.collection.test.insert({id: 'test'});
|
||||
|
||||
db.createUser(
|
||||
{
|
||||
user: "express-user",
|
||||
pwd: "P@ssword1",
|
||||
roles:
|
||||
[
|
||||
{ role: "readWrite", db: "express-test" }
|
||||
]
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user