.
This commit is contained in:
parent
852527cc4e
commit
cb1e076a7b
@ -7,7 +7,7 @@ export interface IContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class ContextFactory {
|
export class ContextFactory {
|
||||||
private static instance: ContextFactory | null = null;
|
protected static instance: ContextFactory | null = null;
|
||||||
|
|
||||||
public static getInstance(): IContext {
|
public static getInstance(): IContext {
|
||||||
if (!ContextFactory.instance) {
|
if (!ContextFactory.instance) {
|
||||||
@ -20,9 +20,9 @@ export class ContextFactory {
|
|||||||
return ContextFactory.instance.context;
|
return ContextFactory.instance.context;
|
||||||
}
|
}
|
||||||
|
|
||||||
private context: IContext;
|
protected context: IContext;
|
||||||
|
|
||||||
private constructor(context: IContext) {
|
protected constructor(context: IContext) {
|
||||||
this.context = context;
|
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 "./ExpressController";
|
||||||
export * from "./ExpressErrorResponse";
|
export * from "./ExpressErrorResponse";
|
||||||
export * from "./PassportStrategyController";
|
|
||||||
export * from "./middlewares";
|
export * from "./middlewares";
|
||||||
|
export * from "./PassportStrategyController";
|
||||||
|
|||||||
@ -1,34 +1,29 @@
|
|||||||
import { IRepositoryManager, RepositoryManager } from "@/contexts/common/domain";
|
import { ContextFactory, IContext } from "@/contexts/common/infrastructure";
|
||||||
import {
|
|
||||||
ISequelizeAdapter,
|
|
||||||
createSequelizeAdapter,
|
|
||||||
} from "@/contexts/common/infrastructure/sequelize";
|
|
||||||
import { config } from "../../../config";
|
import { config } from "../../../config";
|
||||||
|
|
||||||
export interface IProfileContext {
|
export interface IProfileContext extends IContext {
|
||||||
adapter: ISequelizeAdapter;
|
|
||||||
repositoryManager: IRepositoryManager;
|
|
||||||
defaults: Record<string, any>;
|
defaults: Record<string, any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProfileContext {
|
export class ProfileContext extends ContextFactory {
|
||||||
private static instance: ProfileContext | null = null;
|
protected static instance: ProfileContext | null = null;
|
||||||
|
|
||||||
public static getInstance(): IProfileContext {
|
public static getInstance(): IProfileContext {
|
||||||
if (!ProfileContext.instance) {
|
if (!ProfileContext.instance) {
|
||||||
ProfileContext.instance = new ProfileContext({
|
try {
|
||||||
adapter: createSequelizeAdapter(),
|
ProfileContext.instance = new ProfileContext({
|
||||||
repositoryManager: RepositoryManager.getInstance(),
|
...ContextFactory.getInstance(), // Reutilizamos el contexto de la clase base
|
||||||
defaults: config.defaults,
|
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) {
|
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 { registerProfileRepository } from "../../../Profile.repository";
|
||||||
import { UploadProfileLogoController } from "./UploadProfileLogo.controller";
|
import { UploadProfileLogoController } from "./UploadProfileLogo.controller";
|
||||||
|
|
||||||
export const createUpdloadProfileLogoController = (context: IProfileContext) => {
|
export const createUploadProfileLogoController = (context: IProfileContext) => {
|
||||||
registerProfileRepository(context);
|
registerProfileRepository(context);
|
||||||
|
|
||||||
return new UploadProfileLogoController(
|
return new UploadProfileLogoController(
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
import { checkUser } from "@/contexts/auth";
|
import { checkUser } from "@/contexts/auth";
|
||||||
|
import { handleRequest } from "@/contexts/common/infrastructure/express";
|
||||||
import {
|
import {
|
||||||
createGetProfileController,
|
createGetProfileController,
|
||||||
createUpdateProfileController,
|
createUpdateProfileController,
|
||||||
ProfileContext,
|
ProfileContext,
|
||||||
} from "@/contexts/profile/infrastructure";
|
} from "@/contexts/profile/infrastructure";
|
||||||
import { createGetProfileLogoController } from "@/contexts/profile/infrastructure/express/controllers/getProfileLogo";
|
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 { NextFunction, Request, Response, Router } from "express";
|
||||||
|
import multer from "multer";
|
||||||
import { createMulterMiddleware } from "../upload.middleware";
|
import { createMulterMiddleware } from "../upload.middleware";
|
||||||
|
|
||||||
const uploadProfileLogo = createMulterMiddleware({
|
const uploadProfileLogo = createMulterMiddleware({
|
||||||
@ -18,29 +20,33 @@ const uploadProfileLogo = createMulterMiddleware({
|
|||||||
export const profileRouter = (appRouter: Router) => {
|
export const profileRouter = (appRouter: Router) => {
|
||||||
const profileRoutes: Router = Router({ mergeParams: true });
|
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();
|
res.locals["context"] = ProfileContext.getInstance();
|
||||||
return next();
|
next();
|
||||||
});
|
};
|
||||||
|
|
||||||
profileRoutes.get("/", checkUser, (req: Request, res: Response, next: NextFunction) =>
|
profileRoutes.use(profileContextMiddleware);
|
||||||
createGetProfileController(res.locals["context"]).execute(req, res, next)
|
|
||||||
);
|
|
||||||
|
|
||||||
profileRoutes.put("/", checkUser, (req: Request, res: Response, next: NextFunction) =>
|
profileRoutes.get("/", checkUser, handleRequest(createGetProfileController));
|
||||||
createUpdateProfileController(res.locals["context"]).execute(req, res, next)
|
profileRoutes.put("/", checkUser, handleRequest(createUpdateProfileController));
|
||||||
);
|
profileRoutes.get("/logo", checkUser, handleRequest(createGetProfileLogoController));
|
||||||
|
|
||||||
profileRoutes.get("/logo", checkUser, (req: Request, res: Response, next: NextFunction) =>
|
|
||||||
createGetProfileLogoController(res.locals["context"]).execute(req, res, next)
|
|
||||||
);
|
|
||||||
|
|
||||||
profileRoutes.post(
|
profileRoutes.post(
|
||||||
"/logo",
|
"/logo",
|
||||||
checkUser,
|
checkUser,
|
||||||
uploadProfileLogo.single("logo"),
|
(req, res, next) => {
|
||||||
(req: Request, res: Response, next: NextFunction) =>
|
uploadProfileLogo.single("logo")(req, res, (err) => {
|
||||||
createUpdloadProfileLogoController(res.locals["context"]).execute(req, res, next)
|
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);
|
appRouter.use("/profile", profileRoutes);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user