Anousone Worlakoumman commited on
Commit
2d6721b
·
1 Parent(s): 3f1ce87

init checkout

Browse files
Files changed (4) hide show
  1. cmd/checkout.go +84 -9
  2. cmd/exec.go +1 -1
  3. go.mod +1 -3
  4. go.sum +2 -7
cmd/checkout.go CHANGED
@@ -1,15 +1,17 @@
1
  package cmd
2
 
3
  import (
 
4
  "log/slog"
5
  "net/http"
6
 
7
- sq "github.com/Masterminds/squirrel"
8
  "github.com/pocketbase/pocketbase/core"
 
9
  )
10
 
11
  type CheckoutReq struct {
12
- CartItems []string `json:"cartItems"`
13
  }
14
 
15
  func Checkout(e *core.RequestEvent) error {
@@ -23,21 +25,94 @@ func Checkout(e *core.RequestEvent) error {
23
 
24
  err := app.RunInTransaction(func(txApp core.App) error {
25
  // rawQuery := "Update FROM carts set status = purchase WHERE id in ['x001', 'x002', 'x003']"
26
-
27
- query, err := sq.Update("carts").
28
- Set("status", "purchase").
29
- Where(sq.Eq{"id": req.CartItems}).
30
- MustSql()
 
 
 
 
 
 
 
31
  if err != nil {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  }
33
 
34
- if _, err := txApp.DB().NewQuery(query).Execute(); err != nil {
35
- return err
 
 
 
 
 
 
 
36
  }
37
 
 
 
 
 
 
 
 
 
 
38
  return nil
39
  })
40
  if err != nil {
 
41
  return e.JSON(500, "")
42
  }
43
 
 
1
  package cmd
2
 
3
  import (
4
+ "fmt"
5
  "log/slog"
6
  "net/http"
7
 
8
+ "github.com/pocketbase/dbx"
9
  "github.com/pocketbase/pocketbase/core"
10
+ "github.com/shopspring/decimal"
11
  )
12
 
13
  type CheckoutReq struct {
14
+ CartItems []any `json:"cartItems"`
15
  }
16
 
17
  func Checkout(e *core.RequestEvent) error {
 
25
 
26
  err := app.RunInTransaction(func(txApp core.App) error {
27
  // rawQuery := "Update FROM carts set status = purchase WHERE id in ['x001', 'x002', 'x003']"
28
+ // query, args, err := sq.Update("carts").
29
+ // Set("status", "purchased").
30
+ // Where(sq.Eq{"id": req.CartItems}).
31
+ // ToSql()
32
+ _, err := txApp.DB().
33
+ Update(
34
+ "carts",
35
+ dbx.Params{"status": "purchased"},
36
+ // dbx.NewExp("LOWER([[id]]) in {:ids}", dbx.Params{"ids": req.CartItems}),
37
+ dbx.HashExp{"id": req.CartItems},
38
+ ).
39
+ Execute()
40
  if err != nil {
41
+ slog.Error("sq.Update()", "error", err)
42
+ return e.JSON(500, "")
43
+ }
44
+
45
+ cartsInfo := []struct {
46
+ CustomerID string `db:"customer_id" json:"customerId"`
47
+ ProductID string `db:"product_id" json:"productId"`
48
+ ProductName string `db:"name" json:"productName"`
49
+ PriceLAK decimal.Decimal `db:"price" json:"priceLAK"`
50
+ PriceUSD decimal.Decimal `db:"-" json:"priceUSD"`
51
+ PriceTHB decimal.Decimal `db:"-" json:"priceTHB"`
52
+ Quantity int `db:"quantity" json:"quantity"`
53
+ }{}
54
+
55
+ if err := txApp.DB().
56
+ Select(
57
+ "c.customer_id",
58
+ "c.product_id",
59
+ "c.quantity",
60
+ "p.name",
61
+ "p.price",
62
+ ).
63
+ From("carts c").
64
+ InnerJoin("products p", dbx.NewExp("p.id = c.product_id")).
65
+ AndWhere(dbx.HashExp{"c.id": req.CartItems}).
66
+ OrderBy("c.created ASC").
67
+ All(&cartsInfo); err != nil {
68
+ slog.Error("listCart", "error", err)
69
+ return e.JSON(500, "")
70
+ }
71
+
72
+ currency := []struct {
73
+ Ccy string `db:"ccy" json:"ccy"`
74
+ Type string `db:"type" json:"type"`
75
+ Rate decimal.Decimal `db:"rate" json:"rate"`
76
+ }{}
77
+
78
+ if err := txApp.DB().
79
+ Select(
80
+ "ccy",
81
+ "type",
82
+ "rate",
83
+ ).
84
+ From("currency").
85
+ AndWhere(dbx.HashExp{"type": "SELL"}).
86
+ OrderBy("created ASC").
87
+ All(&currency); err != nil {
88
+ slog.Error("listCurrency", "error", err)
89
+ return e.JSON(500, "")
90
  }
91
 
92
+ for i := range cartsInfo {
93
+ for _, item := range currency {
94
+ switch item.Ccy {
95
+ case "USD":
96
+ cartsInfo[i].PriceUSD = cartsInfo[i].PriceLAK.Div(item.Rate).Round(2)
97
+ case "THB":
98
+ cartsInfo[i].PriceTHB = cartsInfo[i].PriceLAK.Div(item.Rate).Round(2)
99
+ }
100
+ }
101
  }
102
 
103
+ fmt.Printf("carts: %+v\n", cartsInfo)
104
+
105
+ fmt.Printf("currency: %+v\n", currency)
106
+
107
+ // if _, err := txApp.DB().NewQuery(query).Execute(); err != nil {
108
+ // slog.Error("txApp.DB()", "error", err)
109
+ // return err
110
+ // }
111
+
112
  return nil
113
  })
114
  if err != nil {
115
+ slog.Error("app.RunInTransaction()", "error", err)
116
  return e.JSON(500, "")
117
  }
118
 
cmd/exec.go CHANGED
@@ -125,7 +125,7 @@ func Execute(app *pocketbase.PocketBase) (*pocketbase.PocketBase, error) {
125
  bo := se.Router.Group("").Bind(apis.RequireAuth("users"))
126
  bo.GET("/order-list", ListOrders)
127
 
128
- customer := se.Router.Group("/cust").Unbind("customers")
129
  customer.POST("/checkout", Checkout)
130
 
131
  return se.Next()
 
125
  bo := se.Router.Group("").Bind(apis.RequireAuth("users"))
126
  bo.GET("/order-list", ListOrders)
127
 
128
+ customer := se.Router.Group("/cust").Bind(apis.RequireAuth("users"))
129
  customer.POST("/checkout", Checkout)
130
 
131
  return se.Next()
go.mod CHANGED
@@ -9,12 +9,12 @@ require (
9
  github.com/pact-foundation/pact-go/v2 v2.4.1
10
  github.com/pocketbase/dbx v1.11.0
11
  github.com/pocketbase/pocketbase v0.27.0
 
12
  github.com/stretchr/testify v1.10.0
13
  golang.org/x/sync v0.13.0
14
  )
15
 
16
  require (
17
- github.com/Masterminds/squirrel v1.5.4 // indirect
18
  github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
19
  github.com/davecgh/go-spew v1.1.1 // indirect
20
  github.com/disintegration/imaging v1.6.2 // indirect
@@ -28,8 +28,6 @@ require (
28
  github.com/hashicorp/go-version v1.7.0 // indirect
29
  github.com/hashicorp/logutils v1.0.0 // indirect
30
  github.com/inconshreveable/mousetrap v1.1.0 // indirect
31
- github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
32
- github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
33
  github.com/mattn/go-colorable v0.1.14 // indirect
34
  github.com/mattn/go-isatty v0.0.20 // indirect
35
  github.com/ncruces/go-strftime v0.1.9 // indirect
 
9
  github.com/pact-foundation/pact-go/v2 v2.4.1
10
  github.com/pocketbase/dbx v1.11.0
11
  github.com/pocketbase/pocketbase v0.27.0
12
+ github.com/shopspring/decimal v1.4.0
13
  github.com/stretchr/testify v1.10.0
14
  golang.org/x/sync v0.13.0
15
  )
16
 
17
  require (
 
18
  github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
19
  github.com/davecgh/go-spew v1.1.1 // indirect
20
  github.com/disintegration/imaging v1.6.2 // indirect
 
28
  github.com/hashicorp/go-version v1.7.0 // indirect
29
  github.com/hashicorp/logutils v1.0.0 // indirect
30
  github.com/inconshreveable/mousetrap v1.1.0 // indirect
 
 
31
  github.com/mattn/go-colorable v0.1.14 // indirect
32
  github.com/mattn/go-isatty v0.0.20 // indirect
33
  github.com/ncruces/go-strftime v0.1.9 // indirect
go.sum CHANGED
@@ -1,7 +1,5 @@
1
  cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2
  github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3
- github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
4
- github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
5
  github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
6
  github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
7
  github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
@@ -107,10 +105,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
107
  github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
108
  github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
109
  github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
110
- github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
111
- github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
112
- github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
113
- github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
114
  github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
115
  github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
116
  github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
@@ -140,6 +134,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq
140
  github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
141
  github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
142
  github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 
 
143
  github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
144
  github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
145
  github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
@@ -153,7 +149,6 @@ github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
153
  github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
154
  github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
155
  github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
156
- github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
157
  github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
158
  github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
159
  github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 
1
  cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2
  github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 
 
3
  github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
4
  github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
5
  github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
 
105
  github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
106
  github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
107
  github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 
 
 
 
108
  github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
109
  github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
110
  github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
 
134
  github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
135
  github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
136
  github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
137
+ github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
138
+ github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
139
  github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
140
  github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
141
  github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
 
149
  github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
150
  github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
151
  github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
 
152
  github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
153
  github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
154
  github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=