This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (181 loc) · 5.99 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/tigrisdata/tigris-client-go/fields"
"github.com/tigrisdata/tigris-client-go/filter"
"github.com/tigrisdata/tigris-client-go/search"
"github.com/tigrisdata/tigris-client-go/tigris"
)
type User struct {
Id int32 `tigris:"primaryKey,autoGenerate"`
Name string
Balance float64
}
type Order struct {
Id int32 `tigris:"primaryKey,autoGenerate"`
UserId int32
Products []Product
}
type Product struct {
Id int32 `tigris:"primaryKey,autoGenerate"`
Name string
Quantity int
Price float64
}
func setupReadRoute[T interface{}](r *gin.Engine, db *tigris.Database, name string) {
r.GET(fmt.Sprintf("/%s/read/:id", name), func(c *gin.Context) {
coll := tigris.GetCollection[T](db)
u, err := coll.ReadOne(c, filter.Eq("Id", c.Param("id")))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, u)
})
}
func setupCRUDRoutes[T interface{}](r *gin.Engine, db *tigris.Database, name string) {
setupReadRoute[T](r, db, name)
setupSearchRoute[T](r, db, name)
r.POST(fmt.Sprintf("/%s/create", name), func(c *gin.Context) {
coll := tigris.GetCollection[T](db)
var u T
if err := c.Bind(&u); err != nil {
return
}
if _, err := coll.Insert(c, &u); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, u)
})
r.DELETE(fmt.Sprintf("/%s/delete/:id", name), func(c *gin.Context) {
coll := tigris.GetCollection[T](db)
if _, err := coll.Delete(c, filter.Eq("Id", c.Param("id"))); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, gin.H{"Status": "DELETED"})
})
}
// Create an order in a transaction,
// taking into account user balances and product stock.
func setupCreateOrderRoute(r *gin.Engine, db *tigris.Database) {
r.POST("/orders/create", func(c *gin.Context) {
var o Order
// Read the request body into o
if err := c.Bind(&o); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Perform the read, update and insert on the users, products and orders
// collections in a transaction. If the function passed to db.Tx
// returns an error then the transaction will be automatically rolled back.
// If no error is returned, the transaction will be automatically committed.
err := db.Tx(c, func(txCtx context.Context) error {
// Fetch an object of the users collection
users := tigris.GetCollection[User](db)
// Read the user with order's UserId
u, err := users.ReadOne(txCtx, filter.Eq("Id", o.UserId))
if err != nil {
return err
}
// Fetch an object of the products collection
products := tigris.GetCollection[Product](db)
orderTotal := 0.0
// For every product in the order
for i := 0; i < len(o.Products); i++ {
v := &o.Products[i]
// Fetch the product in the order from the collection
p, err := products.ReadOne(txCtx, filter.Eq("Id", v.Id))
if err != nil {
return err
}
// Verify that product quantity in the inventory is more than the
// product quantity in the order
if p.Quantity < v.Quantity {
return fmt.Errorf("low stock on product %v", p.Name)
}
// Update the inventory to reduce the product quantity based on the
// quantity in the order
if _, err = products.Update(txCtx,
filter.Eq("Id", v.Id),
fields.Set("Quantity", p.Quantity-v.Quantity)); err != nil {
return err
}
orderTotal += p.Price * float64(v.Quantity)
// Remember purchase price in the being created order
v.Price = p.Price
}
// Verify that the user has enough balance to be able to support the
// order purchase
if orderTotal > u.Balance {
return fmt.Errorf("low balance. order total %v", orderTotal)
}
// Update the user's balance to account for the order purchase
if _, err = users.Update(txCtx,
filter.Eq("Id", o.UserId),
fields.Set("Balance", u.Balance-orderTotal)); err != nil {
return err
}
orders := tigris.GetCollection[Order](db)
// Create the order
_, err = orders.Insert(txCtx, &o)
// Returning no error means that the transaction will be committed.
return err
})
// If there is no error returned then the transaction was successfully
// committed and the data has been consistently updated in Tigris.
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, o)
})
}
// Create routes for searching data in a collection
func setupSearchRoute[T interface{}](r *gin.Engine, db *tigris.Database, name string) {
r.POST(fmt.Sprintf("/%s/search", name), func(c *gin.Context) {
coll := tigris.GetCollection[T](db)
var u search.Request
if err := c.Bind(&u); err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
it, err := coll.Search(c, &u)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
r := &search.Result[T]{}
for it.Next(r) {
c.JSON(http.StatusOK, r)
}
if err := it.Err(); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
})
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Cloud config
// tigrisCfg := tigris.Config{URL: "api.preview.tigrisdata.cloud:443", ClientID: "your-tigris-app-id", ClientSecret: "your-tigris-app-secret", Project: "shop"}
// Local config
tigrisCfg := tigris.Config{URL: "localhost:8081", Project: "shop"}
db, err := tigris.OpenDatabase(ctx, &tigrisCfg, &User{}, &Product{}, &Order{})
if err != nil {
panic(err)
}
r := gin.Default()
setupCRUDRoutes[User](r, db, "users")
setupCRUDRoutes[Product](r, db, "products")
setupReadRoute[Order](r, db, "orders")
setupCreateOrderRoute(r, db)
if err := r.Run("localhost:8080"); err != nil {
panic(err)
}
}