1
+ const mysql = require ( '../mysql' ) ;
2
+
3
+ exports . getOrders = async ( req , res , next ) => {
4
+ try {
5
+ const query = `SELECT orders.orderId,
6
+ orders.quantity,
7
+ products.productId,
8
+ products.name,
9
+ products.price
10
+ FROM orders
11
+ INNER JOIN products
12
+ ON products.productId = orders.productId;`
13
+ const result = await mysql . execute ( query ) ;
14
+ const response = {
15
+ orders : result . map ( order => {
16
+ return {
17
+ orderId : order . orderId ,
18
+ quantity : order . quantity ,
19
+ product : {
20
+ productId : order . productId ,
21
+ name : order . name ,
22
+ price : order . price
23
+ } ,
24
+ request : {
25
+ type : 'GET' ,
26
+ description : 'Retorna os detalhes de um pedido específico' ,
27
+ url : process . env . URL_API + 'orders/' + order . orderId
28
+ }
29
+ }
30
+ } )
31
+ }
32
+ return res . status ( 200 ) . send ( response ) ;
33
+
34
+ } catch ( error ) {
35
+ return res . status ( 500 ) . send ( { error : error } ) ;
36
+ }
37
+ } ;
38
+
39
+ exports . postOrder = async ( req , res , next ) => {
40
+
41
+
42
+ try {
43
+ const queryProduct = 'SELECT * FROM products WHERE productId = ?' ;
44
+ const resultProduct = await mysql . execute ( queryProduct , [ req . body . productId ] ) ;
45
+
46
+ if ( resultProduct . length == 0 ) {
47
+ return res . status ( 404 ) . send ( { message : 'Produto não encontrado' } ) ;
48
+ }
49
+
50
+ const queryOrder = 'INSERT INTO orders (productId, quantity) VALUES (?,?)' ;
51
+ const resultOrder = await mysql . execute ( queryOrder , [ req . body . productId , req . body . quantity ] ) ;
52
+
53
+ const response = {
54
+ message : 'Pedido inserido com sucesso' ,
55
+ createdOrder : {
56
+ orderId : resultOrder . insertId ,
57
+ productId : req . body . productId ,
58
+ quantity : req . body . quantity ,
59
+ request : {
60
+ type : 'GET' ,
61
+ description : 'Retorna todos os pedidos' ,
62
+ url : process . env . URL_API + 'orders'
63
+ }
64
+ }
65
+ }
66
+ return res . status ( 201 ) . send ( response ) ;
67
+
68
+ } catch ( error ) {
69
+ return res . status ( 500 ) . send ( { error : error } ) ;
70
+ }
71
+ } ;
72
+
73
+ exports . getOrderDetail = async ( req , res , next ) => {
74
+ try {
75
+ const query = 'SELECT * FROM orders WHERE orderId = ?;' ;
76
+ const result = await mysql . execute ( query , [ req . params . orderId ] ) ;
77
+
78
+ if ( result . length == 0 ) {
79
+ return res . status ( 404 ) . send ( {
80
+ message : 'Não foi encontrado pedido com este ID'
81
+ } )
82
+ }
83
+ const response = {
84
+ order : {
85
+ orderId : result [ 0 ] . orderId ,
86
+ productId : result [ 0 ] . productId ,
87
+ quantity : result [ 0 ] . quantity ,
88
+ request : {
89
+ type : 'GET' ,
90
+ description : 'Retorna todos os pedidos' ,
91
+ url : process . env . URL_API + 'orders'
92
+ }
93
+ }
94
+ }
95
+ return res . status ( 200 ) . send ( response ) ;
96
+
97
+ } catch ( error ) {
98
+ return res . status ( 500 ) . send ( { error : error } ) ;
99
+ }
100
+ } ;
101
+
102
+ exports . deleteOrder = async ( req , res , next ) => {
103
+ try {
104
+ const query = `DELETE FROM orders WHERE orderId = ?` ;
105
+ await mysql . execute ( query , [ req . params . orderId ] ) ;
106
+
107
+ const response = {
108
+ message : 'Pedido removido com sucesso' ,
109
+ request : {
110
+ type : 'POST' ,
111
+ description : 'Insere um pedido' ,
112
+ url : process . env . URL_API + 'orders' ,
113
+ body : {
114
+ productId : 'Number' ,
115
+ quantity : 'Number'
116
+ }
117
+ }
118
+ }
119
+ return res . status ( 202 ) . send ( response ) ;
120
+
121
+ } catch ( error ) {
122
+ return res . status ( 500 ) . send ( { error : error } ) ;
123
+ }
124
+ } ;
0 commit comments