add controllers for import and export jobs
This commit is contained in:
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;
|
||||
Reference in New Issue
Block a user