Skip to content

Commit 0ad82ed

Browse files
committed
Merge branch 'dev' of https://github.com/boostcampwm-2022/web27-Wabinar into feat/#23-S
2 parents 0e08354 + d2723c8 commit 0ad82ed

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

server/apis/user/controller.ts

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;

server/apis/user/service.ts

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)