コード例 #1
0
func NewAccountFromHistory(id string, repository cqrs.EventSourcingRepository) (*Account, error) {
	account := new(Account)
	account.EventSourceBased = cqrs.NewEventSourceBasedWithID(account, id)

	if error := repository.Get(id, account); error != nil {
		return account, error
	}

	return account, nil
}
コード例 #2
0
ファイル: cqrs_test.go プロジェクト: sullirobert/cqrs
func RegisterCommandHandlers(commandDispatcher *cqrs.CommandDispatchManager, repository cqrs.EventSourcingRepository) {
	commandDispatcher.RegisterCommandHandler(CreateAccountCommand{}, func(command cqrs.Command) error {
		createAccountCommand := command.Body.(CreateAccountCommand)
		log.Println(glr.Green("Processing command - Create account"))
		account := NewAccount(createAccountCommand.FirstName,
			createAccountCommand.LastName,
			createAccountCommand.EmailAddress,
			createAccountCommand.PasswordHash,
			createAccountCommand.InitialBalance)

		log.Println(glr.Green("Set ID..."))
		account.SetID(accountID)
		log.Println(account)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(ChangeEmailAddressCommand{}, func(command cqrs.Command) error {
		changeEmailAddressCommand := command.Body.(ChangeEmailAddressCommand)
		log.Println(glr.Green("Processing command - Change email address"))
		account, err := NewAccountFromHistory(changeEmailAddressCommand.AccountID, repository)
		if err != nil {
			return err
		}

		account.ChangeEmailAddress(changeEmailAddressCommand.NewEmailAddress)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(ChangePasswordCommand{}, func(command cqrs.Command) error {
		changePasswordCommand := command.Body.(ChangePasswordCommand)
		log.Println(glr.Green("Processing command - Change password"))
		account, err := NewAccountFromHistory(changePasswordCommand.AccountID, repository)
		if err != nil {
			return err
		}

		account.ChangePassword(changePasswordCommand.NewPassword)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(CreditAccountCommand{}, func(command cqrs.Command) error {
		creditAccountCommand := command.Body.(CreditAccountCommand)
		log.Println(glr.Green("Processing command - Credit account"))
		account, err := NewAccountFromHistory(creditAccountCommand.AccountID, repository)
		if err != nil {
			return err
		}

		account.Credit(creditAccountCommand.Amount)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(DebitAccountCommand{}, func(command cqrs.Command) error {
		debitAccountCommand := command.Body.(DebitAccountCommand)
		log.Println(glr.Green("Processing command - Debit account"))
		account, err := NewAccountFromHistory(debitAccountCommand.AccountID, repository)
		if err != nil {
			return err
		}

		if err := account.Debit(debitAccountCommand.Amount); err != nil {
			return err
		}

		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})
}