From 3fe475d477c466556659b48c70eeac5153ff5b0e Mon Sep 17 00:00:00 2001 From: sabhas Date: Tue, 19 Oct 2021 15:11:15 +0000 Subject: [PATCH] fix: update sasjs drive controller from function base to class base --- src/controllers/sasjsDrive.ts | 29 ++++++++++------------------- src/routes/index.ts | 6 +++--- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/controllers/sasjsDrive.ts b/src/controllers/sasjsDrive.ts index 079335c..c443aad 100644 --- a/src/controllers/sasjsDrive.ts +++ b/src/controllers/sasjsDrive.ts @@ -1,24 +1,15 @@ import { fileExists, readFile, createFile } from '@sasjs/utils' -export const sasjsDrive = async ( - filePath: string, - action: string, - newFileContent?: string -) => { - let fileContent - const isFileExists = await fileExists(filePath) - if (isFileExists) { - switch (action) { - case 'read': - fileContent = await readFile(filePath) - return fileContent - case 'update': - if (newFileContent) { - await createFile(filePath, newFileContent) - } - break - default: - break +export class SASjsDriveController { + async readFile(filePath: string) { + if (await fileExists(filePath)) { + return await readFile(filePath) + } + } + + async updateFile(filePath: string, fileContent: string) { + if (await fileExists(filePath)) { + return await createFile(filePath, fileContent) } } } diff --git a/src/routes/index.ts b/src/routes/index.ts index ae3b72d..4d1d11c 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -4,7 +4,7 @@ import { createFileTree, getTreeExample, sasjsExecutor, - sasjsDrive, + SASjsDriveController, ExecutionController } from '../controllers' import { @@ -54,7 +54,7 @@ router.get('/SASjsApi/files', async (req, res) => { const filePath = path .join(getTmpFilesFolderPath(), req.query.filePath) .replace(new RegExp('/', 'g'), path.sep) - const fileContent = await sasjsDrive(filePath as string, 'read') + const fileContent = await new SASjsDriveController().readFile(filePath) res.status(200).send({ status: 'success', fileContent: fileContent }) } else { res.status(400).send({ @@ -68,7 +68,7 @@ router.post('/SASjsApi/files', async (req, res) => { const filePath = path .join(getTmpFilesFolderPath(), req.body.filePath) .replace(new RegExp('/', 'g'), path.sep) - await sasjsDrive(filePath, 'update', req.body.fileContent) + await new SASjsDriveController().updateFile(filePath, req.body.fileContent) res.status(200).send({ status: 'success' }) })