This repository has been archived on 2020-08-13. You can view files and clone it, but cannot push or open issues or pull requests.
requestor/lib/KnexCrudHandler.ts
Timo Hocker 374940757a fixes
2020-04-17 08:31:14 +02:00

74 lines
1.8 KiB
TypeScript

import { Request, Response } from 'express';
import { http } from '@scode/consts';
import { try_parse_json } from '@scode/utilities';
import { KnexCrudOptions } from './KnexCrudOptions';
import { CrudHandler } from './CrudHandler';
export class KnexCrudHandler implements CrudHandler {
protected table: string;
protected columns: Array<string>;
protected options: KnexCrudOptions;
public constructor (
table: string,
columns: Array<string>,
options: KnexCrudOptions = {}
) {
this.table = table;
this.columns = columns;
this.options = options;
}
private call_auth (
auth?: Function,
req: Request,
res: Response
): Promise<boolean> {
if (typeof auth === 'undefined')
return true;
const promise = new Promise ((resolve) => {
const result = auth (req, res, resolve);
if (typeof result !== 'undefined')
resolve (result);
});
return promise;
}
protected validate_body (
req: Request,
res: Response
): Promise<object> | object {
if (typeof req.body === 'undefined') {
res.status (http.status_bad_request);
res.end ();
return null;
}
return try_parse_json (req.body);
}
public async create (req: Request, res: Response): Promise<void> {
if (!await this.call_auth (this.options.create_authentication, req, res))
return;
if (!await this.call_auth (this.options.create_authorization, req, res))
return;
const body_data = await this.validate_body (req, res);
if (body_data === null)
return;
}
public async read (req: Request, res: Response): Promise<void> {
}
public async update (req: Request, res: Response): Promise<void> {
}
public async delete (req: Request, res: Response): Promise<void> {
}
}