-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathuser.controller.ts
More file actions
88 lines (77 loc) · 2.09 KB
/
Copy pathuser.controller.ts
File metadata and controls
88 lines (77 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Request, Response, NextFunction } from 'express';
import { UserService } from '../services/user.service';
import {
createUserSchema,
updateUserSchema,
userIdSchema,
} from '../utils/validation';
import { AuthRequest } from '../middleware/auth.middleware';
const userService = new UserService();
export class UserController {
async createUser(req: AuthRequest, res: Response, next: NextFunction) {
try {
if (!req.userId) {
return res.status(401).json({
success: false,
error: 'Unauthorized',
});
}
const data = createUserSchema.parse(req.body);
const user = await userService.createUser(req.userId, data);
return res.status(201).json({
success: true,
data: user,
});
} catch (error) {
next(error);
}
}
async getUsers(_req: Request, res: Response, next: NextFunction) {
try {
const data = await userService.getUsers();
return res.status(200).json({
success: true,
data,
});
} catch (error) {
next(error);
}
}
async getUser(req: Request, res: Response, next: NextFunction) {
try {
const { id } = userIdSchema.parse(req.params);
const user = await userService.getUserById(id);
return res.status(200).json({
success: true,
data: user,
});
} catch (error) {
next(error);
}
}
async updateUser(req: Request, res: Response, next: NextFunction) {
try {
const { id } = userIdSchema.parse(req.params);
const data = updateUserSchema.parse(req.body);
const user = await userService.updateUser(id, data);
return res.status(200).json({
success: true,
data: user,
});
} catch (error) {
next(error);
}
}
async deleteUser(req: Request, res: Response, next: NextFunction) {
try {
const { id } = userIdSchema.parse(req.params);
const result = await userService.deleteUser(id);
return res.status(200).json({
success: true,
data: result,
});
} catch (error) {
next(error);
}
}
}