accounts

package module
v0.0.0-...-8e653a7 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-accounts

Simple implementation of the accounts interaction

Documentation

Index

Constants

View Source
const (
	RoleOwner   = "owner"
	RoleAdmin   = "admin"
	RoleManager = "manager"
	RoleUser    = "user"
	RoleGuest   = "guest"
)

Predefined roles

Variables

View Source
var (
	ErrNotFoundAccount   = errors.New("account not found")
	ErrNotFoundMember    = errors.New("member not found")
	ErrNameMissed        = errors.New("name is missed")
	ErrOwnerIDMissed     = errors.New("owner id is missed")
	ErrNotExistedAccount = errors.New("could not update not existed account")
)

Predefined accounts package errors

Functions

This section is empty.

Types

type AccRepository

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

AccRepository struct

func NewAccountRepository

func NewAccountRepository(db *sql.DB, driverName, tableName string) *AccRepository

NewAccountRepository factory

func (*AccRepository) Delete

func (r *AccRepository) Delete(id string) error

Delete ...

func (*AccRepository) GetByID

func (r *AccRepository) GetByID(id string) (*Account, error)

GetByID ...

func (*AccRepository) GetList

func (r *AccRepository) GetList(c ...Condition) ([]*Account, error)

GetList ...

func (*AccRepository) Insert

func (r *AccRepository) Insert(a *Account) error

Insert ...

func (*AccRepository) Update

func (r *AccRepository) Update(a *Account) error

Update ...

type Account

type Account struct {
	ID        string `db:"id" json:"id"`
	Name      string `db:"name" json:"name"`
	Disabled  bool   `db:"disabled" json:"disabled"`
	CreatedAt int64  `db:"created_at" json:"created_at"`
	UpdatedAt *int64 `db:"updated_at" json:"updated_at,omitempty,"`
}

Account model structure

type AccountRepository

type AccountRepository interface {
	GetByID(id string) (*Account, error)
	GetList(c ...Condition) ([]*Account, error)
	Insert(a *Account) error
	Update(a *Account) error
	Delete(id string) error
}

AccountRepository interface

type AccountWithRole

type AccountWithRole struct {
	Account
	Role string
}

AccountWithRole is account model with role of user for each account

type Condition

type Condition interface {
	Query() string
	Params() []interface{}
	Type() QuerySection
}

Condition struct

func AccountID

func AccountID(id string) Condition

AccountID func add condition to select query

func AccountIDs

func AccountIDs(ids []string) Condition

AccountIDs func add condition to select query

func Disabled

func Disabled(v bool) Condition

Disabled func add condition to select items only with disabled=v

func IDs

func IDs(ids []string) Condition

IDs func add condition to select query

func Limit

func Limit(v int) Condition

Limit func add limit to select query

func Offset

func Offset(v int) Condition

Offset func add offset to select query

func OrderBy

func OrderBy(order ...Order) Condition

OrderBy func add ordering to selected list

func Role

func Role(v string) Condition

Role func add condition to select query

func UserID

func UserID(id string) Condition

UserID func add condition to select query

func UserIDs

func UserIDs(ids []string) Condition

UserIDs func add condition to select query

type Interactor

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

Interactor struct

func NewInteractor

func NewInteractor(ar AccountRepository, mr MemberRepository) *Interactor

NewInteractor factory

func (*Interactor) AddMember

func (i *Interactor) AddMember(aid, uid, role string) error

AddMember member to account

func (*Interactor) Delete

func (i *Interactor) Delete(id string) error

Delete account by id with all related members

func (*Interactor) DeleteMemberByID

func (i *Interactor) DeleteMemberByID(mid string) error

DeleteMemberByID deletes member by member id

func (*Interactor) DeleteMembersByAccountID

func (i *Interactor) DeleteMembersByAccountID(aid string) error

DeleteMembersByAccountID deletes members by account id

