11import { Application , Request , Response } from 'express'
2- import { UserService } from '../.. /services/UserService'
2+ import { UserService } from '@ /services/UserService'
33import { UserPresenter } from '../presenters/UserPresenter'
44
55export default ( app : Application , userService : UserService ) => {
6+ /**
7+ * @swagger
8+ * /api/users:
9+ * post:
10+ * summary: Cria um novo usuário
11+ * requestBody:
12+ * required: true
13+ * content:
14+ * application/json:
15+ * schema:
16+ * type: object
17+ * properties:
18+ * name:
19+ * type: string
20+ * responses:
21+ * '201':
22+ * description: Mensagem de sucesso
23+ */
24+ app . post ( '/api/users' , async ( req : Request , res : Response ) => {
25+ try {
26+ const user = await userService . createUser ( req . body )
27+ res . status ( 201 ) . json ( UserPresenter . toResponse ( user ) )
28+ } catch ( error ) {
29+ res . status ( 500 ) . json ( { error : 'Internal Server Error' } )
30+ }
31+ } )
32+
33+ /**
34+ * @swagger
35+ * /api/users:
36+ * get:
37+ * summary: Busca todos os usuários cadastrados
38+ * responses:
39+ * '200':
40+ * description: Mensagem de sucesso
41+ */
42+ app . get ( '/api/users' , async ( req : Request , res : Response ) => {
43+ try {
44+ const users = await userService . getUsers ( )
45+ if ( ! users || users . length === 0 ) {
46+ return res . status ( 404 ) . json ( { error : 'No users found' } )
47+ }
48+ res . json ( UserPresenter . toResponseArray ( users ) )
49+ } catch ( error ) {
50+ res . status ( 500 ) . json ( { error : 'Internal Server Error' } )
51+ }
52+ } )
53+
54+ /**
55+ * @swagger
56+ * /api/users/{id}:
57+ * get:
58+ * summary: Verifica se existe usuário com o ID informado
59+ * parameters:
60+ * - in: path
61+ * name: id
62+ * required: true
63+ * description: ID do usuário a ser consultado
64+ * schema:
65+ * type: string
66+ * responses:
67+ * '200':
68+ * description: Mensagem de sucesso
69+ */
670 app . get ( '/api/users/:id' , async ( req : Request , res : Response ) => {
771 const { id } = req . params
872 try {
@@ -16,22 +80,68 @@ export default (app: Application, userService: UserService) => {
1680 }
1781 } )
1882
19- app . get ( '/api/users' , async ( req : Request , res : Response ) => {
83+ /**
84+ * @swagger
85+ * /api/users/{id}:
86+ * put:
87+ * summary: Atualiza informações do usuário com o ID informado
88+ * parameters:
89+ * - in: path
90+ * name: id
91+ * required: true
92+ * description: ID do usuário a ser atualizado
93+ * schema:
94+ * type: string
95+ * requestBody:
96+ * required: true
97+ * content:
98+ * application/json:
99+ * schema:
100+ * type: object
101+ * properties:
102+ * name:
103+ * type: string
104+ * responses:
105+ * '200':
106+ * description: Mensagem de sucesso
107+ */
108+ app . put ( '/api/users/:id' , async ( req : Request , res : Response ) => {
109+ const { id } = req . params
20110 try {
21- const users = await userService . getUsers ( )
22- if ( ! users || users . length === 0 ) {
23- return res . status ( 404 ) . json ( { error : 'No users found' } )
111+ const user = await userService . putUserById ( id , req . body )
112+ if ( ! user ) {
113+ return res . status ( 404 ) . json ( { error : 'User not found' } )
24114 }
25- res . json ( UserPresenter . toResponseArray ( users ) )
115+ res . json ( UserPresenter . toResponse ( req . body ) )
26116 } catch ( error ) {
27117 res . status ( 500 ) . json ( { error : 'Internal Server Error' } )
28118 }
29119 } )
30120
31- app . post ( '/api/users' , async ( req : Request , res : Response ) => {
121+ /**
122+ * @swagger
123+ * /api/users/{id}:
124+ * delete:
125+ * summary: Deleta usuário com o ID informado
126+ * parameters:
127+ * - in: path
128+ * name: id
129+ * required: true
130+ * description: ID do usuário a ser deletado
131+ * schema:
132+ * type: string
133+ * responses:
134+ * '200':
135+ * description: Mensagem de sucesso
136+ */
137+ app . delete ( '/api/users/:id' , async ( req : Request , res : Response ) => {
138+ const { id } = req . params
32139 try {
33- const user = await userService . createUser ( req . body )
34- res . status ( 201 ) . json ( UserPresenter . toResponse ( user ) )
140+ const user = await userService . deleteUserById ( id )
141+ if ( ! user ) {
142+ return res . status ( 404 ) . json ( { error : 'User not found' } )
143+ }
144+ res . json ( UserPresenter . toResponse ( user ) )
35145 } catch ( error ) {
36146 res . status ( 500 ) . json ( { error : 'Internal Server Error' } )
37147 }
0 commit comments