sqlite

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatError

func FormatError(err error) error

FormatError returns err as a WTF error, if possible. Otherwise returns the original error.

func FormatLimitOffset

func FormatLimitOffset(limit, offset int) string

FormatLimitOffset returns a SQL string for a given limit & offset. Clauses are only added if limit and/or offset are greater than zero.

Types

type AuthService

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

AuthService represents a service for managing OAuth authentication.

func NewAuthService

func NewAuthService(db *DB) *AuthService

NewAuthService returns a new instance of AuthService attached to DB.

func (*AuthService) CreateAuth

func (s *AuthService) CreateAuth(ctx context.Context, auth *wtf.Auth) error

CreateAuth Creates a new authentication object If a User is attached to auth, then the auth object is linked to an existing user. Otherwise a new user object is created.

On success, the auth.ID is set to the new authentication ID.

func (*AuthService) DeleteAuth

func (s *AuthService) DeleteAuth(ctx context.Context, id int) error

DeleteAuth permanently deletes an authentication object from the system by ID. The parent user object is not removed.

func (*AuthService) FindAuthByID

func (s *AuthService) FindAuthByID(ctx context.Context, id int) (*wtf.Auth, error)

FindAuthByID retrieves an authentication object by ID along with the associated user. Returns ENOTFOUND if ID does not exist.

func (*AuthService) FindAuths

func (s *AuthService) FindAuths(ctx context.Context, filter wtf.AuthFilter) ([]*wtf.Auth, int, error)

FindAuths retrieves authentication objects based on a filter.

Also returns the total number of objects that match the filter. This may differ from the returned object count if the Limit field is set.

type DB

type DB struct {

	// Datasource name.
	DSN string

	// Destination for events to be published.
	EventService wtf.EventService

	// Returns the current time. Defaults to time.Now().
	// Can be mocked for tests.
	Now func() time.Time
	// contains filtered or unexported fields
}

DB represents the database connection.

func NewDB

func NewDB(dsn string) *DB

NewDB returns a new instance of DB associated with the given datasource name.

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx starts a transaction and returns a wrapper Tx type. This type provides a reference to the database and a fixed timestamp at the start of the transaction. The timestamp allows us to mock time during tests as well.

func (*DB) Close

func (db *DB) Close() error

Close closes the database connection.

func (*DB) Open

func (db *DB) Open() (err error)

Open opens the database connection.

type DialMembershipService

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

DialMembershipService represents a service for managing dial memberships in SQLite.

func NewDialMembershipService

func NewDialMembershipService(db *DB) *DialMembershipService

NewDialMembershipService returns a new instance of DialMembershipService.

func (*DialMembershipService) CreateDialMembership

func (s *DialMembershipService) CreateDialMembership(ctx context.Context, membership *wtf.DialMembership) error

CreateDialMembership creates a new membership on a dial for the current user. Returns EUNAUTHORIZED if there is no current user logged in.

func (*DialMembershipService) DeleteDialMembership

func (s *DialMembershipService) DeleteDialMembership(ctx context.Context, id int) error

DeleteDialMembership permanently deletes a membership by ID. Only the membership owner and the parent dial's owner can delete a membership.

func (*DialMembershipService) FindDialMembershipByID

func (s *DialMembershipService) FindDialMembershipByID(ctx context.Context, id int) (*wtf.DialMembership, error)

FindDialMembershipByID retrieves a membership by ID along with the associated dial & user. Returns ENOTFOUND if membership does exist or user does not have permission to view it.

func (*DialMembershipService) FindDialMemberships

func (s *DialMembershipService) FindDialMemberships(ctx context.Context, filter wtf.DialMembershipFilter) ([]*wtf.DialMembership, int, error)

FindDialMemberships retrieves a list of matching memberships based on filter. Only returns memberships that belong to dials that the current user is a member of.

Also returns a count of total matching memberships which may different if "Limit" is specified on the filter.

func (*DialMembershipService) UpdateDialMembership

func (s *DialMembershipService) UpdateDialMembership(ctx context.Context, id int, upd wtf.DialMembershipUpdate) (*wtf.DialMembership, error)

UpdateDialMembership updates the value of a membership. Only the owner of the membership can update the value. Returns EUNAUTHORIZED if user is not the owner. Returns ENOTFOUND if the membership does not exist.

type DialService

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

DialService represents a service for managing dials.

func NewDialService

func NewDialService(db *DB) *DialService

NewDialService returns a new instance of DialService.

