Ejemplo n.º 1
0
}

type bookManagment Managment

var BookM = &bookManagment{DB: db.DB}

func NewBookWithID(id string) (book Book, err error) {
	if i, err := strconv.ParseUint(id, 10, 64); err == nil {
		book = Book{}
		book.ID = uint(i)
	}
	return
}

// define states
var BookStateMachine = transition.New(&Book{})

func initBookState() {
	BookStateMachine.Initial("Preparing")
	BookStateMachine.State("Available")
	BookStateMachine.State("Salling")
	BookStateMachine.Event("Roll").To("Available").From("Preparing")
	BookStateMachine.Event("Discount").To("Salling").From("Available")
}

// methods for bookManagment
func (b *bookManagment) Create(book *Book) bool {
	b.DB.Create(book)
	return b.DB.NewRecord(book)
}
Ejemplo n.º 2
0
	transition.Transition
}

func (order Order) Amount() (amount float32) {
	for _, orderItem := range order.OrderItems {
		amount += orderItem.Amount()
	}
	return
}

func (item OrderItem) Amount() float32 {
	return item.Price * float32(item.Quantity) * float32(100-item.DiscountRate) / 100
}

var (
	OrderState = transition.New(&Order{})
	ItemState  = transition.New(&OrderItem{})
)

func init() {
	// Define Order's States
	OrderState.Initial("draft")
	OrderState.State("checkout")
	OrderState.State("cancelled").Enter(func(value interface{}, tx *gorm.DB) error {
		// release stock, change items's state
		return nil
	})
	OrderState.State("paid").Enter(func(value interface{}, tx *gorm.DB) error {
		// freeze stock, change items's state
		return nil
	})
Ejemplo n.º 3
0
package models

import (
	"github.com/jinzhu/gorm"
	"github.com/qor/qor/transition"
)

type Order struct {
	gorm.Model
	Description string
	transition.Transition
}

var OrderStateMachine = transition.New(&Order{})

func init() {
	OrderStateMachine.Initial("pending")
}