From dc79b7bc0e8d2caaf83eb5a16d063b8be28ad649 Mon Sep 17 00:00:00 2001 From: takiguchi Date: Sun, 6 Sep 2020 19:23:11 +0200 Subject: [PATCH] Correct crud functions. --- .vscode/launch.json | 17 +++++++++++++++++ docker/docker-compose.yml | 7 +++++++ src/js/app.js | 39 +++++++++++++++++++++++++++++---------- src/js/mongoClient.js | 9 ++++----- src/js/repository.js | 22 ++++++++++++++++++++++ 5 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 docker/docker-compose.yml create mode 100644 src/js/repository.js diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..61b1623 --- /dev/null +++ b/.vscode/launch.json @@ -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": [ + "/**" + ], + "program": "${workspaceFolder}/src/js/app.js" + } + ] +} \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..9de7b71 --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,7 @@ +version: "3.6" +services: + mongo: + container_name: "mongo" + image: "mongo:latest" + ports: + - "27017:27017" \ No newline at end of file diff --git a/src/js/app.js b/src/js/app.js index 175e833..f74b60d 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -1,28 +1,47 @@ const express = require('express'); +const ObjectId = require('mongodb').ObjectId; +const bodyParser = require('body-parser'); const MongoClient = require('./mongoClient'); +const Repository = require('./repository'); 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('/test', (request, response) => { - mongoClient.find('test', {}, results => { +app.get('/apps', (request, response) => { + applicationRepository.find({}, results => { response.json(results); }); }); -app.get('/test2', (request, response) => { - // mongoClient.insert('test', {address: 'localhost'}, (result) => { - // response.json(result); - // }); +app.post('/apps', (request, response) => { + applicationRepository.insert(request.body, (result) => { + response.json(result); + }); +}); - // mongoClient.update('test', {_id: '5f54b1c0f4dca82b7ad825bc', address: '127.0.0.1', updated: new Date()}, (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(); + }); + } + }); +}); - mongoClient.delete('test', '5f54b6c52d9355311a4e68a0', () => { +app.delete('/apps/:applicationId', (request, response) => { + applicationRepository.delete(request.params.applicationId, () => { response.status(200).send('Deleted'); }); }); diff --git a/src/js/mongoClient.js b/src/js/mongoClient.js index 1b60bf4..93cdd62 100644 --- a/src/js/mongoClient.js +++ b/src/js/mongoClient.js @@ -1,5 +1,4 @@ const mongodb = require('mongodb'); -const ObjectID = require('mongodb').ObjectID; const assert = require('assert'); const mongoConfig = { @@ -18,13 +17,13 @@ class MongoClient { }); } - find(collectionName, query, callback) { + find(collectionName, query, successCallback, errorCallback) { this.db.collection(collectionName).find(query).toArray() .then(results => { console.log(`Entities ${collectionName} founded.`); - callback(results); + successCallback(results); }) - .catch(error => console.error(error)); + .catch(errorCallback); } insert(collectionName, entity, callback) { @@ -45,7 +44,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(); diff --git a/src/js/repository.js b/src/js/repository.js new file mode 100644 index 0000000..0ced10e --- /dev/null +++ b/src/js/repository.js @@ -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); + } +}; \ No newline at end of file