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 { Router, Request, Response } from "express";
|
||||||
import { IRouteModel, Routes } from "../interfaces/router";
|
import { IRouteModel, Routes } from "../interfaces/router";
|
||||||
|
|
||||||
type Method =
|
export type Method =
|
||||||
| "all"
|
| "all"
|
||||||
| "get"
|
| "get"
|
||||||
| "post"
|
| "post"
|
||||||
|
@ -15,16 +15,33 @@ type Method =
|
||||||
|
|
||||||
export type ResponseBase = Promise<Result<any, any>>;
|
export type ResponseBase = Promise<Result<any, any>>;
|
||||||
|
|
||||||
// TODO(IDONTSUDO): rewrite the router for the strategy
|
export abstract class CallbackStrategyWithEmpty {
|
||||||
export type CallbackStrategyWithEmpty = () => ResponseBase;
|
abstract call(): ResponseBase;
|
||||||
export type CallbackStrategyWithValidationModel<T> = (a: T) => ResponseBase;
|
}
|
||||||
export type CallbackStrategyWithIdQuery = (id: string) => ResponseBase;
|
export abstract class CallbackStrategyWithValidationModel<T> {
|
||||||
export type CallBackStrategyWithQueryPage = (page: string) => ResponseBase;
|
abstract call(a: T): ResponseBase;
|
||||||
export type CallbackStrategyWithFileUpload = (file: File) => 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> {
|
interface ISubSetFeatureRouter<T> {
|
||||||
method: Method;
|
method: Method;
|
||||||
fn: CallbackStrategyWithValidationModel<T>;
|
subUrl: string;
|
||||||
|
fn:
|
||||||
|
| CallbackStrategyWithValidationModel<T>
|
||||||
|
| CallbackStrategyWithEmpty
|
||||||
|
| CallbackStrategyWithIdQuery
|
||||||
|
| CallBackStrategyWithQueryPage
|
||||||
|
| CallbackStrategyWithFileUpload;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class ICoreHttpController {
|
abstract class ICoreHttpController {
|
||||||
|
@ -36,7 +53,7 @@ abstract class ICoreHttpController {
|
||||||
export class CoreHttpController<V> implements ICoreHttpController {
|
export class CoreHttpController<V> implements ICoreHttpController {
|
||||||
mainURL: string;
|
mainURL: string;
|
||||||
validationModel: any;
|
validationModel: any;
|
||||||
subRoutes: ISubSetFeatureRouter<any>[] = [];
|
subRoutes: ISubSetFeatureRouter<V>[] = [];
|
||||||
|
|
||||||
routes = {
|
routes = {
|
||||||
POST: null,
|
POST: null,
|
||||||
|
@ -51,13 +68,70 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
||||||
this.mainURL = "/" + routerModel.url;
|
this.mainURL = "/" + routerModel.url;
|
||||||
this.validationModel = routerModel.validationModel;
|
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 {
|
call(): Routes {
|
||||||
if (this.subRoutes.isNotEmpty()) {
|
if (this.subRoutes.isNotEmpty()) {
|
||||||
this.subRoutes.map((el) => {
|
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) {
|
if (this.routes["POST"] != null) {
|
||||||
this.router.post(
|
this.router.post(
|
||||||
this.mainURL,
|
this.mainURL,
|
||||||
|
@ -101,7 +175,7 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
||||||
usecase: CallbackStrategyWithValidationModel<T>
|
usecase: CallbackStrategyWithValidationModel<T>
|
||||||
) {
|
) {
|
||||||
let payload = null;
|
let payload = null;
|
||||||
|
const useCase = usecase as any;
|
||||||
if (req["model"] != undefined) {
|
if (req["model"] != undefined) {
|
||||||
payload = req.body as T;
|
payload = req.body as T;
|
||||||
}
|
}
|
||||||
|
@ -113,7 +187,7 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
||||||
if (req.query.id !== undefined) {
|
if (req.query.id !== undefined) {
|
||||||
payload = String(req.query.id);
|
payload = String(req.query.id);
|
||||||
}
|
}
|
||||||
(await usecase(payload)).fold(
|
(await useCase(payload)).fold(
|
||||||
(ok) => {
|
(ok) => {
|
||||||
res.json(ok);
|
res.json(ok);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
import { CrudController } from "../../core/controllers/crud_controller";
|
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 { CreateNewProjectInstanceScenario } from "./create_new_project_scenario";
|
||||||
import {
|
import {
|
||||||
ProjectInstanceDbModel,
|
ProjectInstanceDbModel,
|
||||||
ProjectInstanceValidationModel,
|
ProjectInstanceValidationModel,
|
||||||
} from "./project_instance_model";
|
} from "./project_instance_model";
|
||||||
|
import { UploadCadFileToProjectUseCase } from "./upload_file_to_project_usecase";
|
||||||
|
|
||||||
export class ProjectInstancePresentation extends CrudController<
|
export class ProjectInstancePresentation extends CrudController<
|
||||||
ProjectInstanceValidationModel,
|
ProjectInstanceValidationModel,
|
||||||
|
@ -16,8 +22,19 @@ export class ProjectInstancePresentation extends CrudController<
|
||||||
databaseModel: ProjectInstanceDbModel,
|
databaseModel: ProjectInstanceDbModel,
|
||||||
});
|
});
|
||||||
super.post(new CreateNewProjectInstanceScenario().call);
|
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 { TestCore } from "./core/test_core";
|
||||||
import { UnitTestEnv } from "../src/core/di/env";
|
// import { UnitTestEnv } from "../src/core/di/env";
|
||||||
import { dirname } from "path";
|
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 { ExecutorProgramServiceTest } from "./services/executor_program_service_test";
|
||||||
import { FilesChangerTest } from "./services/files_change_notifier_service_test";
|
import { FilesChangerTest } from "./services/files_change_notifier_service_test";
|
||||||
import { TriggerServiceTest } from "./services/trigger_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 dirname__: string = dirname(__filename);
|
||||||
export const assert = testCore.assert;
|
export const assert = testCore.assert;
|
||||||
export const resultTest = testCore.resultTest;
|
export const resultTest = testCore.resultTest;
|
||||||
const env = new UnitTestEnv(dirname__);
|
|
||||||
|
|
||||||
locator(env);
|
|
||||||
|
|
||||||
const tests = [
|
const tests = [
|
||||||
CreateDataBaseModelUseCaseTest,
|
CreateDataBaseModelUseCaseTest,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue