-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.go
81 lines (73 loc) · 1.56 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
package controllers
import (
"database/sql"
"errors"
"github.com/astaxie/beedb"
"github.com/astaxie/beego"
_ "github.com/go-sql-driver/mysql"
)
type Shopping interface {
Add(name string, qty int) string
Remove(name string)
}
type Cart struct {
Items map[string]Item
}
type Item struct {
Id string
Name string
Price string
Qty int
}
func getPrice(name string) int {
return len(name) * 10
}
func getItem(id string) (EcsGoods, error) {
var i EcsGoods
sqlUser := beego.AppConfig.String("mysqluser")
sqlPass := beego.AppConfig.String("mysqlpass")
sqlUrl := beego.AppConfig.String("mysqlurls")
sqlDb := beego.AppConfig.String("mysqldb")
dataSourceName := sqlUser + ":" + sqlPass + "@tcp(" + sqlUrl + ")/" + sqlDb
db, err := sql.Open("mysql", dataSourceName+"?charset=utf8")
if err != nil {
return i, err
}
beedb.OnDebug = true
orm := beedb.New(db)
orm.Where("goods_id=?", id).Find(&i)
if i.GoodsName == "" {
return i, errors.New("Cannot find item")
} else {
return i, nil
}
}
func (c *Cart) Add(id string, qty int) string {
beego.Trace("Add:", id, qty)
item, err := getItem(id)
if err != nil {
beego.Error(err.Error())
return err.Error()
}
beego.Trace(item.GoodsName, item.ShopPrice)
b, ok := c.Items[item.GoodsId]
if !ok {
b = Item{
Id: item.GoodsId,
Name: item.GoodsName,
Price: item.ShopPrice,
Qty: qty,
}
} else {
b.Qty += qty
}
c.Items[id] = b
return "Success add to cart."
}
func (c *Cart) Remove(id string) {
_, ok := c.Items[id]
beego.Trace("Remove:", id, " ok:", ok)
if ok {
delete(c.Items, id)
}
}