models

package
v0.0.0-...-fa930de Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateURL

func CreateURL(url *URL)

CreateURL is a method used to create a new URL in the database It takes in a pointer to a URL struct as a parameter and returns nothing

func UpdateURL

func UpdateURL(url *URL) error

UpdateURL is a method used to update a URL in the database

Types

type Book

type Book struct {
	gorm.Model         // gorm.Model provides the fields ID, CreatedAt, UpdatedAt, and DeletedAt
	Title       string `gorm:"size:191;not null;unique" json:"title"`
	Author      string `gorm:"size:191;not null" json:"author"`
	Publisher   string `gorm:"size:191;not null" json:"publisher"`
	Description string `gorm:"size:191;not null" json:"description"`
}

Book is a struct that represents a book in the database

type File

type File struct {
	gorm.Model        // GORM model that contains the ID, CreatedAt, UpdatedAt, and DeletedAt fields
	Filename   string `gorm:"not null"`        // Filename of the file. Cannot be null.
	UUID       string `gorm:"unique;not null"` // UUID of the file. Must be unique and cannot be null.
}

File is a struct that represents a file in the database

type ToDo

type ToDo struct {
	gorm.Model
	Title    string `gorm:"size:100;not null;unique" json:"title"`
	Content  string `gorm:"text;not null" json:"content"`
	Author   User   `json:"author"`
	AuthorID uint   `gorm:"not null" json:"author_id"`
}

ToDo is a struct contains information about a to-do item

func (*ToDo) DeleteAToDo

func (t *ToDo) DeleteAToDo() (int64, error)

DeleteAToDo is a method that deletes a ToDo struct from the database It takes a pointer to a gorm.DB as an argument and returns the number of rows affected

func (*ToDo) FindUserToDos

func (t *ToDo) FindUserToDos(uid uint) (*[]ToDo, error)

FindUserToDos is a method that finds all ToDo structs for a given user It takes a pointer to a gorm.DB and a user ID as arguments and returns a pointer to a slice of ToDo structs

func (*ToDo) Prepare

func (t *ToDo) Prepare()

Prepare is a method that prepares the ToDo struct for saving It escapes the title and content strings and sets the created and updated times

func (*ToDo) SaveToDo

func (t *ToDo) SaveToDo(db *gorm.DB) (*ToDo, error)

SaveToDo is a method that saves a ToDo struct to the database It takes a pointer to a gorm.DB as an argument and returns a pointer to the saved ToDo struct

func (*ToDo) UpdateAToDo

func (t *ToDo) UpdateAToDo(db *gorm.DB) (*ToDo, error)

UpdateAToDo is a method that updates a ToDo struct in the database It takes a pointer to a gorm.DB as an argument and returns a pointer to the updated ToDo struct

func (*ToDo) Validate

func (t *ToDo) Validate() map[string]string

Validate is a method that validates the ToDo struct It returns a map of errors if any of the fields are not valid

type URL

type URL struct {
	gorm.Model
	LongURL      string     `json:"long_url" gorm:"unique"`
	ShortURL     string     `json:"short_url" gorm:"unique"`
	AccessCount  uint       `json:"access_count"`
	LastAccessed *time.Time `json:"last_accessed"`
	AccessPlace  string     `json:"access_place"`
}

URL is a struct that stores the information for a URL

func GetURLByShortURL

func GetURLByShortURL(shortURL string) (URL, error)

GetURLByShortURL is a method used to get a URL from the database by its short URL It takes a string as a parameter and returns a URL struct and an error

func (*URL) GenerateShortURL

func (u *URL) GenerateShortURL()

GenerateShortURL is a method used to generate a random short URL It takes no parameters and returns nothing

type User

type User struct {
	gorm.Model
	Name     string `json:"name" binding:"required"`
	Email    string `json:"email" binding:"required,email" gorm:"unique"`
	Password string `json:"password" binding:"required"`
}

User defines the user in db User struct is used to store user information in the database

func (*User) BeforeSave

func (user *User) BeforeSave(tx *gorm.DB) error

BeforeSave is a hook that is called before a user is saved to the database It hashes the user's password before saving it to the database

func (*User) CheckPassword

func (user *User) CheckPassword(providedPassword string) error

CheckPassword checks user password CheckPassword takes a string as a parameter and compares it to the user's encrypted password It returns an error if there is an issue comparing the passwords

func (*User) CreateUserRecord

func (user *User) CreateUserRecord() error

CreateUserRecord creates a user record in the database CreateUserRecord takes a pointer to a User struct and creates a user record in the database It returns an error if there is an issue creating the user record

func (*User) HashPassword

func (user *User) HashPassword(password string) error

HashPassword encrypts user password HashPassword takes a string as a parameter and encrypts it using bcrypt It returns an error if there is an issue encrypting the password

func (*User) Prepare

func (user *User) Prepare()

Prepare is a function that is called before a user is saved to the database It escapes any HTML characters and trims any whitespace

func (*User) SaveUser

func (u *User) SaveUser() (*User, error)

SaveUser is a function that is used to save a user to the database It takes a pointer to a gorm.DB as an argument

func (*User) Validate

func (user *User) Validate(action string) map[string]string

Validate is a function that is used to validate a user before saving it to the database It takes an action as an argument, which is used to determine which validation to perform

Jump to

Keyboard shortcuts

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