database

package
v0.0.0-...-088e97f Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 11 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 EmailVerification

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

EmailVerification represents an email verification record

func NewEmailVerification

func NewEmailVerification(userID, codeHash string, expiresAt 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 Role

type Role string

Role - user role

const (
	UserRoleName      Role = "user"
	PublisherRoleName Role = "publisher"
)

User role names

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          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 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 Role) (bool, error)

CheckUserExists checks whether user with provided name and role exists

func (*UserRepo) CreateEmailVerification

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

CreateEmailVerification creates a new email verification record

func (*UserRepo) CreateUser

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

CreateUser inserts a new user into the database

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) 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) 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) 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