-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
97 lines (84 loc) · 2.04 KB
/
db.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
package main
import (
"encoding/binary"
"fmt"
bolt "go.etcd.io/bbolt"
)
const (
bucket = "voucherBucket"
)
type database struct {
b *bolt.DB
}
func newDb(path string) (*database, error) {
db, err := bolt.Open(path, 0600, bolt.DefaultOptions)
if err != nil {
return nil, fmt.Errorf("could not open database: %w", err)
}
createBucketsFunc := func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
if err != nil {
return err
}
return err
}
err = db.Update(createBucketsFunc)
if err != nil {
_ = db.Close()
return nil, fmt.Errorf("could not init database: %s", err)
}
return &database{
b: db,
}, nil
}
func (db *database) withdrawMoney(voucher string, cash uint32) (uint32, error) {
var result uint32
err := db.b.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return fmt.Errorf("expected %s bucket does not exits", bucket)
}
var updatedEndingValue uint32
voucherKey := []byte(voucher)
val := b.Get(voucherKey)
if val != nil {
updatedEndingValue = binary.LittleEndian.Uint32(val)
}
updatedEndingValue = updatedEndingValue + cash
result = updatedEndingValue
val = make([]byte, 4)
binary.LittleEndian.PutUint32(val, updatedEndingValue)
if err := b.Put(voucherKey, val); err != nil {
return err
}
return b.Put(voucherKey, val)
})
if err != nil {
return 0, fmt.Errorf("could not update val in db: %w", err)
}
return result, err
}
func (db *database) showMoneyInVoucher(voucher int64) (byte, error) {
var cash byte
voucherMoney := func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return fmt.Errorf("expected %s bucket does not exist", bucket)
}
voucherKey := make([]byte, 8)
binary.LittleEndian.PutUint64(voucherKey, uint64(voucher))
val := b.Get(voucherKey)
if val != nil {
cash = val[0]
}
return nil
}
err := db.b.View(voucherMoney)
if err != nil {
return 0, fmt.Errorf("could not check user`s role: %w", err)
}
return cash, nil
}
func (db *database) Close() error {
return db.b.Close()
}