models

package
v0.0.0-...-bf8c89b Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2019 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrNotFound is returned when a resource cannot be found
	// in the database
	ErrNotFound modelError = "models: resource not found"

	// ErrPasswordIncorrect is returned when an invalid password
	// is used when attempting to authenticate a user.
	ErrPasswordIncorrect modelError = "models: incorrect password provided"

	// ErrEmailRequired is returned when an email address is not
	// provided when creating an 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"

	// ErrPasswordTooShort is returned when an update or create is
	// attempted with a user password that is less than 8 characthers
	ErrPasswordTooShort modelError = "models: password must be at least 8 characthers"

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

	ErrPwResetInvalid modelError = "models: token provided is not valid"

	// ErrRememberTooShort is returned when a remember token is
	// not at least 32 bytes
	ErrRememberTooShort privateError = "models: Remember token must be at least 32 bytes"

	// ErrRememberRequired is returned when a create or update is attempted
	// without a user remember token hash.
	ErrRememberRequired privateError = "models: remember is required"
	ErrUserIDRequired   privateError = "models: user ID is required"

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

	ErrServiceRequired privateError = "models: service is required"
)
View Source
const (
	OauthDropbox = "dropbox"
)

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:"-"`
}

Gallery is our image container resource that visitors see.

func (*Gallery) ImageSplitN

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

type GalleryDB

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

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 NOT stored in the DB

func (*Image) Path

func (i *Image) Path() string

func (*Image) RelativePath

func (i *Image) RelativePath() string

type ImageService

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

func NewImageService

func NewImageService() ImageService

type OAuth

type OAuth struct {
	gorm.Model
	UserID  uint   `gorm:not null;unique_index:user_id_service`
	Service string `gorm:not null;unique_index:user_id_service`
	oauth2.Token
}

type OAuthDB

type OAuthDB interface {
	Find(userID uint, service string) (*OAuth, error)
	Create(oauth *OAuth) error
	Delete(id uint) error
}

type OAuthService

type OAuthService interface {
	OAuthDB
}

func NewOAuthService

func NewOAuthService(db *gorm.DB) OAuthService

type Services

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

func NewServices

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

func (*Services) AutoMigrate

func (s *Services) AutoMigrate() error

AutoMigrate will attempt to automatically migrate the all tables

func (*Services) Close

func (s *Services) Close() error

Close closes the database connection

func (*Services) DestructiveReset

func (s *Services) DestructiveReset() error

DestructiveReset drops the all tables and rebuilds them

type ServicesConfig

type ServicesConfig func(*Services) error

func WithGallery

func WithGallery() ServicesConfig

func WithGorm

func WithGorm(dialect, connectionInfo string) ServicesConfig

func WithImage

func WithImage() ServicesConfig

func WithLogMode

func WithLogMode(logMode bool) ServicesConfig

func WithOAuth

func WithOAuth() ServicesConfig

func WithUser

func WithUser(pepper, hmacKey string) ServicesConfig

type User

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

User represents the user model stored in our database This is used for user accounts, storing both email address and a password so users can log in and gain access to their content.

type UserDB

type UserDB interface {
	// Methods for querying 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.

type UserService

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

	// InitiateReset will start the reset password process
	// by creating a reset token for the user found with the
	// provided email address.
	InitiateReset(email string) (string, error)
	CompleteReset(token, newPw 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