domain

package
v0.0.0-...-897d125 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const MaxConns = 16

MaxConns is the maximum number of the connections per ActiveClient.

Variables

View Source
var ErrExceedsMaxConns = errors.New("exceed the number of connections")

ErrExceedsConnsMax indicates the number of the connection for the ActiveClient exceeds the maximum limits.

Functions

func SetTx

func SetTx(ctx context.Context, tx Tx) context.Context

set transaction to the new clild context and return it.

Types

type ActiveClient

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

ActiveClient is a one active domain User which has several connections.

ActiveClient can have multiple conncetions, because one user can conncets from PC, mobile device, and so on.

func (*ActiveClient) AddConn

func (ac *ActiveClient) AddConn(c Conn) (int, error)

Add new connection to ActiveClient. It returns current number of the connections and error when the connection is not for the AcitiveClient or ErrExceedsMaxConns when the number of the connections exceeds the maximum limit.

func (*ActiveClient) Delete

Delete deletes this ActiveClient from the repository. If ayt of connections exist, it fail and return error.

func (*ActiveClient) ForceDelete

ForceDelete forcibly deletes this ActiveClient from the repository. It closes all of underlying connections and removes from ActiveClient. It returns error if already Deleted.

func (*ActiveClient) HasConn

func (ac *ActiveClient) HasConn(c Conn) bool

func (*ActiveClient) RemoveConn

func (ac *ActiveClient) RemoveConn(c Conn) (int, error)

RemoveConn removes connection from ActiveClient. It returns the rest number of the connection and errors related with the connection is removed.

func (*ActiveClient) Send

func (ac *ActiveClient) Send(ev event.Event)

Send domain event to all of the client connections.

type ActiveClientRepository

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

ActiveClientRepository manages active clients. It is inmemory struct type rather than interface because ActiveClient has connection interface which can not be serialized.

func NewActiveClientRepository

func NewActiveClientRepository(capHint int) *ActiveClientRepository

func (*ActiveClientRepository) ExistByConn

func (cm *ActiveClientRepository) ExistByConn(c Conn) bool

Check whether the specified connection exists in the repository. It returns true if found otherwise returns false.

func (*ActiveClientRepository) Find

func (cm *ActiveClientRepository) Find(userID uint64) (*ActiveClient, error)

Find ActiveClient by user ID. It returns found AcitiveClient and error if not found.

func (*ActiveClientRepository) FindAllByUserIDs

func (cm *ActiveClientRepository) FindAllByUserIDs(userIDs []uint64) ([]*ActiveClient, error)

Find all ActiveClients by user ID list. It returns found AcitiveClients and error if all of the user ID are not found. The number of the returned ActiveClients may not same as that of user ID list, when a part of the user ID list are not found.

func (*ActiveClientRepository) Remove

func (cm *ActiveClientRepository) Remove(ac *ActiveClient) error

Remove connection from the repository.

func (*ActiveClientRepository) Store

func (cm *ActiveClientRepository) Store(ac *ActiveClient) error

Store activeClient to the repository. It returns error when something wrong.

type Conn

type Conn interface {
	// It returns user specific id to distinguish which client
	// connect to.
	UserID() uint64

	// It sends any domain event to client.
	Send(ev event.Event)

	// Close close the underlying connection.
	// It should not panic when it is called multiple time, returnning error is OK.
	Close() error
}

Conn is a interface for the end-point connection for sending domain event.

type EmptyTxBeginner

type EmptyTxBeginner struct{}

EmptyTxBeginner implements Tx and TxBeginner interfaces. It is used to implement the repository with the no-operating transaction, typically used as embedded struct.

func (EmptyTxBeginner) BeginTx

func (tx EmptyTxBeginner) BeginTx(context.Context, *sql.TxOptions) (Tx, error)

func (EmptyTxBeginner) Commit

func (EmptyTxBeginner) Commit() error

func (EmptyTxBeginner) Rollback

func (EmptyTxBeginner) Rollback() error

type EventHolder

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

EventHolder holds event objects. It is used to embed into entity.

func NewEventHolder

func NewEventHolder() EventHolder

func (*EventHolder) AddEvent

func (holder *EventHolder) AddEvent(ev event.Event)

func (*EventHolder) Events

func (holder *EventHolder) Events() []event.Event

type Message

type Message struct {
	EventHolder

	// ID and CreatedAt are auto set.
	ID        uint64    `db:"id"`
	CreatedAt time.Time `db:"created_at"`

	Content string `db:"content"`
	UserID  uint64 `db:"user_id"`
	RoomID  uint64 `db:"room_id"`
	Deleted bool   `db:"deleted"`
}

func NewRoomMessage

func NewRoomMessage(
	ctx context.Context,
	msgs MessageRepository,
	u User,
	r Room,
	content string,
) (Message, error)

NewRoomMessage creates new message for the specified room. The created message is immediately stored into the repository. It returns new message holding event message created and error if any.

func (*Message) NotExist

func (m *Message) NotExist() bool

type MessageRepository

type MessageRepository interface {
	TxBeginner

	Find(ctx context.Context, msgID uint64) (Message, error)

	// Store stores given message to the repository.
	// user need not to set ID for message since it is auto set
	// when message is newly.
	// It returns stored Message ID and error.
	Store(ctx context.Context, m Message) (uint64, error)

	// RemoveAllByRoomID removes all messages related with roomID.
	RemoveAllByRoomID(ctx context.Context, roomID uint64) error
}

type Repositories

type Repositories interface {
	Users() UserRepository
	Messages() MessageRepository
	Rooms() RoomRepository

	Events() event.EventRepository
}

Repositories holds any XXXRepository. you can get each repository from this.

type Room

