users

package
v0.0.0-...-2b9086d Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

The user module containing the user CRU operation.

model.go: definition of orm based data model

routers.go: router binding and core logic

serializers.go: definition the schema of return data

validators.go: definition the validator of form data

Index

Constants

This section is empty.

Variables

View Source
var AuthorizationHeaderExtractor = &request.PostExtractionFilter{
	request.HeaderExtractor{"Authorization"},
	stripBearerPrefixFromTokenString,
}

Extract token from Authorization header Uses PostExtractionFilter to strip "TOKEN " prefix from header

Extractor for OAuth2 access tokens. Looks in 'Authorization' header then 'access_token' argument for a token.

Functions

func AuthMiddleware

func AuthMiddleware(auto401 bool) gin.HandlerFunc

You can custom middlewares yourself as the doc: https://github.com/gin-gonic/gin#custom-middleware

r.Use(AuthMiddleware(true))

func AutoMigrate

func AutoMigrate()

Migrate the schema of database if needed

func ProfileFollow

func ProfileFollow(c *gin.Context)

func ProfileRegister

func ProfileRegister(router *gin.RouterGroup)

func ProfileRetrieve

func ProfileRetrieve(c *gin.Context)

func ProfileUnfollow

func ProfileUnfollow(c *gin.Context)

func SaveOne

func SaveOne(data interface{}) error

You could input an UserModel which will be saved in database returning with error info

if err := SaveOne(&userModel); err != nil { ... }

func UpdateContextUserModel

func UpdateContextUserModel(c *gin.Context, my_user_id uint)

A helper to write user_id and user_model to the context

func UserRegister

func UserRegister(router *gin.RouterGroup)

func UserRetrieve

func UserRetrieve(c *gin.Context)

func UserUpdate

func UserUpdate(c *gin.Context)

func UsersLogin

func UsersLogin(c *gin.Context)

func UsersRegister

func UsersRegister(router *gin.RouterGroup)

func UsersRegistration

func UsersRegistration(c *gin.Context)

Types

type FollowModel

type FollowModel struct {
	gorm.Model
	Following    UserModel
	FollowingID  uint
	FollowedBy   UserModel
	FollowedByID uint
}

A hack way to save ManyToMany relationship, gorm will build the alias as FollowingBy <-> FollowingByID <-> "following_by_id".

DB schema looks like: id, created_at, updated_at, deleted_at, following_id, followed_by_id.

Retrieve them by:

db.Where(FollowModel{ FollowingID:  v.ID, FollowedByID: u.ID, }).First(&follow)
db.Where(FollowModel{ FollowedByID: u.ID, }).Find(&follows)

More details about gorm.Model: http://jinzhu.me/gorm/models.html#conventions

type LoginValidator

type LoginValidator struct {
	User struct {
		Email    string `form:"email" json:"email" binding:"exists,email"`
		Password string `form:"password"json:"password" binding:"exists,min=8,max=255"`
	} `json:"user"`
	// contains filtered or unexported fields
}

func NewLoginValidator

func NewLoginValidator() LoginValidator

You can put the default value of a Validator here

func (*LoginValidator) Bind

func (self *LoginValidator) Bind(c *gin.Context) error

type ProfileResponse

type ProfileResponse struct {
	ID        uint    `json:"-"`
	Username  string  `json:"username"`
	Bio       string  `json:"bio"`
	Image     *string `json:"image"`
	Following bool    `json:"following"`
}

Declare your response schema here

type ProfileSerializer

type ProfileSerializer struct {
	C *gin.Context
	UserModel
}

func (*ProfileSerializer) Response

func (self *ProfileSerializer) Response() ProfileResponse

Put your response logic including wrap the userModel here.

type UserModel

type UserModel struct {
	ID           uint    `gorm:"primary_key"`
	Username     string  `gorm:"column:username"`
	Email        string  `gorm:"column:email;unique_index"`
	Bio          string  `gorm:"column:bio;size:1024"`
	Image        *string `gorm:"column:image"`
	PasswordHash string  `gorm:"column:password;not null"`
}

Models should only be concerned with database schema, more strict checking should be put in validator.

More detail you can find here: http://jinzhu.me/gorm/models.html#model-definition

HINT: If you want to split null and "", you should use *string instead of string.

func FindOneUser

func FindOneUser(condition interface{}) (UserModel, error)

You could input the conditions and it will return an UserModel in database with error info.

userModel, err := FindOneUser(&UserModel{Username: "username0"})

func (UserModel) GetFollowings

func (u UserModel) GetFollowings() []UserModel

You could get a following list of userModel

followings := userModel.GetFollowings()

func (*UserModel) Update

func (model *UserModel) Update(data interface{}) error

You could update properties of an UserModel to database returning with error info.

err := db.Model(userModel).Update(UserModel{Username: "wangzitian0"}).Error

type UserModelValidator

type UserModelValidator struct {
	User struct {
		Username string `form:"username" json:"username" binding:"exists,alphanum,min=4,max=255"`
		Email    string `form:"email" json:"email" binding:"exists,email"`
		Password string `form:"password" json:"password" binding:"exists,min=8,max=255"`
		Bio      string `form:"bio" json:"bio" binding:"max=1024"`
		Image    string `form:"image" json:"image" binding:"omitempty,url"`
	} `json:"user"`
	// contains filtered or unexported fields
}

*ModelValidator containing two parts: - Validator: write the form/json checking rule according to the doc https://github.com/go-playground/validator - DataModel: fill with data from Validator after invoking common.Bind(c, self) Then, you can just call model.save() after the data is ready in DataModel.

func NewUserModelValidator

func NewUserModelValidator() UserModelValidator

You can put the default value of a Validator here

func NewUserModelValidatorFillWith

func NewUserModelValidatorFillWith(userModel UserModel) UserModelValidator

func (*UserModelValidator) Bind

func (self *UserModelValidator) Bind(c *gin.Context) error

There are some difference when you create or update a model, you need to fill the DataModel before update so that you can use your origin data to cheat the validator. BTW, you can put your general binding logic here such as setting password.

type UserResponse

type UserResponse struct {
	Username string  `json:"username"`
	Email    string  `json:"email"`
	Bio      string  `json:"bio"`
	Image    *string `json:"image"`
	Token    string  `json:"token"`
}

type UserSerializer

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

func (*UserSerializer) Response

func (self *UserSerializer) Response() UserResponse

Jump to

Keyboard shortcuts

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