models

package
v0.0.0-...-69bde9d Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoRecord = errors.New("models: no matching record found")
	// Add a new ErrInvalidCredentials error. We'll use this later if a user
	// tries to login with an incorrect email address or password.
	ErrInvalidCredentials = errors.New("models: invalid credentials")

	// Add a new ErrDuplicateEmail error. We'll use this later if a user
	// tries to signup with an email address that's already in use.
	ErrDuplicateEmail = errors.New("models: duplicate email")
)

Functions

This section is empty.

Types

type Snippet

type Snippet struct {
	ID      int
	Title   string
	Content string
	Created time.Time
	Expires time.Time
}

Define a Snippet type to hold the data for an individual snippet. Notice how the fields of the struct correspond to the fields in our MySQL snippets table?

type SnippetModel

type SnippetModel struct {
	DB *sql.DB
}

Define a SnippetModel type which wraps a sql.DB connection pool.

func (*SnippetModel) Get

func (m *SnippetModel) Get(id int) (Snippet, error)

This will return a specific snippet based on its id.

func (*SnippetModel) Insert

func (m *SnippetModel) Insert(title string, content string, expires int) (int, error)

This will insert a new snippet into the database.

func (*SnippetModel) Latest

func (m *SnippetModel) Latest() ([]Snippet, error)

This will return the 10 most recently created snippets.

type SnippetModelInterface

type SnippetModelInterface interface {
	Insert(title string, content string, expires int) (int, error)
	Get(id int) (Snippet, error)
	Latest() ([]Snippet, error)
}

type User

type User struct {
	ID             int
	Name           string
	Email          string
	HashedPassword []byte
	Created        time.Time
}

Define a new User struct. Notice how the field names and types align with the columns in the database "users" table?

type UserModel

type UserModel struct {
	DB *sql.DB
}

Define a new UserModel struct which wraps a database connection pool.

func (*UserModel) Authenticate

func (m *UserModel) Authenticate(email, password string) (int, error)

We'll use the Authenticate method to verify whether a user exists with the provided email address and password. This will return the relevant user ID if they do.

func (*UserModel) Exists

func (m *UserModel) Exists(id int) (bool, error)

We'll use the Exists method to check if a user exists with a specific ID.

func (*UserModel) Insert

func (m *UserModel) Insert(name, email, password string) error

We'll use the Insert method to add a new record to the "users" table.

type UserModelInterface

type UserModelInterface interface {
	Insert(name, email, password string) error
	Authenticate(email, password string) (int, error)
	Exists(id int) (bool, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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