/
invoice.go
71 lines (61 loc) · 1.61 KB
/
invoice.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
package main
import (
"encoding/binary"
"time"
"github.com/CCJ16/registration/regbackend/boltorm"
)
type Invoice struct {
ID uint64 `json:"id"`
To string `json:"to"`
LineItems []InvoiceItem `json:"lineItems"`
Created time.Time `json:"created"`
}
type InvoiceItem struct {
Description string `json:"description"`
UnitPrice int64 `json:"unitPrice"`
Count int64 `json:"count"`
}
type InvoiceDb interface {
NewInvoice(in *Invoice, tx boltorm.Tx) error
GetInvoice(invoiceID uint64, tx boltorm.Tx) (*Invoice, error)
}
type invoiceDb struct {
}
var (
BOLT_INVOICEBUCKET = []byte("BUCKET_INVOICES")
)
func NewInvoiceDb(db boltorm.DB) (InvoiceDb, error) {
if err := db.Update(func(tx boltorm.Tx) error {
return tx.CreateBucketIfNotExists(BOLT_INVOICEBUCKET)
}); err != nil {
return nil, err
}
return &invoiceDb{}, nil
}
func (i *invoiceDb) NewInvoice(in *Invoice, tx boltorm.Tx) error {
in.ID = 0
for in.ID == 0 {
id, err := tx.NextSequenceForBucket(BOLT_INVOICEBUCKET)
if err != nil {
return err
}
in.ID = id
}
in.Created = time.Now()
var idBytes [8]byte
binary.BigEndian.PutUint64(idBytes[:], in.ID)
return tx.Insert(BOLT_INVOICEBUCKET, idBytes[:], in)
}
func (i *invoiceDb) GetInvoice(invoiceID uint64, tx boltorm.Tx) (inv *Invoice, err error) {
inv = &Invoice{}
var key [8]byte
binary.BigEndian.PutUint64(key[:], invoiceID)
if err = tx.Get(BOLT_INVOICEBUCKET, key[:], inv); err != nil {
if boltorm.ErrKeyDoesNotExist.Contains(err) {
return nil, RecordDoesNotExist.New("Could not find invoice")
} else {
return nil, err
}
}
return inv, nil
}