From d77bb807ea9f1eb1dc39277da4995dea4e03b4bf Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 22 Dec 2020 13:35:48 +0100 Subject: [PATCH] Save Command Implementation --- Client/httpInterface.js | 110 ++++++++++++++++++ .../UI/Spaces/Docs-Space/DocsSpace.js | 39 ++++++- 2 files changed, 146 insertions(+), 3 deletions(-) diff --git a/Client/httpInterface.js b/Client/httpInterface.js index 14bf3851a..64f5baab4 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -301,6 +301,116 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE } break + case 'Docs': + { + switch (requestParameters[2]) { // switch by command + case 'Save-Node-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + const fs = require('fs') + + for (let i = 0; i < docsSchema.length; i++) { + let schemaDocument = docsSchema[i] + let fileContent = JSON.stringify(schemaDocument) + let fileName = schemaDocument.type.toLowerCase().replaceAll(' ', '-') + '.json' + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Nodes' + fs.writeFileSync(filePath + '/' + fileName, fileContent) + } + + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Node-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> WEB3s -> Save-Node-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> WEB3s -> Save-Node-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + case 'Save-Concept-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + const fs = require('fs') + + for (let i = 0; i < docsSchema.length; i++) { + let schemaDocument = docsSchema[i] + let fileContent = JSON.stringify(schemaDocument) + let fileName = schemaDocument.type.toLowerCase().replaceAll(' ', '-') + '.json' + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Concepts' + fs.writeFileSync(filePath + '/' + fileName, fileContent) + } + + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Concept-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> WEB3s -> Save-Concept-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> WEB3s -> Save-Concept-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + case 'Save-Topic-Schema': { + getBody(processRequest) + + async function processRequest(body) { + try { + let docsSchema = JSON.parse(body) + let project = requestParameters[3] + const fs = require('fs') + + for (let i = 0; i < docsSchema.length; i++) { + let schemaDocument = docsSchema[i] + let fileContent = JSON.stringify(schemaDocument) + let fileName = schemaDocument.type.toLowerCase().replaceAll(' ', '-') + '.json' + let filePath = global.env.PATH_TO_PROJECTS + '/' + project + '/Schemas/Docs-Topics' + fs.writeFileSync(filePath + '/' + fileName, fileContent) + } + + respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse) + + } catch (err) { + console.log('[ERROR] httpInterface -> Docs -> Save-Topic-Schema -> Method call produced an error.') + console.log('[ERROR] httpInterface -> WEB3s -> Save-Topic-Schema -> err.stack = ' + err.stack) + console.log('[ERROR] httpInterface -> WEB3s -> Save-Topic-Schema -> Params Received = ' + body) + + let error = { + result: 'Fail Because', + message: err.message, + stack: err.stack + } + respondWithContent(JSON.stringify(error), httpResponse) + } + } + break + } + } + break + } + break + case 'LegacyPlotter.js': { respondWithFile(global.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse) diff --git a/Projects/Superalgos/UI/Spaces/Docs-Space/DocsSpace.js b/Projects/Superalgos/UI/Spaces/Docs-Space/DocsSpace.js index 1d873c05b..ddb8d2ccf 100644 --- a/Projects/Superalgos/UI/Spaces/Docs-Space/DocsSpace.js +++ b/Projects/Superalgos/UI/Spaces/Docs-Space/DocsSpace.js @@ -818,7 +818,7 @@ function newSuperalgosDocSpace() { "Delete: Use this command to delete existing Nodes, Concepts or Topics from the Docs.", "Save: Use this command whenever you would like to save the changes you made to the Docs.", "Reindex: Use this command to re-index all the info again so that your changes are visible at search results.", - "Note that anything you type not identified as a command will be treated as a search query." + "Note that anything you type not identified as a command will be treated as a search query." ] ) return @@ -1000,8 +1000,41 @@ function newSuperalgosDocSpace() { } if (command.indexOf('Save') !== 0 && command.indexOf('save') !== 0) { return 'Not Save Command' } - - renderCommandResultsPage(["Succesfully saved all the latest changes."]) + let requestsSent = 0 + let responseCount = 0 + let okResponses = 0 + for (let j = 0; j < PROJECTS_ARRAY.length; j++) { + let docsSchema + let project = PROJECTS_ARRAY[j] + + docsSchema = SCHEMAS_BY_PROJECT.get(project).array.docsNodeSchema + httpRequest(JSON.stringify(docsSchema), 'Docs/Save-Node-Schema/' + project, onResponse) + requestsSent++ + + docsSchema = SCHEMAS_BY_PROJECT.get(project).array.docsConceptSchema + httpRequest(JSON.stringify(docsSchema), 'Docs/Save-Concept-Schema/' + project, onResponse) + requestsSent++ + + docsSchema = SCHEMAS_BY_PROJECT.get(project).array.docsTopicSchema + httpRequest(JSON.stringify(docsSchema), 'Docs/Save-Topic-Schema/' + project, onResponse) + requestsSent++ + } + + function onResponse(err, data) { + /* Lets check the result of the call through the http interface */ + if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) { + okResponses++ + } + responseCount++ + + if (responseCount === requestsSent) { + if (responseCount === okResponses) { + renderCommandResultsPage(["Succesfully saved all the latest changes."]) + } else { + renderCommandResultsPage(["Some of the changes could not be saved."]) + } + } + } } function addNode(project, type) {