Skip to content

Instantly share code, notes, and snippets.

@italolelis
Last active January 18, 2019 14:09
Show Gist options
  • Save italolelis/9a4bbc9693468944906d0e1163ca8b5f to your computer and use it in GitHub Desktop.
Save italolelis/9a4bbc9693468944906d0e1163ca8b5f to your computer and use it in GitHub Desktop.
Order modelling
// Order represents a coffee order
type Order struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
Items Items `json:"items" db:"items"`
CustomerName string `json:"customer_name" db:"customer_name"`
}
// Items are a collection of order items
type Items []*Item
// Item represents the order item
type Item struct {
Type string `json:"type"`
Size string `json:"size" db:"size"`
}
// NewOrder creates a new instance of Order
func NewOrder(id uuid.UUID, customerName string) *Order {
return &Order{
ID: id,
CreatedAt: time.Now(),
CustomerName: customerName,
}
}
// NextOrderID generates the next order ID
func NextOrderID() uuid.UUID {
return uuid.NewV4()
}
// AddItems adds a list of items to the order
func (o *Order) AddItems(items Items) *Order {
o.Items = items
return o
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment