This commit is contained in:
IDONTSUDO 2023-11-16 00:40:35 +03:00
parent 8ecb036b1d
commit d70253d6a6
33 changed files with 201 additions and 81 deletions

View file

@ -84,7 +84,6 @@ export class App {
}
async appStartController() {
console.log(App.staticFilesStoreDir());
if (await dirIsExists(App.staticFilesStoreDir())) {
return;
}

View file

@ -12,7 +12,7 @@ export class CrudController<V, D> extends CoreHttpController<V> {
constructor(routerModel: IRouteModel) {
super(routerModel);
this.url = "/" + routerModel.url;
this.mainURL = "/" + routerModel.url;
this.validationModel = routerModel.validationModel;
this.dataBaseModel = routerModel.databaseModel;
this.init();

View file

@ -3,16 +3,33 @@ import { Result } from "../helper/result";
import { Router, Request, Response } from "express";
import { IRouteModel, Routes } from "../interfaces/router";
export type CallBackFunction<T> = (a: T) => Promise<Result<any, any>>;
type Method =
| "all"
| "get"
| "post"
| "put"
| "delete"
| "patch"
| "options"
| "head";
export type CallbackStrategyWithValidationModel<T> = (
a: T
) => Promise<Result<any, any>>;
// TODO(IDOTSUDO):NEED IMPLEMENTS
// interface ISubSetFeatureRouter<T>{
// method:Method,
// fn:CallbackStrategyWithValidationModel<T>
// }
abstract class ICoreHttpController {
abstract url: string;
abstract mainURL: string;
public router = Router();
abstract call(): Routes;
}
export class CoreHttpController<V> implements ICoreHttpController {
url: string;
mainURL: string;
validationModel: any;
routes = {
@ -25,35 +42,34 @@ export class CoreHttpController<V> implements ICoreHttpController {
public router = Router();
constructor(routerModel: IRouteModel) {
this.url = "/" + routerModel.url;
this.mainURL = "/" + routerModel.url;
this.validationModel = routerModel.validationModel;
}
call(): Routes {
if (this.routes["POST"] != null) {
this.router.post(
this.url,
this.mainURL,
validationModelMiddleware(this.validationModel),
(req, res) =>
this.requestResponseController<V>(req, res, this.routes["POST"])
);
}
if (this.routes["DELETE"] != null) {
this.router.delete(this.url, (req, res) =>
this.router.delete(this.mainURL, (req, res) =>
this.requestResponseController<V>(req, res, this.routes["DELETE"])
);
}
if (this.routes["PUT"] != null) {
this.router.put(
this.url,
this.mainURL,
validationModelMiddleware(this.validationModel),
(req, res) =>
this.requestResponseController<V>(req, res, this.routes["PUT"])
);
}
if (this.routes["GET"] != null) {
this.router.get(this.url, (req, res) =>
this.router.get(this.mainURL, (req, res) =>
this.requestResponseController<V>(req, res, this.routes["GET"])
);
}
@ -62,18 +78,17 @@ export class CoreHttpController<V> implements ICoreHttpController {
router: this.router,
};
}
public put(usecase: CallBackFunction<V>) {
public put(usecase: CallbackStrategyWithValidationModel<V>) {
this.routes["PUT"] = usecase;
}
public delete(usecase: CallBackFunction<V>) {
public delete(usecase: CallbackStrategyWithValidationModel<V>) {
this.routes["DELETE"] = usecase;
}
public async requestResponseController<T>(
req: Request,
res: Response,
usecase: CallBackFunction<T>
usecase: CallbackStrategyWithValidationModel<T>
) {
let payload = null;
if (req["model"] != undefined) {
@ -98,11 +113,11 @@ export class CoreHttpController<V> implements ICoreHttpController {
}
);
}
public post(usecase: CallBackFunction<V>) {
public post(usecase: CallbackStrategyWithValidationModel<V>) {
this.routes["POST"] = usecase;
}
public get(usecase: CallBackFunction<V>) {
public get(usecase: CallbackStrategyWithValidationModel<V>) {
this.routes["GET"] = usecase;
}
}