add controllers for import and export jobs
This commit is contained in:
59
backend/src/controllers/exportController.ts
Normal file
59
backend/src/controllers/exportController.ts
Normal file
@@ -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;
|
||||||
60
backend/src/controllers/importController.ts
Normal file
60
backend/src/controllers/importController.ts
Normal file
@@ -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;
|
||||||
@@ -1,24 +1,8 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
|
import ImportController from "../controllers/importController";
|
||||||
|
import ExportController from "../controllers/exportController";
|
||||||
|
|
||||||
export const register = (app: express.Application) => {
|
export const register = (app: express.Application) => {
|
||||||
// define a route handler for the default home page
|
app.use("/", new ImportController().router);
|
||||||
app.get("/", (req: any, res) => {
|
app.use("/", new ExportController().router);
|
||||||
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" );
|
|
||||||
// } );
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user