db

package
v0.0.0-...-730fded Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

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

func (d *Db) AwardPoints(ctx context.Context, ids []string, score int) error

AwardPoints gives points to all the users in the ids array

func (*Db) CreateUser

func (d *Db) CreateUser(ctx context.Context, id string) error

CreateUser creates a user starting with a score of 0

func (*Db) DeleteUser

func (d *Db) DeleteUser(ctx context.Context, id string) error

DeleteUser deletes a user with the given ID

func (*Db) GetTopUsers

func (d *Db) GetTopUsers(ctx context.Context, count int) ([]*User, error)

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

func (d *Db) GetUser(ctx context.Context, id string) (*User, error)

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

func (d *Db) GetUserScore(ctx context.Context, id string) (int, error)

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.

type User

type User struct {
	ID    string
	Score int
}

User represents a user as it's stored in the database

Jump to

Keyboard shortcuts

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