123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //© 2019 Dublin City University, Trinity College Dublin. All rights reserved. This material may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder.
- var path = require('path');
- const dataStoreFile = __dirname+'/store/datasetConfig.json';
- //const dataStoreFile = path.normalize(path.resolve(__dirname))+'/store/datasetConfig.json';
- class datasetsController {
-
- getStatus(req, res) {
- return res.status(200).send(
- {
- status : "OK"
- }
- );
- }
-
- getAllDataset(req, res) {
- const fs = require('fs');
- let rawdata = fs.readFileSync(dataStoreFile);
- let transformedData = JSON.parse(rawdata);
- return res.status(200).send(
- transformedData
- );
- }
- getDataset(req, res) {
- const fs = require('fs');
- let datasetFound = false;
- let datasetDetails=[];
- let rawdata = fs.readFileSync(dataStoreFile);
- let transformedData = JSON.parse(rawdata);
- const datasetID = parseInt(req.query.datasetID, 10);
- let datasetPLD = req.query.datasetPLD;
- let datasetName = req.query.datasetName;
- transformedData["datasetDetails"].map((dataset) => {
- console.log(req.query)
- if (datasetID){
- if (dataset.datasetID === datasetID) {
- datasetDetails.push(dataset);
- }
- }
- else if (datasetName)
- {
- if (((dataset.datasetName).toUpperCase()).includes(datasetName.toUpperCase())) {
- datasetDetails.push(dataset);
- }
- }
- else if (datasetPLD)
- {
- if (dataset.datasetPLD === datasetPLD) {
- datasetDetails.push(dataset);
- }
- }
- console.log(datasetDetails)
- });
- if (datasetDetails.length>0){
- return res.status(200).send({
- datasetDetails
- });
- }
- else{
- return res.status(404).send({
- success: 'false',
- message: 'Dataset configuration details does not exist',
- });
- }
- }
- updateDataset(req, res) {
- const fs = require('fs');
- let rawdata = fs.readFileSync(dataStoreFile);
- let transformedData = JSON.parse(rawdata);
- const datasetID = parseInt(req.query.datasetID, 10);
- let datasetFound;
- let datasetIndex;
- let newLastAssessmentRequestID = req.body.lastAssessmentRequestID;
- let newfileName = req.body.fileName;
- let newisSparqlEndPoint = req.body.isSparqlEndPoint;
- let newsparqlEndPoint = req.body.sparqlEndPoint;
- let newexpectedProgress = req.body.expectedProgress;
- let newassessmentMetrics = req.body.assessmentMetrics;
- let newKnowledgeBaseID = req.body.knowledgeBaseID;
- transformedData["datasetDetails"].map((dataset, index) => {
- if (dataset.datasetID === datasetID) {
- datasetFound = dataset;
- datasetIndex = index;
- }
- });
- if (!datasetFound) {
- return res.status(404).send({
- success: 'false',
- message: 'Dataset configuration details does not exist',
- });
- }
- else
- {
-
- if(typeof newLastAssessmentRequestID!=="undefined")
- {
- transformedData["datasetDetails"][datasetIndex].lastAssessmentRequestID=newLastAssessmentRequestID;
- }
-
- if(typeof newfileName!=="undefined")
- {
- transformedData["datasetDetails"][datasetIndex].fileName=newfileName;
- }
-
- if(typeof newisSparqlEndPoint!=="undefined")
- {
- transformedData["datasetDetails"][datasetIndex].isSparqlEndPoint=newisSparqlEndPoint;
- }
-
- if(typeof newsparqlEndPoint!=="undefined")
- {
- transformedData["datasetDetails"][datasetIndex].sparqlEndPoint=newsparqlEndPoint;
- }
-
- if(typeof newexpectedProgress!=="undefined")
- {
- transformedData["datasetDetails"][datasetIndex].expectedProgress=newexpectedProgress;
- }
-
- if(typeof newassessmentMetrics!=="undefined")
- {
- transformedData["datasetDetails"][datasetIndex].assessmentMetrics=newassessmentMetrics;
- }
-
- if(typeof newKnowledgeBaseID!=="undefined")
- {
- transformedData["datasetDetails"][datasetIndex].knowledgeBaseID=newKnowledgeBaseID;
- }
-
- let updatedData = JSON.stringify(transformedData,null, 2);
- fs.writeFileSync(dataStoreFile, updatedData);
-
- return res.status(200).send({
- success: 'true',
- message: 'Dataset Updated successfuly',
- });
- }
- }
-
- addDataset(req, res) {
- const fs = require('fs');
- let rawdata = fs.readFileSync(dataStoreFile);
- let transformedData = JSON.parse(rawdata);
-
- const datasetDetails = {
- datasetID: parseInt(req.body.datasetID,10),
- datasetName: req.body.datasetName,
- fileName: req.body.fileName,
- datasetPLD: req.body.datasetPLD,
- knowledgeBaseID : req.body.knowledgeBaseID,
- isSparqlEndPoint: req.body.isSparqlEndPoint,
- sparqlEndPoint: req.body.sparqlEndPoint,
- lastAssessmentRequestID: req.body.lastAssessmentRequestID,
- expectedProgress: Number(req.body.expectedProgress),
- assessmentMetrics: req.body.assessmentMetrics
- };
- transformedData["datasetDetails"].push(datasetDetails);
- let updatedData = JSON.stringify(transformedData,null, 2);
- fs.writeFileSync(dataStoreFile, updatedData);
- return res.status(201).send({
- success: 'true',
- message: 'Dataset configuration details added successfully'
- });
- }
- removeDataset(req, res) {
- const fs = require('fs');
- let rawdata = fs.readFileSync(dataStoreFile);
- let transformedData = JSON.parse(rawdata);
- const datasetID = parseInt(req.query.datasetID, 10);
- let datasetFound;
- let datasetIndex;
- transformedData["datasetDetails"].map((dataset, index) => {
- if (dataset.datasetID === datasetID) {
- datasetFound = dataset;
- datasetIndex = index;
- }
- });
- if (!datasetFound) {
- return res.status(404).send({
- success: 'false',
- message: 'Dataset configuration details does not exist',
- });
- }
- else
- {
- transformedData["datasetDetails"].splice(datasetIndex, 1);
- let updatedData = JSON.stringify(transformedData,null, 2);
- fs.writeFileSync(dataStoreFile, updatedData);
-
- return res.status(200).send({
- success: 'true',
- message: 'Dataset deleted successfuly',
- });
- }
- }
- }
- const datasetController = new datasetsController();
- export default datasetController;
|