crud and http controller
This commit is contained in:
parent
cba12be4b1
commit
c27c061c15
51 changed files with 930 additions and 387 deletions
79
server/src/core/controllers/app.ts
Normal file
79
server/src/core/controllers/app.ts
Normal file
|
@ -0,0 +1,79 @@
|
|||
import express from "express";
|
||||
import { Routes } from "../interfaces/router";
|
||||
import cors from "cors";
|
||||
import locator from "../di/register_di";
|
||||
import { DevEnv, UnitTestEnv } from "../di/env";
|
||||
import mongoose from "mongoose";
|
||||
import http from "http";
|
||||
import { Server } from "socket.io";
|
||||
|
||||
export class App {
|
||||
public app: express.Application;
|
||||
public port: number;
|
||||
public env: string;
|
||||
public computedFolder: string;
|
||||
// public io:
|
||||
constructor(routes: Routes[], computedFolder: string) {
|
||||
this.app = express();
|
||||
this.port = Number(process.env.PORT) || 3000;
|
||||
this.env = "dev";
|
||||
this.initializeMiddlewares();
|
||||
this.initializeRoutes(routes);
|
||||
this.loadAppDependencies();
|
||||
this.computedFolder = computedFolder;
|
||||
}
|
||||
|
||||
public listen() {
|
||||
const httpServer = new http.Server(this.app);
|
||||
// const io = new Server(httpServer);
|
||||
|
||||
httpServer.listen(this.port, () => {
|
||||
console.info(`=================================`);
|
||||
console.info(`======= ENV: ${this.env} =======`);
|
||||
console.info(`🚀 HTTP http://localhost:${this.port}`);
|
||||
console.info(`🚀 WS ws://localhost:${this.port}`);
|
||||
console.info(`=================================`);
|
||||
});
|
||||
// io.on("connection", (socket) => {
|
||||
// socket.on("disconnect", function (msg) {
|
||||
// console.log("Disconnected");
|
||||
// });
|
||||
// });
|
||||
|
||||
// setInterval(function () {
|
||||
// io.emit("goodbye");
|
||||
// console.log(200);
|
||||
// }, 1000);
|
||||
|
||||
}
|
||||
|
||||
public getServer() {
|
||||
return this.app;
|
||||
}
|
||||
|
||||
private initializeMiddlewares() {
|
||||
this.app.use(cors());
|
||||
this.app.use(express.json());
|
||||
this.app.use(express.urlencoded({ extended: true }));
|
||||
}
|
||||
|
||||
private initializeRoutes(routes: Routes[]) {
|
||||
routes.forEach((route) => {
|
||||
this.app.use("/", route.router);
|
||||
});
|
||||
}
|
||||
loadAppDependencies() {
|
||||
locator(
|
||||
this.env == "development"
|
||||
? new DevEnv(this.computedFolder)
|
||||
: new UnitTestEnv(this.computedFolder)
|
||||
);
|
||||
|
||||
mongoose
|
||||
.connect("mongodb://127.0.0.1:27017/test")
|
||||
.then(() => console.log("Connected!"))
|
||||
.catch((e) => {
|
||||
console.log("ERROR:", e);
|
||||
});
|
||||
}
|
||||
}
|
34
server/src/core/controllers/crud_controller.ts
Normal file
34
server/src/core/controllers/crud_controller.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { IRouteModel } from "../interfaces/router";
|
||||
import { CreateDataBaseModelUseCase } from "../usecases/create_database_model_usecase";
|
||||
import { DeleteDataBaseModelUseCase } from "../usecases/delete_database_model_usecase";
|
||||
import { PaginationDataBaseModelUseCase } from "../usecases/pagination_database_model_usecase";
|
||||
import { UpdateDataBaseModelUseCase } from "../usecases/update_database_model_usecase";
|
||||
|
||||
import { CoreHttpController } from "./http_controller";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
export class CrudController<V, D> extends CoreHttpController<V> {
|
||||
dataBaseModel: mongoose.Model<D>;
|
||||
|
||||
constructor(routerModel: IRouteModel) {
|
||||
super(routerModel);
|
||||
this.url = "/" + routerModel.url;
|
||||
this.validationModel = routerModel.validationModel;
|
||||
this.dataBaseModel = routerModel.databaseModel;
|
||||
this.init();
|
||||
}
|
||||
init() {
|
||||
this.routes["POST"] = new CreateDataBaseModelUseCase<D>(
|
||||
this.dataBaseModel
|
||||
).call;
|
||||
this.routes["GET"] = new PaginationDataBaseModelUseCase<D>(
|
||||
this.dataBaseModel
|
||||
).call;
|
||||
this.routes["DELETE"] = new DeleteDataBaseModelUseCase<D>(
|
||||
this.dataBaseModel
|
||||
).call;
|
||||
this.routes["PUT"] = new UpdateDataBaseModelUseCase<V, D>(
|
||||
this.dataBaseModel
|
||||
).call;
|
||||
}
|
||||
}
|
115
server/src/core/controllers/http_controller.ts
Normal file
115
server/src/core/controllers/http_controller.ts
Normal file
|
@ -0,0 +1,115 @@
|
|||
import { validationModelMiddleware } from "../middlewares/validation_model";
|
||||
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>>;
|
||||
|
||||
abstract class ICoreHttpController {
|
||||
abstract url: string;
|
||||
public router = Router();
|
||||
abstract call(): Routes;
|
||||
}
|
||||
|
||||
export class CoreHttpController<V> implements ICoreHttpController {
|
||||
url: string;
|
||||
validationModel: any;
|
||||
|
||||
routes = {
|
||||
POST: null,
|
||||
GET: null,
|
||||
DELETE: null,
|
||||
PUT: null,
|
||||
};
|
||||
|
||||
public router = Router();
|
||||
|
||||
constructor(routerModel: IRouteModel) {
|
||||
this.url = "/" + routerModel.url;
|
||||
this.validationModel = routerModel.validationModel;
|
||||
}
|
||||
|
||||
call(): Routes {
|
||||
if (this.routes["POST"] != null) {
|
||||
this.router.post(
|
||||
this.url,
|
||||
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.requestResponseController<V>(req, res, this.routes["DELETE"])
|
||||
);
|
||||
}
|
||||
if (this.routes["PUT"] != null) {
|
||||
this.router.put(
|
||||
this.url,
|
||||
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.requestResponseController<V>(req, res, this.routes["GET"])
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
router: this.router,
|
||||
};
|
||||
}
|
||||
public put(usecase: CallBackFunction<V>) {
|
||||
this.routes["PUT"] = usecase;
|
||||
}
|
||||
public delete(usecase: CallBackFunction<V>) {
|
||||
this.routes["DELETE"] = usecase;
|
||||
}
|
||||
private async requestResponseController<T>(
|
||||
req: Request,
|
||||
res: Response,
|
||||
usecase: CallBackFunction<T>
|
||||
) {
|
||||
let payload = null
|
||||
|
||||
if (req["model"] != undefined) {
|
||||
payload = req.body as T
|
||||
|
||||
}
|
||||
|
||||
if (req.query.page !== undefined) {
|
||||
payload = String(req.query.page)
|
||||
|
||||
}
|
||||
|
||||
if (req.query.id !== undefined) {
|
||||
payload = String(req.query.id)
|
||||
}
|
||||
|
||||
|
||||
|
||||
(await usecase(payload)).fold(
|
||||
(ok) => {
|
||||
res.json(ok);
|
||||
return;
|
||||
},
|
||||
(err) => {
|
||||
res.status(400).json(err);
|
||||
return;
|
||||
}
|
||||
);
|
||||
}
|
||||
public post(usecase: CallBackFunction<V>) {
|
||||
this.routes["POST"] = usecase;
|
||||
}
|
||||
|
||||
public get(usecase: CallBackFunction<V>) {
|
||||
this.routes["GET"] = usecase;
|
||||
}
|
||||
}
|
||||
|
15
server/src/core/controllers/socket_controller.ts
Normal file
15
server/src/core/controllers/socket_controller.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import path from "path";
|
||||
import { TypedEvent } from "../helper/typed_event";
|
||||
import { StackService } from "../services/stack_service";
|
||||
// TODO(IDONTSUDO): up to do
|
||||
|
||||
class SocketController<T>{
|
||||
emitter:TypedEvent<T>;
|
||||
constructor(emitter:TypedEvent<T>, ){
|
||||
this.emitter = emitter
|
||||
}
|
||||
call = () =>{
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue