user

package
v0.0.0-...-61905c3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: GPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotFound = New(gorm.ErrRecordNotFound, "user not found")
)
View Source
var UserFieldJsonMap = map[string]string{
	"Email":     "email",
	"FirstName": "firstname",
	"LastName":  "lastname",
	"Password":  "password",
}

UserFieldJsonMap represents user's struct field for json key

Functions

func GetValidate

func GetValidate() *validator.Validate

func Validate

func Validate(user *User) error

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents errors for user packages whilst maintaining base error. Error can either be checked using == or by calling errors.Is() to check the base of Error.

Example of User not found in database.

user, err := s.UserByEmail(tt.args.email)
if err != nil {
	if err == ErrUserNotFound {
		// omitted
	}
	//or
	if errors.Is(err, gorm.ErrRecordNotFound) {
		// omitted
	}
}

func New

func New(base error, msg string) *Error

New creates Error with base from other error, like from gorm.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

type Store

type Store interface {
	// Create inserts a new user into the database.
	// Returns an error if the user could not be created.
	Create(u *User) error

	// User gets a user from the database that matches the specified criteria.
	// Returns the user and any error that occurred.
	User(u *User) (*User, error)

	// UserByID gets a user from the database with the specified ID.
	// Returns the user and any error that occurred.
	UserByID(id uint) (*User, error)

	// UserByEmail gets a user from the database with the specified email address.
	// Returns the user and any error that occurred.
	UserByEmail(email string) (*User, error)

	// UserByName gets users from the database with the specified name.
	// Returns the users and any error that occurred.
	UserByName(name string) ([]*User, error)

	// DB gets the underlying *gorm.DB instance.
	DB() *gorm.DB

	// SetDB sets the underlying *gorm.DB instance.
	SetDB(db *gorm.DB)

	// Migrate auto-migrates the User model to database.
	Migrate() error
}

func NewStore

func NewStore(db *gorm.DB) Store

type User

type User struct {
	ID        uint           `gorm:"primarykey"`
	UUID      string         `json:"uuid" gorm:"index:uuid_index,unique"`
	Email     string         `json:"email" gorm:"index:email_index,unique" validate:"required,email"`
	FirstName string         `json:"firstname" validate:"required,min=3"`
	LastName  string         `json:"lastname" validate:"required,min=3"`
	Password  string         `json:"-" validate:"required,min=5"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

func (*Validator) GetValidate

func (v *Validator) GetValidate() *validator.Validate

func (*Validator) Validate

func (v *Validator) Validate(user *User) error

Jump to

Keyboard shortcuts

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