Documentation ¶
Index ¶
- Variables
- type FakeStock
- func (stock *FakeStock) GetAll(ctx context.Context) (items []*StockItem, err error)
- func (stock *FakeStock) GetItem(ctx context.Context, ID string) (*StockItem, error)
- func (stock *FakeStock) ReserveItem(ctx context.Context, ID string) error
- func (stock *FakeStock) UpdateItem(ctx context.Context, ID string, status vend.Result) error
- type FakeStockItem
- type FakeTransaction
- type FakeTransactions
- type Stock
- type StockItem
- type Transaction
- type TransactionState
- type Transactions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotAnItem is what happens if you try to vend an item what not be there ErrNotAnItem = errors.New("invalid item ID") // ErrItemEmpty is if you vend something that's out ErrItemEmpty = errors.New("no stock available") // ErrItemBroken in if you vend something that's ErrItemBroken = errors.New("item is jammed") )
Functions ¶
This section is empty.
Types ¶
type FakeStock ¶
type FakeStock struct {
Items map[string]*FakeStockItem
}
FakeStock is an in-memory Stock
func GetFakeStock ¶
func GetFakeStock() *FakeStock
GetFakeStock returns a Stock with a bunch of fake items in it
func (*FakeStock) GetAll ¶
GetAll returns all items currently in the vending machine (available or not)
func (*FakeStock) ReserveItem ¶
ReserveItem indicates that you are queued to vend an item and it's unavailable for other vends TODO: This should probably have a saner way of exhibiting reservations (eg grants?)
type FakeStockItem ¶
FakeStockItem is a data store for FakeStock
func (*FakeStockItem) GetStockItem ¶
func (item *FakeStockItem) GetStockItem() *StockItem
GetStockItem returns a copy of the stock item
type FakeTransaction ¶
type FakeTransaction struct { Transaction sync.RWMutex }
FakeTransaction is the memory backing store for FakeTransactions
func (*FakeTransaction) GetTransaction ¶
func (txn *FakeTransaction) GetTransaction() *Transaction
GetTransaction returns a copy of the transaction
type FakeTransactions ¶
type FakeTransactions struct {
// contains filtered or unexported fields
}
FakeTransactions handles syncing transactions with the database etc
func NewFakeTransactions ¶
func NewFakeTransactions(ctx context.Context) *FakeTransactions
NewFakeTransactions does the needful
func (*FakeTransactions) Get ¶
func (txmgr *FakeTransactions) Get(ctx context.Context, ID string) (*Transaction, error)
Get retrieves an existing transaction
func (*FakeTransactions) New ¶
func (txmgr *FakeTransactions) New(ctx context.Context, item *StockItem, user string) (*Transaction, error)
New sets up a new transaction starting now
func (*FakeTransactions) Update ¶
func (txmgr *FakeTransactions) Update(ctx context.Context, txn *Transaction) error
Update stores the new transaction state in the database
type Stock ¶
type Stock interface { // GetAll returns all items currently in the vending machine (available or not) GetAll(context.Context) ([]*StockItem, error) // GetItem returns information specific to a single item GetItem(ctx context.Context, ID string) (*StockItem, error) // ReserveItem indicates that you are queued to vend an item and it's unavailable for other vends ReserveItem(ctx context.Context, ID string) error // UpdateItem updates the stock with the result of your recent vend attempt. UpdateItem(ctx context.Context, ID string, status vend.Result) error }
Stock is a storage container for items in the vending machine
type StockItem ¶
type StockItem struct { ID string Name string Quantity uint8 Reserved uint8 Image string Price uint64 Location uint8 Broken bool }
StockItem represents the current state of an item in the vending machine. It should not be retained as any changes to the stock (eg vends) will not be propagated
func (*StockItem) FormattedPrice ¶
FormattedPrice returns the price as a currency string
type Transaction ¶
type Transaction struct { ID uuid.UUID `json:"id"` Item string `json:"-"` Amount uint64 `json:"-"` ProviderID string `json:"-"` User string `json:"-"` // Name + email of person doing this State TransactionState `json:"state"` Date time.Time `json:"-"` Reason string `json:"reason,omitempty"` // If something went wrong - what }
Transaction represents a single transaction in the system It has a very minimal JSON footprint to improve transfers
type TransactionState ¶
type TransactionState uint8
TransactionState describes the state of a transaction
const ( // TransactionPending is when we have a token from Stripe but it's not been charged TransactionPending TransactionState = iota + 1 // TransactionPaid is when stripe has confirmed the charge but we've not yet vended TransactionPaid // TransactionComplete is when we've successfully vended the item TransactionComplete // TransactionRejected is when Stripe declines us TransactionRejected // TransactionFailed is when something went wrong and we didn't vend TransactionFailed )
func (TransactionState) MarshalJSON ¶
func (st TransactionState) MarshalJSON() ([]byte, error)
MarshalJSON returns the value of String but lowercase
func (TransactionState) String ¶
func (st TransactionState) String() string
type Transactions ¶
type Transactions interface { // New creates a transaction from the passed info and returns it New(ctx context.Context, item *StockItem, user string) (*Transaction, error) // Get returns an existing transaction or nil if it doesn't exist Get(ctx context.Context, ID string) (*Transaction, error) // Update updates an existing transaction to the new state. // Note: It will only update the fields ProviderID, State and Reason. Update(ctx context.Context, txn *Transaction) error }
Transactions looks after transactions