diff --git a/server/src/core/controllers/http_controller.ts b/server/src/core/controllers/http_controller.ts index f2cb1fb..f180a2d 100644 --- a/server/src/core/controllers/http_controller.ts +++ b/server/src/core/controllers/http_controller.ts @@ -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>; -// TODO(IDONTSUDO): rewrite the router for the strategy -export type CallbackStrategyWithEmpty = () => ResponseBase; -export type CallbackStrategyWithValidationModel = (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 { + 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 { method: Method; - fn: CallbackStrategyWithValidationModel; + subUrl: string; + fn: + | CallbackStrategyWithValidationModel + | CallbackStrategyWithEmpty + | CallbackStrategyWithIdQuery + | CallBackStrategyWithQueryPage + | CallbackStrategyWithFileUpload; } abstract class ICoreHttpController { @@ -36,7 +53,7 @@ abstract class ICoreHttpController { export class CoreHttpController implements ICoreHttpController { mainURL: string; validationModel: any; - subRoutes: ISubSetFeatureRouter[] = []; + subRoutes: ISubSetFeatureRouter[] = []; routes = { POST: null, @@ -51,13 +68,70 @@ export class CoreHttpController 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 implements ICoreHttpController { usecase: CallbackStrategyWithValidationModel ) { let payload = null; - + const useCase = usecase as any; if (req["model"] != undefined) { payload = req.body as T; } @@ -113,7 +187,7 @@ export class CoreHttpController 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; diff --git a/server/src/features/project_instance/project_instance_presentation.ts b/server/src/features/project_instance/project_instance_presentation.ts index 475f241..eb564ed 100644 --- a/server/src/features/project_instance/project_instance_presentation.ts +++ b/server/src/features/project_instance/project_instance_presentation.ts @@ -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); } } diff --git a/server/src/features/project_instance/upload_file_to_project_usecase.ts b/server/src/features/project_instance/upload_file_to_project_usecase.ts new file mode 100644 index 0000000..2a5cfb6 --- /dev/null +++ b/server/src/features/project_instance/upload_file_to_project_usecase.ts @@ -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"); + } +} diff --git a/server/test/test.ts b/server/test/test.ts index 87888d8..d113a1e 100644 --- a/server/test/test.ts +++ b/server/test/test.ts @@ -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,