func (*Interactor) DeleteMembersByUserID

func (i *Interactor) DeleteMembersByUserID(uid string) error

DeleteMembersByUserID deletes members by user id

func (*Interactor) GetAccountsListByUserID

func (i *Interactor) GetAccountsListByUserID(uid string, c ...Condition) ([]*Account, error)

GetAccountsListByUserID returns accounts list by user id

func (*Interactor) GetAccountsListWithRoleByUserID

func (i *Interactor) GetAccountsListWithRoleByUserID(uid string, c ...Condition) ([]*AccountWithRole, error)

GetAccountsListWithRoleByUserID returns accounts list with roles by user id

func (*Interactor) GetByID

func (i *Interactor) GetByID(id string) (*Account, error)

GetByID fetch account by id

func (*Interactor) GetList

func (i *Interactor) GetList(c ...Condition) ([]*Account, error)

GetList of accounts

func (*Interactor) GetMembersList

func (i *Interactor) GetMembersList(c ...Condition) ([]*Member, error)

GetMembersList returns members list

func (*Interactor) HasRole

func (i *Interactor) HasRole(aid, uid string, role ...string) bool

HasRole helper to check user role for account

func (*Interactor) Insert

func (i *Interactor) Insert(a *Account, uid string) error

Insert a new account and add user as owner

func (*Interactor) Update

func (i *Interactor) Update(a *Account) error

Update existed account

type MembRepository

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

MembRepository struct

func NewMemberRepository

func NewMemberRepository(db *sql.DB, driverName, tableName string) *MembRepository

NewMemberRepository factory

func (*MembRepository) Delete

func (r *MembRepository) Delete(id string) error

Delete ...

func (*MembRepository) DeleteByAccountID

func (r *MembRepository) DeleteByAccountID(aid string) error

DeleteByAccountID ...

func (*MembRepository) DeleteByUserID

func (r *MembRepository) DeleteByUserID(uid string) error

DeleteByUserID ...

func (*MembRepository) GetByID

func (r *MembRepository) GetByID(id string) (*Member, error)

GetByID ...

func (*MembRepository) GetList

func (r *MembRepository) GetList(c ...Condition) ([]*Member, error)

GetList ...

func (*MembRepository) HasRole

func (r *MembRepository) HasRole(aid, uid string, role ...string) error

HasRole checks whether the member has the given role in the account or not

func (*MembRepository) Insert

func (r *MembRepository) Insert(m *Member) error

Insert ...

func (*MembRepository) Update

func (r *MembRepository) Update(m *Member) error

Update ...

type Member

type Member struct {
	ID        string `json:"id" db:"id"`
	AccountID string `json:"account_id" db:"account_id"`
	UserID    string `json:"user_id" db:"user_id"`
	Role      string `json:"role" db:"role"`
	CreatedAt int64  `json:"created_at" db:"created_at"`
}

Member model structure

type MemberRepository

type MemberRepository interface {
	GetByID(id string) (*Member, error)
	HasRole(aid, uid string, role ...string) error
	GetList(c ...Condition) ([]*Member, error)
	Insert(m *Member) error
	Update(m *Member) error
	Delete(id string) error
	DeleteByAccountID(aid string) error
	DeleteByUserID(uid string) error
}

MemberRepository interface

type Order

type Order int

Order type

const (
	// ORDER BY created_at ASC
	OrderByCreatedAtAsc Order = iota + 1
	// ORDER BY created_at DESC
	OrderByCreatedAtDesc
	// ORDER BY updated_at ASC
	OrderByUpdatedAtAsc
	// ORDER BY updated_at DESC
	OrderByUpdatedAtDesc
)

Predefined users list ordering

func (Order) String

func (o Order) String() string

type QuerySection

type QuerySection int

QuerySection type

const (
	QsWhere QuerySection = iota + 1
	QsOrderBy
	QsLimit
	QsOffset
)

Predefined query section type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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