-
Notifications
You must be signed in to change notification settings - Fork 0
/
receipt.go
63 lines (55 loc) · 1.54 KB
/
receipt.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
package aumo
import (
"github.com/google/uuid"
"upper.io/db.v3/lib/sqlbuilder"
)
// Receipt is a digital receipt
type Receipt struct {
ReceiptID uuid.UUID `json:"receipt_id" db:"receipt_id"`
Content string `json:"content" db:"content"`
Total float64 `json:"total" db:"total"`
UserID *uuid.UUID `json:"-" db:"user_id,omitempty"`
ShopID uint `json:"shop_id" db:"shop_id"`
Shop *Shop `json:"shop" db:"-"`
}
// NewReceipt is a contrsuctor for `Receipt`
func NewReceipt(content string, sID uint, total float64) *Receipt {
return &Receipt{
ReceiptID: uuid.New(),
Total: total,
Content: content,
ShopID: sID,
}
}
// Claim sets the user field of a receipt
func (r *Receipt) Claim(uID uuid.UUID) error {
if r.IsClaimed() {
return ErrUserAlreadySet
}
r.UserID = &uID
return nil
}
// IsClaimed checks if the Receipt has been claimed
func (r *Receipt) IsClaimed() bool {
return r.UserID != nil
}
// ReceiptService contains all `Receipt`
// related business logic
type ReceiptService interface {
Receipt(id string) (*Receipt, error)
Receipts() ([]Receipt, error)
Create(*Receipt) error
Update(id string, r *Receipt) error
Delete(id string) error
ClaimReceipt(uID, rID string) (*Receipt, error)
}
// ReceiptStore contains all `Receipt`
// related persistence logic
type ReceiptStore interface {
DB() sqlbuilder.Database
FindByID(tx Tx, id string) (*Receipt, error)
FindAll(tx Tx) ([]Receipt, error)
Save(tx Tx, r *Receipt) error
Update(tx Tx, id string, r *Receipt) error
Delete(tx Tx, id string) error
}