models

package
v0.0.0-...-9bc149c Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitializeDB

func InitializeDB(connection string) error

InitializeDB create a connection with the database and migrates all models

func StopDB

func StopDB()

StopDB closes connection to database

Types

type Profile

type Profile struct {
	Username  string `json:"username"`
	Bio       string `json:"bio"`
	Image     string `json:"image"`
	Following bool   `json:"following"`
}

Profile is a subset of the user model meant for presentation

func ProfileFromUser

func ProfileFromUser(u User, following bool) Profile

ProfileFromUser creates a new profile object from a user

type Tag

type Tag struct {
	ID   uint   `json:"-" gorm:"unique;not null;primary_key"`
	Name string `json:"name" gorm:"unique;not null"`
}

Tag describe article content

func GetTags

func GetTags() []Tag

GetTags retrieves all Tags from the database

func (*Tag) MarshalJSON

func (t *Tag) MarshalJSON() ([]byte, error)

MarshalJSON encodes the tag as a single string

func (*Tag) Save

func (t *Tag) Save()

Save migrates any changes to database

func (*Tag) UnmarshalJSON

func (t *Tag) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a tag from a single string

type User

type User struct {
	ID       uint   `json:"-" gorm:"unique;not null;primary_key"`
	Username string `json:"username" gorm:"unique;not null"`
	Email    string `json:"email" gorm:"unique;not null"`
	Bio      string `json:"bio"`
	Image    string `json:"image"` // image url?
	Hash     string `json:"-" gorm:"not null"`
	Token    string `json:"token" gorm:"-"`

	// Self referencing many 2 many relationship
	// http://gorm.io/docs/many_to_many.html
	Following []User `json:"-" gorm:"many2many:followings;association_jointable_foreignkey:follower_id"`
}

User is a database model to hold user information

func GetUser

func GetUser(username string) (User, error)

GetUser retrieves a user model from the database by it's username

func (User) CheckPassword

func (u User) CheckPassword(password string) bool

CheckPassword compares a given password to the users hashed password

func (*User) FollowUser

func (u *User) FollowUser(username string) error

FollowUser adds a new user to this users following list

func (*User) GetProfile

func (u *User) GetProfile(following bool) Profile

GetProfile returns the profile for a user

func (*User) IsFollowingUser

func (u *User) IsFollowingUser(username string) bool

IsFollowingUser return bool indicating whether a user is in this users following list

func (*User) Save

func (u *User) Save()

Save migrates any changes to database

func (*User) UnFollowUser

func (u *User) UnFollowUser(username string) error

UnFollowUser removes a user to this users following list

func (*User) UpdatePassword

func (u *User) UpdatePassword(newPassword string) error

UpdatePassword hashes a new password and updates the stored hash (DOES NOT SAVE TO DB)

func (*User) UpdateUser

func (u *User) UpdateUser(newUser User)

UpdateUser updates fields on the User model, does not update password. (Does not save to db)

Jump to

Keyboard shortcuts

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