-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.go
151 lines (133 loc) · 3.67 KB
/
order.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
type ProductQuantity struct {
ProductId int `json:"product_id"`
Quantity int `json:"quantity"`
}
func postOrderHandler(c *gin.Context, db *sql.DB) {
userId, err := authorization(c, db)
if err != nil {
return
}
var orderId int
err = db.QueryRow(`INSERT INTO orders (user_id,created_at) VALUES($1,$2) RETURNING id`,
userId, time.Now().Format(time.RFC3339)).Scan(&orderId)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
rows, err := db.Query(`SELECT product_id,quantity FROM cart WHERE user_id = $1`, userId)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var productId, quantity int
productQuantitys := []ProductQuantity{}
for rows.Next() {
err = rows.Scan(&productId, &quantity)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
productQuantity := ProductQuantity{
ProductId: productId,
Quantity: quantity,
}
productQuantitys = append(productQuantitys, productQuantity)
}
for _, productQuantity := range productQuantitys {
_, err = db.Exec(`INSERT INTO order_products (order_id, product_id, quantity) VALUES ($1, $2, $3)`,
orderId, productQuantity.ProductId, productQuantity.Quantity)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
}
_, err = db.Exec(`DELETE FROM cart WHERE user_id= $1`, userId)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusCreated)
}
type OrderResponse struct {
ID int `json:"id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Products []OrderProduct `json:"products"`
}
type OrderProduct struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Image string `json:"image,omitempty"`
Price int `json:"price,omitempty"`
Quantity int `json:"quantity,omitempty"`
}
func getOrdersHandler(c *gin.Context, db *sql.DB) {
userId, err := authorization(c, db)
if err != nil {
return
}
rows, err := db.Query(`SELECT id, created_at FROM orders WHERE user_id=$1`, userId)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var Id int
var createdAt time.Time
orderResponses := []OrderResponse{}
for rows.Next() {
err = rows.Scan(&Id, &createdAt)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
orderResponse := OrderResponse{
ID: Id,
CreatedAt: createdAt,
}
orderResponses = append(orderResponses, orderResponse)
}
for i, orderResponse := range orderResponses {
rows, err := db.Query(`SELECT products.id, products.name, products.image, products.price, order_products.quantity
FROM order_products JOIN products ON order_products.product_id= products.id WHERE order_id =$1`,
orderResponse.ID)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
orderProducts := []OrderProduct{}
for rows.Next() {
var id, price, quantity int
var name, image string
err = rows.Scan(&id, &name, &image, &price, &quantity)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
orderProduct := OrderProduct{
Id: id,
Name: name,
Image: image,
Price: price,
Quantity: quantity,
}
orderProducts = append(orderProducts, orderProduct)
}
orderResponses[i].Products = orderProducts
}
c.JSON(http.StatusOK, orderResponses)
}