-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.go
126 lines (106 loc) · 2.74 KB
/
cart.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
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
type Cart struct {
ProductId int `json:"product_id,omitempty"`
UserId int `json:"user_id,omitempty"`
Quantity int `json:"quantity,omitempty"`
}
type CartProduct 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 postToCartHandler(c *gin.Context, db *sql.DB) {
userId, err := authorization(c, db)
if err != nil {
return
}
cart := Cart{}
err = c.BindJSON(&cart)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
var count int
row := db.QueryRow(`SELECT count(*) FROM cart WHERE user_id = $1 AND product_id = $2`, userId, cart.ProductId)
err = row.Scan(&count)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if count == 1 {
c.Status(http.StatusExpectationFailed)
return
}
_, err = db.Exec(`INSERT INTO cart(product_id,user_id,Quantity)VALUES($1,$2,$3)`, cart.ProductId, userId, 1)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusCreated)
}
func getCartHandler(c *gin.Context, db *sql.DB) {
userId, err := authorization(c, db)
if err != nil {
return
}
rows, err := db.Query(`SELECT products.id,products.name,products.image,products.price,cart.quantity FROM
products JOIN cart ON cart.product_id = products.id WHERE cart.user_id = $1
ORDER BY products.id ASC`, userId)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
cartProducts := []CartProduct{}
for rows.Next() {
var id, price, quantity int
var image, name string
err = rows.Scan(&id, &name, &image, &price, &quantity)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
cartProduct := CartProduct{
Id: id,
Name: name,
Image: image,
Price: price,
Quantity: quantity,
}
cartProducts = append(cartProducts, cartProduct)
}
c.JSON(http.StatusOK, cartProducts)
}
func updateCartHandler(c *gin.Context, db *sql.DB) {
userId, err := authorization(c, db)
if err != nil {
return
}
cart := Cart{}
err = c.BindJSON(&cart)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
_, err = db.Exec(`UPDATE cart SET quantity= $1 WHERE product_id=$2 AND user_id= $3`,
cart.Quantity, cart.ProductId, userId)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusNoContent)
}