models

package
v0.0.0-...-c749255 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrNotFound modelError = "models: resource not found"

	ErrIDInvalid modelError = "models: ID provided was invalid"

	ErrPasswordIncorrect modelError = "models: incorrect password provided"

	ErrPasswordTooShort modelError = "models: password must be at least 8 characters long"

	ErrPasswordRequired modelError = "models: password is required"

	ErrEmailRequired modelError = "models: email address is required"

	ErrEmailInvalid modelError = "models: email address is not valid"

	ErrEmailTaken modelError = "models: email address is already taken"

	ErrRememberRequired modelError = "models: remember token is required"

	ErrRememberTooShort modelError = "models: remember token must be at least 32 bytes"

	ErrTokenInvalid modelError = "models: token provided is not valid"
)
View Source
const (
	ErrJobIDRequired modelError = "models: job ID is required"
)
View Source
const (
	ErrNameRequired modelError = "models: name is required"
)
View Source
const (
	ErrUserIDRequired modelError = "models: user ID is required"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Assignment

type Assignment struct {
	gorm.Model
	UserID    uint `gorm:"not_null"`
	Job       Job  `gorm:"not_null"`
	WeekStart *time.Time
}

Assignment represents the assignments table in our DB and is when a user is assigned to a assignment.

type AssignmentDB

type AssignmentDB interface {
	ByID(id uint) (*Assignment, error)
	ByUserID(userID uint) ([]Assignment, error)
	List() ([]Assignment, error)
	Create(assignment *Assignment) error
	Update(assignment *Assignment) error
	Delete(id uint) error
}

AssignmentDB is used to interact with the assignments database.

type AssignmentService

type AssignmentService interface {
	AssignmentDB
}

func NewAssignmentService

func NewAssignmentService(db *gorm.DB) AssignmentService

type Job

type Job struct {
	gorm.Model
	Name string `gorm:"not_null"`
}

Job represents the jobs table in our DB and is a single job such as "Kitchen".

type JobDB

type JobDB interface {
	ByID(id uint) (*Job, error)
	List() ([]Job, error)
	Create(job *Job) error
	Update(job *Job) error
	Delete(id uint) error
}

JobDB is used to interact with the jobs database.

type JobService

type JobService interface {
	JobDB
}

func NewJobService

func NewJobService(db *gorm.DB) JobService

type Mate

type Mate struct {
	gorm.Model
	Email string `gorm:"not_null"`
}

type MateDB

type MateDB interface {
	ByID(id uint) (*Mate, error)
	List() ([]Mate, error)
	Create(mate *Mate) error
	Update(mate *Mate) error
	Delete(id uint) error
}

MateDB is used to interact with the mates database.

type MateService

type MateService interface {
	MateDB
}

func NewMateService

func NewMateService(db *gorm.DB) MateService

type Services

type Services struct {
	Mate       MateService
	Assignment AssignmentService
	Job        JobService
	User       UserService
	DB         *gorm.DB
}

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

func (s *Services) Close() error

Closes the database connection

func (*Services) DestructiveReset

func (s *Services) DestructiveReset() error

DestructiveReset drops all tables and rebuilds them

type ServicesConfig

type ServicesConfig func(*Services) error

func WithAssignment

func WithAssignment() ServicesConfig

WithAssignment will use the existing GORM DB connection of the Services object to build and set a AssignmentService.

func WithGorm

func WithGorm(connectionInfo string) ServicesConfig

WithGorm will open a GORM connection with the provided info and attach it to the Services type if there aren't any errors.

func WithJob

func WithJob() ServicesConfig

WithJob will use the existing GORM DB connection of the Services object to build and set a JobService.

func WithLogMode

func WithLogMode(mode bool) ServicesConfig

WithLogMode will set the LogMode of the current GORM DB object associated with the Services type. It is assumed that the DB object will already exist and be initialized properly, so you will want to call something like WithGorm before this config function.

func WithMate

func WithMate() ServicesConfig

func WithUser

func WithUser(pepper, hmacKey string) ServicesConfig

WithUser will use the existing GORM DB connection of the Services object along with the provided pepper and hmacKey to build and set a UserService.

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

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.

type UserService

type UserService interface {
	Authenticate(email, password string) (*User, error)
	// InitiateReset will complete all the model-related tasks
	// to start the password reset process for the user with
	// the provided email address. Once completed, it will
	// return the token, or an error if there was one.
	InitiateReset(email string) (string, error)
	// CompleteReset will complete all the model-related tasks
	// to complete the password reset process for the user that
	// the token matches, including updating that user's pw.
	// If the token has expired, or if it is invalid for any
	// other reason the ErrTokenInvalid error will be returned.
	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