You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

45 lines
1.5 KiB

import Knex from "knex";
import { exportDB, importDB } from "../db/ImportExport";
import { EndpointError, EndpointHandler, handleErrorsInEndpoint } from "./types";
import * as api from '../../client/src/api/api';
import { DBExportEndpoint } from "../../client/src/api/api";
export const DBExport: EndpointHandler = async (req: any, res: any, knex: Knex) => {
try {
let db = await exportDB(req.user.id, knex);
res.status(200);
res.setHeader('Content-disposition', 'attachment; filename= mudbase.json');
res.setHeader('Content-type', 'application/json');
res.send( JSON.stringify(db, null, 2) );
} catch (e) {
handleErrorsInEndpoint(e)
}
}
export const DBImport: EndpointHandler = async (req: any, res: any, knex: Knex) => {
if (!api.checkDBImportRequest(req.body)) {
const e: EndpointError = {
name: "EndpointError",
message: 'Invalid DBImport request',
httpStatus: 400
};
throw e;
}
const reqObject: api.DBImportRequest = req.body;
const { id: userId } = req.user;
console.log("User ", userId, ": Import DB ");
try {
await importDB(userId, reqObject, knex)
res.status(200).send();
} catch (e) {
handleErrorsInEndpoint(e);
}
}
export const importExportEndpoints: [ string, string, boolean, EndpointHandler ][] = [
[ api.DBExportEndpoint, 'get', true, DBExport ],
[ api.DBImportEndpoint, 'post', true, DBImport ],
];