Skip to content

Commit d2723c8

Browse files
authored
Merge pull request #32 from dohun31/feat/#19-B
Feat/#19-B: GET /user/:id/workspace API ๊ตฌํ˜„
2 parents 382f692 + 6c62a63 commit d2723c8

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import express, { Request, Response, NextFunction } from 'express';
2+
import asyncWrapper from '@utils/async-wrapper';
3+
import * as userService from './service';
4+
import jwtAuthenticator from '@middlewares/jwt-authenticator';
5+
6+
const router = express.Router();
7+
8+
router.get(
9+
'/:id/workspace',
10+
jwtAuthenticator,
11+
asyncWrapper(async (req: Request, res: Response, next: NextFunction) => {
12+
const { id: userId } = req.user;
13+
const { id: targetUserId } = req.params;
14+
15+
const workspaces = await userService.getWorkspaces(
16+
Number(targetUserId),
17+
userId,
18+
);
19+
20+
res.send({ workspaces });
21+
}),
22+
);
23+
24+
export default router;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import userModel from '@apis/user/model';
2+
import workspaceModel from '@apis/workspace/model';
3+
import AuthorizationError from '@errors/authorization-error';
4+
5+
export const getWorkspaces = async (targetUserId: number, userId: number) => {
6+
if (targetUserId !== userId)
7+
throw new AuthorizationError(
8+
'์š”์ฒญํ•˜์‹  ์œ ์ € ์ •๋ณด์™€ ํ˜„์žฌ ๋กœ๊ทธ์ธ๋œ ์œ ์ € ์ •๋ณด๊ฐ€ ๋‹ฌ๋ผ์š” ^^',
9+
);
10+
11+
const user = await userModel.findOne({ id: targetUserId });
12+
13+
const workspaces = await workspaceModel.find({
14+
id: { $in: user.workspaces },
15+
});
16+
17+
return workspaces;
18+
};

โ€Žserver/index.tsโ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import cookieParser from 'cookie-parser';
33
import env from '@config';
44
import authRouter from '@apis/auth/controller';
55
import workspaceRouter from '@apis/workspace/controller';
6+
import userRouter from '@apis/user/controller';
67
import errorHandler from '@middlewares/error-handler';
78

89
const app = express();
@@ -12,6 +13,7 @@ app.use(cookieParser(env.COOKIE_SECRET_KEY));
1213
app.get('/', (req: Request, res: Response) => res.send('Express'));
1314
app.use('/auth', authRouter);
1415
app.use('/workspace', workspaceRouter);
16+
app.use('/user', userRouter);
1517

1618
app.use(errorHandler);
1719

0 commit comments

Comments
ย (0)