Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions Godeps/Readme

This file was deleted.

53 changes: 53 additions & 0 deletions controller/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package controller

import (
"fmt"

"github.com/RealImage/QLedger/errors"
e "github.com/RealImage/QLedger/errors"
"github.com/RealImage/QLedger/models"
)

func (c *Controller) AddAccount(account *models.Account) error {
isExists, err := c.AccountDB.IsExists(account.ID)
if err != nil {
return fmt.Errorf("%w: error while checking for existing account: %v", errors.ErrInternal, err)
}
if isExists {
return fmt.Errorf("%w: account is conflicting: %s", errors.ErrConflict, account.ID)
}
err = c.AccountDB.CreateAccount(account)
if err != nil {
return fmt.Errorf("%w: error while adding account: %s (%v)", errors.ErrInternal, account.ID, err)
}
return nil
}

func (c *Controller) GetAccounts(query string) (interface{}, error) {
results, err := c.SearchEngine.Query(query)
if err != nil {
switch err.ErrorCode() {
case "search.query.invalid":
return nil, fmt.Errorf("%w: error while querying :%v", e.ErrBadRequest, err)
default:
return nil, fmt.Errorf("%w: error while querying :%v", e.ErrInternal, err)
}
}
return results, nil
}

func (c *Controller) UpdateAccount(account *models.Account) error {
isExists, err := c.AccountDB.IsExists(account.ID)
if err != nil {
return fmt.Errorf("%w: error while checking for existing account: %v", e.ErrInternal, err)
}
if !isExists {
return fmt.Errorf("%w: account doesn't exist: %v", e.ErrNotFound, err)
}

err = c.AccountDB.UpdateAccount(account)
if err != nil {
return fmt.Errorf("%w: error while updating account %s (%v)", e.ErrInternal, account.ID, err)
}
return nil
}
24 changes: 24 additions & 0 deletions controller/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controller

import (
ledgerError "github.com/RealImage/QLedger/errors"
"github.com/RealImage/QLedger/models"
)

type SearchEngine interface {
Query(q string) (interface{}, ledgerError.ApplicationError)
}

type AccountDB interface {
GetByID(id string) (*models.Account, ledgerError.ApplicationError)
IsExists(id string) (bool, ledgerError.ApplicationError)
CreateAccount(account *models.Account) ledgerError.ApplicationError
UpdateAccount(account *models.Account) ledgerError.ApplicationError
}

type TransactionDB interface {
IsExists(id string) (bool, ledgerError.ApplicationError)
IsConflict(transaction *models.Transaction) (bool, ledgerError.ApplicationError)
Transact(txn *models.Transaction) bool
UpdateTransaction(txn *models.Transaction) ledgerError.ApplicationError
}
11 changes: 11 additions & 0 deletions controller/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package controller

type Controller struct {
SearchEngine SearchEngine
AccountDB AccountDB
TransactionDB TransactionDB
}

func NewController(s SearchEngine, a AccountDB, t TransactionDB) *Controller {
return &Controller{SearchEngine: s, AccountDB: a, TransactionDB: t}
}
62 changes: 62 additions & 0 deletions controller/transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package controller

import (
"fmt"

e "github.com/RealImage/QLedger/errors"
"github.com/RealImage/QLedger/models"
)

func (c *Controller) MakeTransaction(transaction *models.Transaction) (bool, error) {
if !transaction.IsValid() {
return false, fmt.Errorf("%w: transaction is invalid: %s", e.ErrBadRequest, transaction.ID)
}
isExists, err := c.TransactionDB.IsExists(transaction.ID)
if err != nil {
return false, fmt.Errorf("%w: error while checking for existing transaction: %v", e.ErrInternal, err)
}
if !isExists {
done := c.TransactionDB.Transact(transaction)
if !done {
return false, fmt.Errorf("%w: transaction failed: %s", e.ErrInternal, transaction.ID)
}
return false, nil
}
isConflict, err := c.TransactionDB.IsConflict(transaction)
if err != nil {
return false, fmt.Errorf("%w: error while checking for conflicting transaction: %v", e.ErrInternal, err)
}
if isConflict {
// The conflicting transactions are denied
return false, fmt.Errorf("%w: transaction is conflicting: %s", e.ErrConflict, transaction.ID)
}
return true, nil
}

func (c *Controller) GetTransactions(query string) (interface{}, error) {
results, err := c.SearchEngine.Query(query)
if err != nil {
switch err.ErrorCode() {
case "search.query.invalid":
return nil, fmt.Errorf("%w: error while querying: %v", e.ErrBadRequest, err)
default:
return nil, fmt.Errorf("%w: error while querying: %v", e.ErrInternal, err)
}
}
return results, nil
}

func (c *Controller) UpdateTransaction(transaction *models.Transaction) error {
isExists, err := c.TransactionDB.IsExists(transaction.ID)
if err != nil {
return fmt.Errorf("%w: error while checking for existing transaction: %v", e.ErrInternal, err)
}
if !isExists {
return fmt.Errorf("%w: transaction doesn't exist: %s", e.ErrNotFound, transaction.ID)
}
err = c.TransactionDB.UpdateTransaction(transaction)
if err != nil {
return fmt.Errorf("%w: error while updating transaction: %s (%v)", e.ErrInternal, transaction.ID, err)
}
return nil
}
145 changes: 0 additions & 145 deletions controllers/accounts.go

This file was deleted.

Loading