change http controller
This commit is contained in:
parent
fa645dde92
commit
f746039ee5
4 changed files with 123 additions and 21 deletions
|
@ -3,7 +3,7 @@ import { Result } from "../helper/result";
|
|||
import { Router, Request, Response } from "express";
|
||||
import { IRouteModel, Routes } from "../interfaces/router";
|
||||
|
||||
type Method =
|
||||
export type Method =
|
||||
| "all"
|
||||
| "get"
|
||||
| "post"
|
||||
|
@ -15,16 +15,33 @@ type Method =
|
|||
|
||||
export type ResponseBase = Promise<Result<any, any>>;
|
||||
|
||||
// TODO(IDONTSUDO): rewrite the router for the strategy
|
||||
export type CallbackStrategyWithEmpty = () => ResponseBase;
|
||||
export type CallbackStrategyWithValidationModel<T> = (a: T) => ResponseBase;
|
||||
export type CallbackStrategyWithIdQuery = (id: string) => ResponseBase;
|
||||
export type CallBackStrategyWithQueryPage = (page: string) => ResponseBase;
|
||||
export type CallbackStrategyWithFileUpload = (file: File) => ResponseBase;
|
||||
export abstract class CallbackStrategyWithEmpty {
|
||||
abstract call(): ResponseBase;
|
||||
}
|
||||
export abstract class CallbackStrategyWithValidationModel<T> {
|
||||
abstract call(a: T): ResponseBase;
|
||||
}
|
||||
export abstract class CallbackStrategyWithIdQuery {
|
||||
abstract call(id: string): ResponseBase;
|
||||
}
|
||||
export abstract class CallBackStrategyWithQueryPage {
|
||||
abstract call(page: string): ResponseBase;
|
||||
}
|
||||
|
||||
export abstract class CallbackStrategyWithFileUpload {
|
||||
abstract checkingFileExpression: RegExp;
|
||||
abstract call(file: File): ResponseBase;
|
||||
}
|
||||
|
||||
interface ISubSetFeatureRouter<T> {
|
||||
method: Method;
|
||||
fn: CallbackStrategyWithValidationModel<T>;
|
||||
subUrl: string;
|
||||
fn:
|
||||
| CallbackStrategyWithValidationModel<T>
|
||||
| CallbackStrategyWithEmpty
|
||||
| CallbackStrategyWithIdQuery
|
||||
| CallBackStrategyWithQueryPage
|
||||
| CallbackStrategyWithFileUpload;
|
||||
}
|
||||
|
||||
abstract class ICoreHttpController {
|
||||
|
@ -36,7 +53,7 @@ abstract class ICoreHttpController {
|
|||
export class CoreHttpController<V> implements ICoreHttpController {
|
||||
mainURL: string;
|
||||
validationModel: any;
|
||||
subRoutes: ISubSetFeatureRouter<any>[] = [];
|
||||
subRoutes: ISubSetFeatureRouter<V>[] = [];
|
||||
|
||||
routes = {
|
||||
POST: null,
|
||||
|
@ -51,13 +68,70 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
|||
this.mainURL = "/" + routerModel.url;
|
||||
this.validationModel = routerModel.validationModel;
|
||||
}
|
||||
|
||||
async responseHelper(res: Response, fn: ResponseBase) {
|
||||
(await fn).fold(
|
||||
(ok) => {
|
||||
res.json(ok);
|
||||
return;
|
||||
},
|
||||
(err) => {
|
||||
res.status(400).json(err);
|
||||
return;
|
||||
}
|
||||
);
|
||||
}
|
||||
call(): Routes {
|
||||
if (this.subRoutes.isNotEmpty()) {
|
||||
this.subRoutes.map((el) => {
|
||||
console.log(this.router[el.method]);
|
||||
this.router[el.method](
|
||||
this.mainURL + "/" + el.subUrl,
|
||||
async (req, res) => {
|
||||
if (el.fn instanceof CallbackStrategyWithValidationModel) {
|
||||
// TODO(IDONTSUDO):
|
||||
throw Error("needs to be implimed");
|
||||
}
|
||||
if (el.fn instanceof CallbackStrategyWithIdQuery) {
|
||||
throw Error("needs to be implimed");
|
||||
}
|
||||
if (el.fn instanceof CallBackStrategyWithQueryPage) {
|
||||
throw Error("needs to be implimed");
|
||||
}
|
||||
if (el.fn instanceof CallbackStrategyWithEmpty) {
|
||||
await this.responseHelper(res, el.fn.call());
|
||||
return;
|
||||
}
|
||||
|
||||
if (el.fn instanceof CallbackStrategyWithFileUpload) {
|
||||
if (req["files"] === undefined) {
|
||||
res.status(400).json("need files to form-data request");
|
||||
return;
|
||||
}
|
||||
if (req["files"]["file"] === undefined) {
|
||||
res.status(400).json("need file to form data request");
|
||||
return;
|
||||
}
|
||||
if (el.fn instanceof CallbackStrategyWithFileUpload) {
|
||||
if (
|
||||
!el.fn.checkingFileExpression.test(
|
||||
req["files"]["file"]["name"]
|
||||
)
|
||||
) {
|
||||
res
|
||||
.status(400)
|
||||
.json(
|
||||
"a file with this extension is expected: " +
|
||||
String(el.fn.checkingFileExpression)
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await this.responseHelper(res, el.fn.call(req["files"]["file"]));
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
if (this.routes["POST"] != null) {
|
||||
this.router.post(
|
||||
this.mainURL,
|
||||
|
@ -101,7 +175,7 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
|||
usecase: CallbackStrategyWithValidationModel<T>
|
||||
) {
|
||||
let payload = null;
|
||||
|
||||
const useCase = usecase as any;
|
||||
if (req["model"] != undefined) {
|
||||
payload = req.body as T;
|
||||
}
|
||||
|
@ -113,7 +187,7 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
|||
if (req.query.id !== undefined) {
|
||||
payload = String(req.query.id);
|
||||
}
|
||||
(await usecase(payload)).fold(
|
||||
(await useCase(payload)).fold(
|
||||
(ok) => {
|
||||
res.json(ok);
|
||||
return;
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
import { CrudController } from "../../core/controllers/crud_controller";
|
||||
import {
|
||||
CallbackStrategyWithEmpty,
|
||||
ResponseBase,
|
||||
} from "../../core/controllers/http_controller";
|
||||
import { Result } from "../../core/helper/result";
|
||||
import { CreateNewProjectInstanceScenario } from "./create_new_project_scenario";
|
||||
import {
|
||||
ProjectInstanceDbModel,
|
||||
ProjectInstanceValidationModel,
|
||||
} from "./project_instance_model";
|
||||
import { UploadCadFileToProjectUseCase } from "./upload_file_to_project_usecase";
|
||||
|
||||
export class ProjectInstancePresentation extends CrudController<
|
||||
ProjectInstanceValidationModel,
|
||||
|
@ -16,8 +22,19 @@ export class ProjectInstancePresentation extends CrudController<
|
|||
databaseModel: ProjectInstanceDbModel,
|
||||
});
|
||||
super.post(new CreateNewProjectInstanceScenario().call);
|
||||
// super.router.post(this.mainURL + "/file", (req, res) => {
|
||||
// TODO:
|
||||
// });
|
||||
|
||||
super.subRoutes = [
|
||||
{
|
||||
method: "post",
|
||||
subUrl: "upload",
|
||||
fn: new TestUseCase(),
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class TestUseCase extends CallbackStrategyWithEmpty {
|
||||
async call(): ResponseBase {
|
||||
return Result.ok(200);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import {
|
||||
CallbackStrategyWithFileUpload,
|
||||
ResponseBase,
|
||||
} from "../../core/controllers/http_controller";
|
||||
import { Result } from "../../core/helper/result";
|
||||
|
||||
export class UploadCadFileToProjectUseCase extends CallbackStrategyWithFileUpload {
|
||||
checkingFileExpression: RegExp = RegExp('.FCStd')
|
||||
async call(file: File): ResponseBase {
|
||||
|
||||
return Result.ok("200");
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import { TestCore } from "./core/test_core";
|
||||
import { UnitTestEnv } from "../src/core/di/env";
|
||||
// import { UnitTestEnv } from "../src/core/di/env";
|
||||
import { dirname } from "path";
|
||||
import locator from "../src/core/di/register_di";
|
||||
// import locator from "../src/core/di/register_di";
|
||||
import { ExecutorProgramServiceTest } from "./services/executor_program_service_test";
|
||||
import { FilesChangerTest } from "./services/files_change_notifier_service_test";
|
||||
import { TriggerServiceTest } from "./services/trigger_service_test";
|
||||
|
@ -19,9 +19,7 @@ const testCore = TestCore.instance;
|
|||
export const dirname__: string = dirname(__filename);
|
||||
export const assert = testCore.assert;
|
||||
export const resultTest = testCore.resultTest;
|
||||
const env = new UnitTestEnv(dirname__);
|
||||
|
||||
locator(env);
|
||||
|
||||
|
||||
const tests = [
|
||||
CreateDataBaseModelUseCaseTest,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue