From 5c7e45b60b5764c9186ffaae0483a628e1ff0c7f Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 14 Feb 2020 21:06:16 +0100 Subject: [PATCH] add controllers for import and export jobs --- backend/src/controllers/exportController.ts | 59 ++++++++++++++++++++ backend/src/controllers/importController.ts | 60 +++++++++++++++++++++ backend/src/routes/index.ts | 24 ++------- 3 files changed, 123 insertions(+), 20 deletions(-) create mode 100644 backend/src/controllers/exportController.ts create mode 100644 backend/src/controllers/importController.ts diff --git a/backend/src/controllers/exportController.ts b/backend/src/controllers/exportController.ts new file mode 100644 index 0000000..5c38567 --- /dev/null +++ b/backend/src/controllers/exportController.ts @@ -0,0 +1,59 @@ +import * as express from 'express'; +import {ExportJob, ExportJobsList} from "../interfaces" +import {PROCESSING_TIME_EXPORT} from "../config"; + +class ExportController { + public path = '/export'; + public router = express.Router(); + + private exportJobs: ExportJob[] = []; + + constructor() { + this.initializeRoutes(); + } + + public initializeRoutes() { + this.router.get(this.path, this.getAllExportJobs); + this.router.post(this.path, this.addExportJob); + } + + getAllExportJobs = (request: express.Request, response: express.Response) => { + const result: ExportJobsList = { + pending: [], + finished: [] + }; + + this.exportJobs.forEach((exportJob:ExportJob) => { + if (exportJob.state === "pending"){ + result.pending.push(exportJob); + }else if (exportJob.state === "finished"){ + result.finished.push(exportJob); + } + }); + + response.send(result); + }; + + addExportJob = (request: express.Request, response: express.Response) => { + const receivedExportJob = request.body; + const fullExportJob:ExportJob = { + bookId: receivedExportJob.bookId, + type: receivedExportJob.type, + state: "pending", + created_at: new Date(), + updated_at: new Date() + }; + this.exportJobs.push(fullExportJob); + + const jobIndexToUpdate:number = this.exportJobs.length-1; + const timeout:number = PROCESSING_TIME_EXPORT[fullExportJob.type]*1000; + setTimeout((jobIndex:number) => { + this.exportJobs[jobIndex].state = "finished"; + this.exportJobs[jobIndex].updated_at = new Date(); + }, timeout, jobIndexToUpdate); + + response.send(); + } +} + +export default ExportController; diff --git a/backend/src/controllers/importController.ts b/backend/src/controllers/importController.ts new file mode 100644 index 0000000..dcbbfbf --- /dev/null +++ b/backend/src/controllers/importController.ts @@ -0,0 +1,60 @@ +import * as express from 'express'; +import {ImportJobsList, ImportJob} from "../interfaces" +import {PROCESSING_TIME_IMPORT_ANY} from "../config"; + +class ImportController { + public path = '/import'; + public router = express.Router(); + + private importJobs: ImportJob[] = []; + + constructor() { + this.initializeRoutes(); + } + + public initializeRoutes() { + this.router.get(this.path, this.getAllImportJobs); + this.router.post(this.path, this.addImportJob); + } + + getAllImportJobs = (request: express.Request, response: express.Response) => { + const result: ImportJobsList = { + pending: [], + finished: [] + }; + + this.importJobs.forEach((importJob:ImportJob) => { + if (importJob.state === "pending"){ + result.pending.push(importJob); + }else if (importJob.state === "finished"){ + result.finished.push(importJob); + } + }); + + response.send(result); + }; + + addImportJob = (request: express.Request, response: express.Response) => { + const receivedImportJob = request.body; + const fullImportJob:ImportJob = { + bookId: receivedImportJob.bookId, + type: receivedImportJob.type, + url: receivedImportJob.url, + state: "pending", + created_at: new Date(), + updated_at: new Date() + }; + this.importJobs.push(fullImportJob); + + const jobIndexToUpdate:number = this.importJobs.length-1; + const timeout:number = PROCESSING_TIME_IMPORT_ANY*1000; + setTimeout((jobIndex:number) => { + this.importJobs[jobIndex].state = "finished"; + this.importJobs[jobIndex].updated_at = new Date(); + }, timeout, jobIndexToUpdate); + + response.send(); + } +} + +export default ImportController; diff --git a/backend/src/routes/index.ts b/backend/src/routes/index.ts index 33149e7..66c96d7 100644 --- a/backend/src/routes/index.ts +++ b/backend/src/routes/index.ts @@ -1,24 +1,8 @@ import * as express from "express"; +import ImportController from "../controllers/importController"; +import ExportController from "../controllers/exportController"; export const register = (app: express.Application) => { - // define a route handler for the default home page - app.get("/", (req: any, res) => { - res.send("Welcome Home"); - }); - - // // define a secure route handler for the login page that redirects to /guitars - // app.get( "/login", oidc.ensureAuthenticated(), ( req, res ) => { - // res.redirect( "/guitars" ); - // } ); - // - // // define a route to handle logout - // app.get( "/logout", ( req: any, res ) => { - // req.logout(); - // res.redirect( "/" ); - // } ); - // - // // define a secure route handler for the guitars page - // app.get( "/guitars", oidc.ensureAuthenticated(), ( req: any, res ) => { - // res.render( "guitars" ); - // } ); + app.use("/", new ImportController().router); + app.use("/", new ExportController().router); };