.
This commit is contained in:
parent
852527cc4e
commit
cb1e076a7b
@ -7,7 +7,7 @@ export interface IContext {
|
||||
}
|
||||
|
||||
export class ContextFactory {
|
||||
private static instance: ContextFactory | null = null;
|
||||
protected static instance: ContextFactory | null = null;
|
||||
|
||||
public static getInstance(): IContext {
|
||||
if (!ContextFactory.instance) {
|
||||
@ -20,9 +20,9 @@ export class ContextFactory {
|
||||
return ContextFactory.instance.context;
|
||||
}
|
||||
|
||||
private context: IContext;
|
||||
protected context: IContext;
|
||||
|
||||
private constructor(context: IContext) {
|
||||
protected constructor(context: IContext) {
|
||||
this.context = context;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,12 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { IContext } from "../ContextFactory";
|
||||
import { ExpressController } from "./ExpressController";
|
||||
|
||||
export type ControllerFactory<T extends IContext> = (context: T) => ExpressController;
|
||||
|
||||
export const handleRequest =
|
||||
<T extends IContext>(controllerFactory: ControllerFactory<T>) =>
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
const context: T = res.locals["context"];
|
||||
return controllerFactory(context).execute(req, res, next);
|
||||
};
|
||||
@ -1,4 +1,5 @@
|
||||
export * from "./ControllerFactory";
|
||||
export * from "./ExpressController";
|
||||
export * from "./ExpressErrorResponse";
|
||||
export * from "./PassportStrategyController";
|
||||
export * from "./middlewares";
|
||||
export * from "./PassportStrategyController";
|
||||
|
||||
@ -1,34 +1,29 @@
|
||||
import { IRepositoryManager, RepositoryManager } from "@/contexts/common/domain";
|
||||
import {
|
||||
ISequelizeAdapter,
|
||||
createSequelizeAdapter,
|
||||
} from "@/contexts/common/infrastructure/sequelize";
|
||||
import { ContextFactory, IContext } from "@/contexts/common/infrastructure";
|
||||
import { config } from "../../../config";
|
||||
|
||||
export interface IProfileContext {
|
||||
adapter: ISequelizeAdapter;
|
||||
repositoryManager: IRepositoryManager;
|
||||
export interface IProfileContext extends IContext {
|
||||
defaults: Record<string, any>;
|
||||
}
|
||||
|
||||
export class ProfileContext {
|
||||
private static instance: ProfileContext | null = null;
|
||||
export class ProfileContext extends ContextFactory {
|
||||
protected static instance: ProfileContext | null = null;
|
||||
|
||||
public static getInstance(): IProfileContext {
|
||||
if (!ProfileContext.instance) {
|
||||
ProfileContext.instance = new ProfileContext({
|
||||
adapter: createSequelizeAdapter(),
|
||||
repositoryManager: RepositoryManager.getInstance(),
|
||||
defaults: config.defaults,
|
||||
});
|
||||
try {
|
||||
ProfileContext.instance = new ProfileContext({
|
||||
...ContextFactory.getInstance(), // Reutilizamos el contexto de la clase base
|
||||
defaults: config.defaults, // Agregamos los valores específicos de ProfileContext
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
throw new Error(`Error initializing ProfileContext: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return ProfileContext.instance.context;
|
||||
return ProfileContext.instance.context as IProfileContext;
|
||||
}
|
||||
|
||||
private context: IProfileContext;
|
||||
|
||||
private constructor(context: IProfileContext) {
|
||||
this.context = context;
|
||||
super(context); // Llamamos al constructor de la clase base
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ import { IProfileContext } from "../../../Profile.context";
|
||||
import { registerProfileRepository } from "../../../Profile.repository";
|
||||
import { UploadProfileLogoController } from "./UploadProfileLogo.controller";
|
||||
|
||||
export const createUpdloadProfileLogoController = (context: IProfileContext) => {
|
||||
export const createUploadProfileLogoController = (context: IProfileContext) => {
|
||||
registerProfileRepository(context);
|
||||
|
||||
return new UploadProfileLogoController(
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
import { checkUser } from "@/contexts/auth";
|
||||
import { handleRequest } from "@/contexts/common/infrastructure/express";
|
||||
import {
|
||||
createGetProfileController,
|
||||
createUpdateProfileController,
|
||||
ProfileContext,
|
||||
} from "@/contexts/profile/infrastructure";
|
||||
import { createGetProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/getProfileLogo";
|
||||
import { createUpdloadProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/uploadProfileLogo";
|
||||
import { createUploadProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/uploadProfileLogo";
|
||||
import { NextFunction, Request, Response, Router } from "express";
|
||||
import multer from "multer";
|
||||
import { createMulterMiddleware } from "../upload.middleware";
|
||||
|
||||
const uploadProfileLogo = createMulterMiddleware({
|
||||
@ -18,29 +20,33 @@ const uploadProfileLogo = createMulterMiddleware({
|
||||
export const profileRouter = (appRouter: Router) => {
|
||||
const profileRoutes: Router = Router({ mergeParams: true });
|
||||
|
||||
profileRoutes.use((req: Request, res: Response, next: NextFunction) => {
|
||||
const profileContextMiddleware = (req: Request, res: Response, next: NextFunction) => {
|
||||
res.locals["context"] = ProfileContext.getInstance();
|
||||
return next();
|
||||
});
|
||||
next();
|
||||
};
|
||||
|
||||
profileRoutes.get("/", checkUser, (req: Request, res: Response, next: NextFunction) =>
|
||||
createGetProfileController(res.locals["context"]).execute(req, res, next)
|
||||
);
|
||||
profileRoutes.use(profileContextMiddleware);
|
||||
|
||||
profileRoutes.put("/", checkUser, (req: Request, res: Response, next: NextFunction) =>
|
||||
createUpdateProfileController(res.locals["context"]).execute(req, res, next)
|
||||
);
|
||||
|
||||
profileRoutes.get("/logo", checkUser, (req: Request, res: Response, next: NextFunction) =>
|
||||
createGetProfileLogoController(res.locals["context"]).execute(req, res, next)
|
||||
);
|
||||
profileRoutes.get("/", checkUser, handleRequest(createGetProfileController));
|
||||
profileRoutes.put("/", checkUser, handleRequest(createUpdateProfileController));
|
||||
profileRoutes.get("/logo", checkUser, handleRequest(createGetProfileLogoController));
|
||||
|
||||
profileRoutes.post(
|
||||
"/logo",
|
||||
checkUser,
|
||||
uploadProfileLogo.single("logo"),
|
||||
(req: Request, res: Response, next: NextFunction) =>
|
||||
createUpdloadProfileLogoController(res.locals["context"]).execute(req, res, next)
|
||||
(req, res, next) => {
|
||||
uploadProfileLogo.single("logo")(req, res, (err) => {
|
||||
if (err instanceof multer.MulterError) {
|
||||
// Error relacionado con Multer (e.g. tamaño del archivo excedido)
|
||||
return res.status(400).json({ error: err.message });
|
||||
} else if (err) {
|
||||
// Otro tipo de error
|
||||
return res.status(500).json({ error: "Error uploading the file" });
|
||||
}
|
||||
next();
|
||||
});
|
||||
},
|
||||
handleRequest(createUploadProfileLogoController)
|
||||
);
|
||||
|
||||
appRouter.use("/profile", profileRoutes);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user