models

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2020 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPasswordRequired = errors.New("validate: password required")

ErrPasswordRequired occurs when the password field is left blank

View Source
var ErrUsernameRequired = errors.New("validate: username required")

ErrUsernameRequired occurs when the username field is left blank

Functions

func DbSetup

func DbSetup(cfg config.DbConfig) (*gorm.DB, error)

DbSetup opens a database connection and initializes

func EncryptPassword

func EncryptPassword(pass *string) error

EncryptPassword encrypts the password

func InitLogger

func InitLogger(cfg zap.Config) (*zap.Logger, error)

InitLogger initializes a logger to record all events and write to log files

func TrimUsername

func TrimUsername(username *string)

TrimUsername removes whitespaces in the username

func Validate

func Validate(username, password string) error

Validate checks if the user has input the required fields

func VerifyPassword

func VerifyPassword(hash, pass string) error

VerifyPassword verifies that the password matches the hash

Types

type Book

type Book struct {
	ID        uint   `json:"id"`
	Title     string `json:"title" gorm:"NOT NULL"`
	Author    string `json:"author"`
	Publisher string `json:"publisher"`
	ISBN      string `json:"isbn"`
}

Book is the basic object stored in the database

type Record

type Record struct {
	ID          uint       `json:"id"`
	UserID      uint       `json:"user_id" gorm:"NOT NULL"`
	BookID      uint       `json:"book_id" gorm:"NOT NULL"`
	ReturnDate  time.Time  `json:"return_date" gorm:"NOT NULL"`
	ExtendTimes uint       `json:"extend_times" gorm:"NOT NULL"`
	DeletedAt   *time.Time `json:"real_return_date"`
}

Record is stored when a user borrows a book from the library, and is soft deleted when the book is returned

type User

type User struct {
	ID       uint   `json:"id"`
	Username string `json:"username" gorm:"NOT NULL; UNIQUE"`
	Password string `json:"password" gorm:"NOT NULL"`
	Level    uint   `json:"level" gorm:"NOT NULL"`
}

User is a person who has access to the library Level indicates the user's privilege 0: Banned 1: User 2: Admin 3: Super Admin

func (*User) BeforeSave

func (u *User) BeforeSave() error

BeforeSave trims the username and encrypts the password before saving user data

func (*User) IsAdmin

func (u *User) IsAdmin() bool

IsAdmin checks if the user has admin privilege

func (*User) Validate

func (u *User) Validate() error

Validate checks if the user has input the required fields

Jump to

Keyboard shortcuts

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