database

package
v0.0.0-...-fbdc1e2 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is used when a record is not found
	ErrNotFound = errors.New("not found")
	// ErrUserExists is used when username/email already exists
	ErrUserExists = errors.New("user already exists")
)

Functions

This section is empty.

Types

type EmailUnsubscribe

type EmailUnsubscribe struct {
	ID    string `db:"id"`
	Email string `db:"email"`
}

EmailUnsubscribe represents an email unsubscribe record

func NewEmailUnsubscribe

func NewEmailUnsubscribe(email string) EmailUnsubscribe

NewEmailUnsubscribe creates a new email unsubscribe record

type EmailVerification

type EmailVerification struct {
	ID               string         `db:"id"`
	UserID           string         `db:"user_id"`
	CodeHash         sql.NullString `db:"verification_code"`
	MessageID        sql.NullString `db:"message_id"`
	UnsubscribeToken sql.NullString `db:"unsubscribe_token"`
	DateCreated      time.Time      `db:"date_created"`
}

EmailVerification represents an email verification record

func NewEmailVerification

func NewEmailVerification(userID, codeHash, unsubscribeToken string, createdAt time.Time) EmailVerification

NewEmailVerification creates a new email verification record

func (*EmailVerification) IsExpired

func (ev *EmailVerification) IsExpired() bool

IsExpired checks if the verification code has expired

type RefreshToken

type RefreshToken struct {
	ID          string    `db:"id"`
	UserID      string    `db:"user_id"`
	TokenHash   string    `db:"token_hash"`
	ExpiresAt   time.Time `db:"expires_at"`
	DateCreated time.Time `db:"date_created"`
}

RefreshToken represents a refresh token

func NewRefreshToken

func NewRefreshToken(userID, tokenHash string, expiresAt time.Time) RefreshToken

NewRefreshToken creates a new refresh token

func (*RefreshToken) IsExpired

func (rt *RefreshToken) IsExpired() bool

IsExpired checks if the refresh token has expired

type User

type User struct {
	ID            string         `db:"id"`
	Username      string         `db:"username"`
	DisplayName   string         `db:"name"`
	Email         sql.NullString `db:"email"`
	EmailVerified bool           `db:"email_verified"`
	PasswordHash  []byte         `db:"password_hash"`
	Role          model.Role     `db:"role"`
	OAuthProvider sql.NullString `db:"oauth_provider"`
	OAuthID       sql.NullString `db:"oauth_id"`
	DateCreated   time.Time      `db:"date_created"`
	DateUpdated   sql.NullTime   `db:"date_updated"`
}

User represents a user

func NewUser

func NewUser(username, name string, passwordHash []byte, role model.Role) User

NewUser creates a new user

func (*User) SetEmail

func (u *User) SetEmail(email string, verified bool)

SetEmail sets user email and verification status

func (*User) SetOAuthID

func (u *User) SetOAuthID(provider string, oauthID string)

SetOAuthID sets oauth provider and oauth id

type UserRepo

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

UserRepo manages API for user access

func NewUserRepo

func NewUserRepo(db *sqlx.DB, log *zap.Logger) *UserRepo

NewUserRepo constructs a user Repo

func (*UserRepo) CheckUserExists

func (r *UserRepo) CheckUserExists(ctx context.Context, name string, role model.Role) (bool, error)

CheckUserExists checks whether user with provided name and role exists

func (*UserRepo) CreateEmailUnsubscribe

func (r *UserRepo) CreateEmailUnsubscribe(ctx context.Context, unsubscribe EmailUnsubscribe) error

CreateEmailUnsubscribe creates a new email unsubscribe record

func (*UserRepo) CreateEmailVerification

func (r *UserRepo) CreateEmailVerification(ctx context.Context, vrf EmailVerification) error

CreateEmailVerification creates a new email verification record

func (*UserRepo) CreateRefreshToken

func (r *UserRepo) CreateRefreshToken(ctx context.Context, refreshToken RefreshToken) error

CreateRefreshToken inserts a new refresh token into the database

func (*UserRepo) CreateUser

func (r *UserRepo) CreateUser(ctx context.Context, user User) error

CreateUser inserts a new user into the database

func (*UserRepo) DeleteExpiredRefreshTokens

func (r *UserRepo) DeleteExpiredRefreshTokens(ctx context.Context) error

DeleteExpiredRefreshTokens deletes all expired refresh tokens

func (*UserRepo) DeleteRefreshToken

func (r *UserRepo) DeleteRefreshToken(ctx context.Context, tokenHash string) error

DeleteRefreshToken deletes a refresh token by token hash string

func (*UserRepo) DeleteRefreshTokensByUserID

func (r *UserRepo) DeleteRefreshTokensByUserID(ctx context.Context, userID string) error

DeleteRefreshTokensByUserID deletes all refresh tokens for a user

func (*UserRepo) DeleteUser

func (r *UserRepo) DeleteUser(ctx context.Context, userID string) error

DeleteUser deletes a user by user id

func (*UserRepo) GetEmailVerificationByUserID

func (r *UserRepo) GetEmailVerificationByUserID(ctx context.Context, userID string) (EmailVerification, error)

GetEmailVerificationByUserID gets email verification by user ID (most recent unused)

func (*UserRepo) GetRefreshTokenByHash

func (r *UserRepo) GetRefreshTokenByHash(ctx context.Context, tokenHash string) (RefreshToken, error)

GetRefreshTokenByHash retrieves a refresh token by token hash string with row-level lock

func (*UserRepo) GetUserByEmail

func (r *UserRepo) GetUserByEmail(ctx context.Context, email string) (User, error)

GetUserByEmail gets user by email address

func (*UserRepo) GetUserByID

func (r *UserRepo) GetUserByID(ctx context.Context, userID string) (user User, err error)

GetUserByID returns user by id

func (*UserRepo) GetUserByOAuth

func (r *UserRepo) GetUserByOAuth(ctx context.Context, provider string, oauthID string) (User, error)

GetUserByOAuth returns a user by oauth provider and oauth_id

func (*UserRepo) GetUserByUsername

func (r *UserRepo) GetUserByUsername(ctx context.Context, username string) (user User, err error)

GetUserByUsername returns user by username

func (*UserRepo) IsEmailUnsubscribed

func (r *UserRepo) IsEmailUnsubscribed(ctx context.Context, email string) (bool, error)

IsEmailUnsubscribed checks if an email address is unsubscribed

func (*UserRepo) RunWithTx

func (r *UserRepo) RunWithTx(ctx context.Context, f func(context.Context) error) error

RunWithTx runs a function in a transaction

func (*UserRepo) SetEmailVerificationMessageID

func (r *UserRepo) SetEmailVerificationMessageID(ctx context.Context, id string, messageID string) error

SetEmailVerificationMessageID sets the message_id for an email verification record

func (*UserRepo) SetEmailVerificationUsed

func (r *UserRepo) SetEmailVerificationUsed(ctx context.Context, id string, verified bool) error

SetEmailVerificationUsed sets email verification as used by clearing code hash and optionally setting verified_at

func (*UserRepo) SetUnsubscribeToken

func (r *UserRepo) SetUnsubscribeToken(ctx context.Context, id string, token string) error

SetUnsubscribeToken sets the unsubscribe token for an email verification record

func (*UserRepo) SetUserEmailVerified

func (r *UserRepo) SetUserEmailVerified(ctx context.Context, userID string) error

SetUserEmailVerified sets user email as verified

func (*UserRepo) UpdateUser

func (r *UserRepo) UpdateUser(ctx context.Context, user User) error

UpdateUser updates user

Jump to

Keyboard shortcuts

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