Correct crud functions.

This commit is contained in:
2020-09-06 19:23:11 +02:00
parent d4cc887bf5
commit dc79b7bc0e
5 changed files with 79 additions and 15 deletions

17
.vscode/launch.json vendored Normal file
View 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"
}
]
}

View File

@@ -0,0 +1,7 @@
version: "3.6"
services:
mongo:
container_name: "mongo"
image: "mongo:latest"
ports:
- "27017:27017"

View File

@@ -1,28 +1,47 @@
const express = require('express'); const express = require('express');
const ObjectId = require('mongodb').ObjectId;
const bodyParser = require('body-parser');
const MongoClient = require('./mongoClient'); const MongoClient = require('./mongoClient');
const Repository = require('./repository');
const app = express(); const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const port = 3000; const port = 3000;
const mongoClient = new MongoClient(); const mongoClient = new MongoClient();
const applicationRepository = new Repository('applications', mongoClient);
app.get('/test', (request, response) => { app.get('/apps', (request, response) => {
mongoClient.find('test', {}, results => { applicationRepository.find({}, results => {
response.json(results); response.json(results);
}); });
}); });
app.get('/test2', (request, response) => { app.post('/apps', (request, response) => {
// mongoClient.insert('test', {address: 'localhost'}, (result) => { applicationRepository.insert(request.body, (result) => {
// response.json(result); response.json(result);
// }); });
});
// mongoClient.update('test', {_id: '5f54b1c0f4dca82b7ad825bc', address: '127.0.0.1', updated: new Date()}, (result) => { app.put('/apps/:applicationId', (request, response) => {
// response.json(result); 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();
});
}
});
});
mongoClient.delete('test', '5f54b6c52d9355311a4e68a0', () => { app.delete('/apps/:applicationId', (request, response) => {
applicationRepository.delete(request.params.applicationId, () => {
response.status(200).send('Deleted'); response.status(200).send('Deleted');
}); });
}); });

View File

@@ -1,5 +1,4 @@
const mongodb = require('mongodb'); const mongodb = require('mongodb');
const ObjectID = require('mongodb').ObjectID;
const assert = require('assert'); const assert = require('assert');
const mongoConfig = { const mongoConfig = {
@@ -18,13 +17,13 @@ class MongoClient {
}); });
} }
find(collectionName, query, callback) { find(collectionName, query, successCallback, errorCallback) {
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.`);
callback(results); successCallback(results);
}) })
.catch(error => console.error(error)); .catch(errorCallback);
} }
insert(collectionName, entity, callback) { insert(collectionName, entity, callback) {
@@ -45,7 +44,7 @@ class MongoClient {
} }
delete(collectionName, entityId, callback) { 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}.`); assert.equal(null, error, `Unable to delete ${collectionName} entity with id ${entityId}: ${error}.`);
console.log(`Entity ${collectionName} with id ${entityId} deleted.`); console.log(`Entity ${collectionName} with id ${entityId} deleted.`);
callback(); callback();

22
src/js/repository.js Normal file
View File

@@ -0,0 +1,22 @@
module.exports = class Repository {
constructor(collectionName, mongoClient) {
this.collectionName = collectionName;
this.mongoClient = mongoClient;
}
find(query, successCallback, errorCallback) {
this.mongoClient.find(this.collectionName, query, successCallback, errorCallback);
}
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);
}
};