storage

package
v0.0.0-...-e2f2381 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2022 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package storage contains the code to persist and retrieve orders from a database

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrOrderNotFound is returned when the specified order cannot be found
	ErrOrderNotFound = errors.New("order not found")

	// ErrOrderExists is returned when a new order is being inserted but an order
	// with the same ID already exists
	ErrOrderExists = errors.New("order already exists")
)

Functions

This section is empty.

Types

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

Instance holds a database connection for use in the storage methods

func New

func New(overrideDatabase string) *Instance

func (*Instance) GetOrder

func (i *Instance) GetOrder(ctx context.Context, id string) (Order, error)

GetOrder should return the order with the given ID. If that ID isn't found then the special ErrOrderNotFound error should be returned.

func (*Instance) GetOrders

func (i *Instance) GetOrders(ctx context.Context, status OrderStatus) ([]Order, error)

GetOrders should return all orders with the given status. If status is the special -1 value then it should return all orders regardless of their status.

func (*Instance) InsertOrder

func (i *Instance) InsertOrder(ctx context.Context, order Order) (string, error)

InsertOrder should fill in the order's ID with a unique identifier if it's not already set and then insert it into the database. It should return the order's ID. If the order already exists then ErrOrderExists should be returned.

func (*Instance) SetOrderStatus

func (i *Instance) SetOrderStatus(ctx context.Context, id string, status OrderStatus) error

SetOrderStatus should update the order with the given ID and set the status field. If that ID isn't found then the special ErrOrderNotFound error should be returned.

type LineItem

type LineItem struct {
	// Description is a product ID or a discount ID
	Description string `json:"description"`
	// PriceCents is the individual price that should be multiplied against quantity
	PriceCents int64 `json:"priceCents"`
	// Quantity is how many descriptions this line item represents
	Quantity int64 `json:"quantity"`
}

LineItem is a single charge on an order. The product of the PriceCents and Quantity is the total price of the line item.

type Order

type Order struct {
	// ID is the unique identifier for the order that never changes throughout the
	// order's lifecycle
	ID string `json:"id"`
	// CustomerEmail is the email address of the customer who placed the order
	CustomerEmail string `json:"customerEmail"`
	// LineItems holds the actual products, or discounts, that apply to the order
	LineItems []LineItem `json:"lineItems"`
	// Status represents the current state of the order throughout the
	// pending->charged->fulfilled lifecycle
	Status OrderStatus `json:"status"`
}

Order represents a single order for one or more products

func (Order) TotalCents

func (o Order) TotalCents() int64

TotalCents is a helper function that loops over each line item and totals up the amount to charge for the whole order

type OrderStatus

type OrderStatus int64

OrderStatus describes the current status of the order

const (
	// OrderStatusPending means we haven't charged the customer yet
	OrderStatusPending OrderStatus = 0

	// OrderStatusCharged means we've successfully charged the customer
	OrderStatusCharged OrderStatus = 1

	// OrderStatusFulfilled means we've successfully fulfilled ALL of the line
	// items and we've begun to ship the order
	OrderStatusFulfilled OrderStatus = 2
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL