Skip to content

Commit a92847f

Browse files
committed
[#152] refactor: 기존 controller를 middleware로 수정
1 parent 774f84f commit a92847f

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

backend/src/middlewares/block.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Request, Response, NextFunction } from 'express';
2+
3+
import { blockService } from '@/services';
4+
import { transactionHandler } from '@/aops';
5+
import { BlockDoc } from '@/models';
6+
7+
export const create = transactionHandler(
8+
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
9+
const { parent, block } = await blockService.create({
10+
parentId: req.params.parentId,
11+
index: req.body.index,
12+
blockDTO: req.body.block,
13+
});
14+
15+
res.locals.result = { parent, block };
16+
next();
17+
},
18+
);
19+
20+
export const update = async (
21+
req: Request,
22+
res: Response,
23+
next: NextFunction,
24+
): Promise<void> => {
25+
const block = await blockService.update(req.params.blockId, req.body.block);
26+
res.locals.result = { block };
27+
next();
28+
};
29+
30+
export const move = transactionHandler(
31+
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
32+
const { block, from, to } = await blockService.move(
33+
req.params.blockId,
34+
req.params.toId,
35+
req.body.index,
36+
);
37+
res.locals.result = { block, from, to };
38+
next();
39+
},
40+
);
41+
42+
export const deleteCascade = transactionHandler(
43+
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
44+
const parent = await blockService.deleteCascade(req.params.blockId);
45+
res.locals.result = { parent };
46+
next();
47+
},
48+
);
49+
50+
export const createAndUpdate = transactionHandler(
51+
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
52+
const { create, update } = req.body;
53+
const { parent, block } = await blockService.create(create);
54+
let updated: BlockDoc = null;
55+
if (update) {
56+
updated = await blockService.update(update.blockId, update);
57+
}
58+
res.locals.result = { parent, block, updated };
59+
next();
60+
},
61+
);
62+
63+
export const deleteAndUpdate = transactionHandler(
64+
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
65+
const { deleteId, update } = req.body;
66+
const parent = await blockService.deleteOnly(deleteId);
67+
let updated: BlockDoc = null;
68+
if (update) {
69+
updated = await blockService.update(update.blockId, update);
70+
}
71+
res.locals.result = { parent, updated };
72+
next();
73+
},
74+
);

0 commit comments

Comments
 (0)