-
Notifications
You must be signed in to change notification settings - Fork 17
/
UserController.ts
76 lines (59 loc) · 1.75 KB
/
UserController.ts
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
import type { FastifyReply, FastifyRequest } from 'fastify'
import type {
CREATE_USER_BODY_SCHEMA_TYPE,
DELETE_USER_PARAMS_SCHEMA_TYPE,
GET_USER_PARAMS_SCHEMA_TYPE,
UPDATE_USER_BODY_SCHEMA_TYPE,
UPDATE_USER_PARAMS_SCHEMA_TYPE,
} from '../schemas/userSchemas.js'
export const postCreateUser = async (
req: FastifyRequest<{ Body: CREATE_USER_BODY_SCHEMA_TYPE }>,
reply: FastifyReply,
): Promise<void> => {
const { name, email, age } = req.body
const { userService } = req.diScope.cradle
const createdUser = await userService.createUser({
name,
email,
age,
})
return reply.status(201).send({
data: createdUser,
})
}
export const getUser = async (
req: FastifyRequest<{ Params: GET_USER_PARAMS_SCHEMA_TYPE }>,
reply: FastifyReply,
): Promise<void> => {
const { userId } = req.params
const { reqContext } = req
const { userService } = req.diScope.cradle
const user = await userService.getUser(reqContext, userId)
return reply.send({
data: user,
})
}
export const deleteUser = async (
req: FastifyRequest<{ Params: DELETE_USER_PARAMS_SCHEMA_TYPE }>,
reply: FastifyReply,
): Promise<void> => {
const { userId } = req.params
const { reqContext } = req
const { userService } = req.diScope.cradle
await userService.deleteUser(reqContext, userId)
return reply.status(204).send()
}
export const patchUpdateUser = async (
req: FastifyRequest<{
Params: UPDATE_USER_PARAMS_SCHEMA_TYPE
Body: UPDATE_USER_BODY_SCHEMA_TYPE
}>,
reply: FastifyReply,
): Promise<void> => {
const { userId } = req.params
const updatedUser = req.body
const { reqContext } = req
const { userService } = req.diScope.cradle
await userService.updateUser(reqContext, userId, updatedUser)
return reply.status(204).send()
}