models

package
v0.0.0-...-a8002e0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrUserIDRequired modelError = "models: user ID is required"
	ErrTitleRequired  modelError = "models: title is required"
)
View Source
const (

	// ErrNotFound is returned when a resource cannot be found
	// in the database.
	ErrNotFound modelError = "models: resource not found"

	// ErrIDInvalid is returned when an invalid ID is provided
	// to a method like Delete.
	ErrIDInvalid modelError = "models: ID provided was invalid"

	// ErrPasswordIncorrect is returned when an incorrect
	// password is used when authenticating a user.
	ErrPasswordIncorrect modelError = "models: incorrect password provided"

	// ErrPasswordTooShort is returned when a user tries to set
	// a password that is less than 8 characters long.
	ErrPasswordTooShort modelError = "models: password must " +
		"be at least 8 characters long"

		// ErrPasswordRequired is returned when a create is attempted
	// without a user password provided.
	ErrPasswordRequired modelError = "models: password is required"

	// ErrEmailRequired is returned when an email address is
	// not provided when creating a user
	ErrEmailRequired modelError = "models: email address is required"

	// ErrEmailInvalid is returned when an email address provided
	// does not match any of our requirements
	ErrEmailInvalid modelError = "models: email address is not valid"

	// ErrEmailTaken is returned when an update or create is attempted
	// with an email address that is already in use.
	ErrEmailTaken modelError = "models: email address is already taken"

	// ErrRememberRequired is returned when a create or update
	// is attempted without a user remember token hash
	ErrRememberRequired modelError = "models: remember token " +
		"is required"

	// ErrRememberTooShort is returned when a remember token is
	// not at least 32 bytes
	ErrRememberTooShort modelError = "models: remember token " +
		"must be at least 32 bytes"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Gallery struct {
	gorm.Model
	UserID uint    `gorm:"not null;index"`
	Title  string  `gorm:"not null"`
	Images []Image `gorm:"-"`
}

func (*Gallery) ImagesSplitN

func (g *Gallery) ImagesSplitN(n int) [][]Image

type GalleryDB

type GalleryDB interface {
	ByID(id uint) (*Gallery, error)
	ByUserID(userID uint) ([]Gallery, error)
	Create(gallery *Gallery) error
	Update(gallery *Gallery) error
	Delete(id uint) error
}

GalleryDB is used to interact with the galleries database.

For pretty much all single gallery queries: If the gallery is found, we will return a nil error If the gallery is not found, we will return ErrNotFound If there is another error, we will return an error with more information about what went wrong. This may not be an error generated by the models package.

type GalleryService

type GalleryService interface {
	GalleryDB
}

func NewGalleryService

func NewGalleryService(db *gorm.DB) GalleryService

type Image

type Image struct {
	GalleryID uint
	Filename  string
}

Image is used to represent images stored in a Gallery. Image is NOT stored in the database, and instead references data stored on disk.

func (*Image) Path

func (i *Image) Path() string

Path is used to build the absolute path used to reference this image via a web request.

func (*Image) RelativePath

func (i *Image) RelativePath() string

RelativePath is used to build the path to this image on our local disk, relative to where our Go application is run from.

type ImageService

type ImageService interface {
	Create(galleryID uint, r io.Reader, filename string) error
	ByGalleryID(galleryID uint) ([]Image, error)
	Delete(i *Image) error
}

func NewImageService

func NewImageService() ImageService

type Services

type Services struct {
	Gallery GalleryService
	User    UserService
	Image   ImageService
	// contains filtered or unexported fields
}

func NewServices

func NewServices(cfgs ...ServicesConfig) (*Services, error)

NewServices now will accept a list of config functions to run. Each function will accept a pointer to the current Services object as its only argument and will edit that object inline and return an error if there is one. Once we have run all configs we will return the Services object.

func (*Services) AutoMigrate

func (s *Services) AutoMigrate() error

AutoMigrate will attempt to automatically migrate all tables

func (*Services) DestructiveReset

func (s *Services) DestructiveReset() error

DestructiveReset drops all tables and rebuilds them

type ServicesConfig

type ServicesConfig func(*Services) error

ServicesConfig is really just a function, but I find using types like this are easier to read in my source code.

func WithGallery

func WithGallery() ServicesConfig

func WithGorm

func WithGorm(connectionInfo string) ServicesConfig

func WithImage

func WithImage() ServicesConfig

func WithNewLogger

func WithNewLogger(mode bool) ServicesConfig

func WithUser

func WithUser(pepper, hmacKey string) ServicesConfig

type User

type User struct {
	gorm.Model
	Name         string
	Email        string `gorm:"not null;uniqueIndex"`
	Password     string `gorm:"_"`
	PasswordHash string `gorm:"not null"`
	Remember     string `gorm:"-"`
	RememberHash string `gorm:"not null;uniqueIndex"`
}

type UserDB

type UserDB interface {
	// Methods for querying for single users
	ByID(id uint) (*User, error)
	ByEmail(email string) (*User, error)
	ByRemember(token string) (*User, error)

	// Methods for altering users
	Create(user *User) error
	Update(user *User) error
	Delete(id uint) error
}

UserDB is used to interact with the users database.

For pretty much all single user queries: If the user is found, we will return a nil error If the user is not found, we will return ErrNotFound If there is another error, we will return an error with more information about what went wrong. This may not be an error generated by the models package.

For single user queries, any error but ErrNotFound should probably result in a 500 error until we make "public" facing errors.

type UserService

type UserService interface {
	// Authenticate will verify the provided email address and
	// password are correct. If they are correct, the user
	// corresponding to that email will be returned. Otherwise
	// You will receive either:
	// ErrNotFound, ErrPasswordIncorrect, or another error if
	// something goes wrong.
	Authenticate(email, password string) (*User, error)
	UserDB
}

UserService is a set of methods used to manipulate and work with the user model

func NewUserService

func NewUserService(db *gorm.DB, pepper, hmacKey string) UserService

Jump to

Keyboard shortcuts

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