This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
/
inventory.go
122 lines (109 loc) · 2.66 KB
/
inventory.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
package db
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"strconv"
"sync"
"github.com/OpenBazaar/openbazaar-go/repo"
)
type InventoryDB struct {
modelStore
}
func NewInventoryStore(db *sql.DB, lock *sync.Mutex) repo.InventoryStore {
return &InventoryDB{modelStore{db, lock}}
}
func (i *InventoryDB) Put(slug string, variantIndex int, count int64) error {
i.lock.Lock()
defer i.lock.Unlock()
id := sha256.Sum256([]byte(slug + strconv.Itoa(variantIndex)))
tx, _ := i.db.Begin()
stmt, err := tx.Prepare("insert or replace into inventory(invID, slug, variantIndex, count) values(?,?,?,?)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(hex.EncodeToString(id[:]), slug, variantIndex, count)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (i *InventoryDB) GetSpecific(slug string, variantIndex int) (int64, error) {
i.lock.Lock()
defer i.lock.Unlock()
stmt, err := i.db.Prepare("select count from inventory where slug=? and variantIndex=?")
if err != nil {
return 0, err
}
defer stmt.Close()
var count int64
err = stmt.QueryRow(slug, variantIndex).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func (i *InventoryDB) Get(slug string) (map[int]int64, error) {
i.lock.Lock()
defer i.lock.Unlock()
ret := make(map[int]int64)
stmt, err := i.db.Prepare("select slug, variantIndex, count from inventory where slug=?")
if err != nil {
return ret, err
}
defer stmt.Close()
rows, err := stmt.Query(slug)
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var slug string
var count int64
var variantIndex int
rows.Scan(&slug, &variantIndex, &count)
ret[variantIndex] = count
}
return ret, nil
}
func (i *InventoryDB) GetAll() (map[string]map[int]int64, error) {
i.lock.Lock()
defer i.lock.Unlock()
ret := make(map[string]map[int]int64)
stm := "select slug, variantIndex, count from inventory"
rows, err := i.db.Query(stm)
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var slug string
var count int64
var variantIndex int
rows.Scan(&slug, &variantIndex, &count)
m, ok := ret[slug]
if !ok {
r := make(map[int]int64)
r[variantIndex] = count
ret[slug] = r
} else {
m[variantIndex] = count
}
}
return ret, nil
}
func (i *InventoryDB) Delete(slug string, variantIndex int) error {
i.lock.Lock()
defer i.lock.Unlock()
_, err := i.db.Exec("delete from inventory where slug=? and variantIndex=?", slug, variantIndex)
return err
}
func (i *InventoryDB) DeleteAll(slug string) error {
i.lock.Lock()
defer i.lock.Unlock()
_, err := i.db.Exec("delete from inventory where slug=?", slug)
return err
}