type Room struct {
	EventHolder

	// ID = 0 means new entity which is not in the repository
	ID         uint64
	Name       string
	IsTalkRoom bool

	CreatedAt time.Time

	OwnerID     uint64
	MemberIDSet UserIDSet

	// key: userID, value: ReadTime
	MemberReadTimes TimeSet
}

Room entity. Its fields are exported due to construct from the datastore. In application side, creating/modifying/deleting the room should be done by the methods which emits the domain event.

func NewRoom

func NewRoom(ctx context.Context, roomRepo RoomRepository, name string, user *User, memberIDs UserIDSet) (*Room, error)

create new Room entity into the repository. It retruns room holding RoomCreated event and error if any.

func (*Room) AddMember

func (r *Room) AddMember(user User) (event.RoomAddedMember, error)

It adds the member to the room. It returns the event adding to the room, and error when the user already exist in the room.

func (*Room) Delete

func (r *Room) Delete(ctx context.Context, repo RoomRepository, user *User) error

It deletes the room from repository. After successing that, the room holds RoomDeleted event.

func (*Room) HasMember

func (r *Room) HasMember(member User) bool

It returns true when the room member exists in the room, otherwise returns false.

func (*Room) MemberIDs

func (r *Room) MemberIDs() []uint64

It returns a deep copy of the room member's IDs as list.

func (*Room) NotExist

func (r *Room) NotExist() bool

It returns whether the room is newly.

func (*Room) ReadMessagesBy

func (r *Room) ReadMessagesBy(u *User, readAt time.Time) (event.RoomMessagesReadByUser, error)

ReadMessagesBy marks that the room messages before time readAt are read by the specified user.

It returns MessageReadByUser event and error if any.

func (*Room) RemoveMember

func (r *Room) RemoveMember(user User) (event.RoomRemovedMember, error)

RoomRemovedMember removes the member from the room. It returns the event the room member is removed or returns error when the user already does not exist in the room.

type RoomRepository

type RoomRepository interface {
	TxBeginner

	// get one room.
	Find(ctx context.Context, roomID uint64) (Room, error)

	// get all the rooms which user has, from repository.
	FindAllByUserID(ctx context.Context, userID uint64) ([]Room, error)

	// store new room to repository and return
	// stored room id.
	Store(ctx context.Context, r Room) (uint64, error)

	// remove room from repository.
	Remove(ctx context.Context, r Room) error
}

type SimpleRepositories

type SimpleRepositories struct {
	UserRepository    UserRepository
	MessageRepository MessageRepository
	RoomRepository    RoomRepository

	EventRepository event.EventRepository
}

SimpleRepositories implementes Repositories interface. It acts just returning its fields when interface methods, Users(), Messages() and Rooms(), are called.

func (SimpleRepositories) Events

func (SimpleRepositories) Messages

func (SimpleRepositories) Rooms

func (SimpleRepositories) Users

type TimeSet

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

TimeSet is a set for the time.Time. empty value is valid for use.

func NewTimeSet

func NewTimeSet(ids ...uint64) TimeSet

func (*TimeSet) Delete

func (set *TimeSet) Delete(id uint64)

Delete deletes id from the internal map.

func (*TimeSet) Get

func (set *TimeSet) Get(id uint64) (time.Time, bool)

Get returns time.Time corresponding to the id and true. If id not found It's second returned value is false.

func (*TimeSet) Set

func (set *TimeSet) Set(id uint64, t time.Time)

Set sets time.Time with corresponding id to the internal map.

type Tx

type Tx interface {
	Commit() error
	Rollback() error
}

Tx is a interface for the transaction context. the transaction must end by calling Commit() or Rollback().

Because the transaction object depends on the external infrastructures such as Sql-like DB, it can be used with type assertion to use full features for the implementation-specific transaction object.

func GetTx

func GetTx(ctx context.Context) (tx Tx, exists bool)

Get Tx object from context.

type TxBeginner

type TxBeginner interface {
	BeginTx(context.Context, *sql.TxOptions) (Tx, error)
}

TxBeginner can start transaction with context object. TxOptions are typically used to specify the transaction level. A nil TxOptions means to use default transaction level.

type User

type User struct {
	EventHolder

	ID        uint64
	Name      string
	FirstName string
	LastName  string
	Password  string

	FriendIDs UserIDSet
}

User entity. Its fields are exported due to construct from the datastore. In application side, creating/modifying/deleting the user should be done by the methods which emits the domain event.

func NewUser

func NewUser(
	ctx context.Context,
	userRepo UserRepository,
	name, firstName, lastName, password string,
	friendIDs UserIDSet,
) (User, error)

create new Room entity into the repository. It retruns the new user holding event for UserCreated and error if any.

func (*User) AddFriend

func (u *User) AddFriend(friend User) (event.UserAddedFriend, error)

It adds the friend to the user. It returns the event adding into the user, and error when the friend already exist in the user.

func (*User) HasFriend

func (u *User) HasFriend(friend User) bool

It returns whether the user has specified friend?

func (*User) NotExist

func (u *User) NotExist() bool

return whether user is not in the datastore.

type UserIDSet

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

set for user id.

func NewUserIDSet

func NewUserIDSet(ids ...uint64) UserIDSet

func (*UserIDSet) Add

func (set *UserIDSet) Add(id uint64)

func (*UserIDSet) Has

func (set *UserIDSet) Has(id uint64) bool

func (*UserIDSet) List

func (set *UserIDSet) List() []uint64

It returns a deep copy of the ID list.

func (*UserIDSet) Remove

func (set *UserIDSet) Remove(id uint64)

type UserRepository

type UserRepository interface {
	TxBeginner

	// Store specified user to the repository, and return user id
	// for stored new user.
	Store(context.Context, User) (uint64, error)

	// Find one user by id.
	Find(ctx context.Context, id uint64) (User, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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