func (*DialService) AverageDialValueReport

func (s *DialService) AverageDialValueReport(ctx context.Context, start, end time.Time, interval time.Duration) (*wtf.DialValueReport, error)

AverageDialValueReport returns a report of the average dial value across all dials that the user is a member of. Average values are computed between start & end time and are slotted into given intervals. The minimum interval size is one minute.

func (*DialService) CreateDial

func (s *DialService) CreateDial(ctx context.Context, dial *wtf.Dial) error

CreateDial creates a new dial and assigns the current user as the owner. The owner will automatically be added as a member of the new dial.

func (*DialService) DeleteDial

func (s *DialService) DeleteDial(ctx context.Context, id int) error

DeleteDial permanently removes a dial by ID. Only the dial owner may delete a dial. Returns ENOTFOUND if dial does not exist. Returns EUNAUTHORIZED if user is not the dial owner.

func (*DialService) DialValues

func (s *DialService) DialValues(ctx context.Context, id int) ([]int, error)

DialValues returns a list of all stored historical values for a dial. This is only used for testing.

func (*DialService) FindDialByID

func (s *DialService) FindDialByID(ctx context.Context, id int) (*wtf.Dial, error)

FindDialByID retrieves a single dial by ID along with associated memberships. Only the dial owner & members can see a dial. Returns ENOTFOUND if dial does not exist or user does not have permission to view it.

func (*DialService) FindDials

func (s *DialService) FindDials(ctx context.Context, filter wtf.DialFilter) ([]*wtf.Dial, int, error)

FindDials retrieves a list of dials based on a filter. Only returns dials that the user owns or is a member of.

Also returns a count of total matching dials which may different from the number of returned dials if the "Limit" field is set.

func (*DialService) SetDialMembershipValue added in v0.1.1

func (s *DialService) SetDialMembershipValue(ctx context.Context, dialID, value int) error

Sets the value of the user's membership in a dial. This works the same as calling UpdateDialMembership() although it doesn't require that the user know their membership ID. Only the dial ID.

Returns ENOTFOUND if the membership does not exist.

func (*DialService) UpdateDial

func (s *DialService) UpdateDial(ctx context.Context, id int, upd wtf.DialUpdate) (*wtf.Dial, error)

UpdateDial updates an existing dial by ID. Only the dial owner can update a dial. Returns the new dial state even if there was an error during update.

Returns ENOTFOUND if dial does not exist. Returns EUNAUTHORIZED if user is not the dial owner.

type NullTime

type NullTime time.Time

NullTime represents a helper wrapper for time.Time. It automatically converts time fields to/from RFC 3339 format. Also supports NULL for zero time.

func (*NullTime) Scan

func (n *NullTime) Scan(value interface{}) error

Scan reads a time value from the database.

func (*NullTime) Value

func (n *NullTime) Value() (driver.Value, error)

Value formats a time value for the database.

type Tx

type Tx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

Tx wraps the SQL Tx object to provide a timestamp at the start of the transaction.

type UserService

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

UserService represents a service for managing users.

func NewUserService

func NewUserService(db *DB) *UserService

NewUserService returns a new instance of UserService.

func (*UserService) CreateUser

func (s *UserService) CreateUser(ctx context.Context, user *wtf.User) error

CreateUser creates a new user. This is only used for testing since users are typically created during the OAuth creation process in AuthService.CreateAuth().

func (*UserService) DeleteUser

func (s *UserService) DeleteUser(ctx context.Context, id int) error

DeleteUser permanently deletes a user and all owned dials. Returns EUNAUTHORIZED if current user is not the user being deleted. Returns ENOTFOUND if user does not exist.

func (*UserService) FindUserByID

func (s *UserService) FindUserByID(ctx context.Context, id int) (*wtf.User, error)

FindUserByID retrieves a user by ID along with their associated auth objects. Returns ENOTFOUND if user does not exist.

func (*UserService) FindUsers

func (s *UserService) FindUsers(ctx context.Context, filter wtf.UserFilter) ([]*wtf.User, int, error)

FindUsers retrieves a list of users by filter. Also returns total count of matching users which may differ from returned results if filter.Limit is specified.

func (*UserService) UpdateUser

func (s *UserService) UpdateUser(ctx context.Context, id int, upd wtf.UserUpdate) (*wtf.User, error)

UpdateUser updates a user object. Returns EUNAUTHORIZED if current user is not the user that is being updated. Returns ENOTFOUND if user does not exist.

Jump to

Keyboard shortcuts

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