Documentation ¶
Index ¶
- type Db
- func (d *Db) AwardPoints(ctx context.Context, ids []string, score int) error
- func (d *Db) CreateUser(ctx context.Context, id string) error
- func (d *Db) DeleteUser(ctx context.Context, id string) error
- func (d *Db) GetTopUsers(ctx context.Context, count int) ([]*User, error)
- func (d *Db) GetUser(ctx context.Context, id string) (*User, error)
- func (d *Db) GetUserScore(ctx context.Context, id string) (int, error)
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Db ¶
type Db struct { }
Db does some IO with a database, mocked here for simplicity
func New ¶
func New() *Db
New returns a new Db ready to do Db things.
Notice we don't provide an interface here! We only provide the concrete implementation. Accept interfaces, return implementations.
func (*Db) AwardPoints ¶
AwardPoints gives points to all the users in the ids array
func (*Db) CreateUser ¶
CreateUser creates a user starting with a score of 0
func (*Db) DeleteUser ¶
DeleteUser deletes a user with the given ID
func (*Db) GetTopUsers ¶
GetTopUsers returns the top X users ranked by score
Note that it makes sense here to return full user information, or at least some struct that contains user IDs and scores combined. So we're tying interfaces to this package. Tradeoffs.
func (*Db) GetUser ¶
GetUser returns a user's full information from the database
Notice this returns a User, which is part of this package. This means that any interface that wants to use GetUser will tie itself to this package. This is often unavoidable for any non-trivial returns, but it's a tradeoff to be aware of.
func (*Db) GetUserScore ¶
GetUserScore returns a user's score from their ID
Notice this function signature doesn't contain any package-specific types, which means any interfaces that want to implement this do -not- need to tie themselves to this package. This is great when you only need single fields at a time, but that won't always be the case.