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 ¶
- Variables
- func AuthMiddleware(auto401 bool) gin.HandlerFunc
- func AutoMigrate()
- func ProfileFollow(c *gin.Context)
- func ProfileRegister(router *gin.RouterGroup)
- func ProfileRetrieve(c *gin.Context)
- func ProfileUnfollow(c *gin.Context)
- func SaveOne(data interface{}) error
- func UpdateContextUserModel(c *gin.Context, my_user_id uint)
- func UserRegister(router *gin.RouterGroup)
- func UserRetrieve(c *gin.Context)
- func UserUpdate(c *gin.Context)
- func UsersLogin(c *gin.Context)
- func UsersRegister(router *gin.RouterGroup)
- func UsersRegistration(c *gin.Context)
- type FollowModel
- type LoginValidator
- type ProfileResponse
- type ProfileSerializer
- type UserModel
- type UserModelValidator
- type UserResponse
- type UserSerializer
Constants ¶
This section is empty.
Variables ¶
var AuthorizationHeaderExtractor = &request.PostExtractionFilter{ request.HeaderExtractor{"Authorization"}, stripBearerPrefixFromTokenString, }
Extract token from Authorization header Uses PostExtractionFilter to strip "TOKEN " prefix from header
var MyAuth2Extractor = &request.MultiExtractor{ AuthorizationHeaderExtractor, request.ArgumentExtractor{"access_token"}, }
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 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 ¶
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 ¶
You could get a following list of userModel
followings := userModel.GetFollowings()
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 UserSerializer ¶
type UserSerializer struct {
// contains filtered or unexported fields
}
func (*UserSerializer) Response ¶
func (self *UserSerializer) Response() UserResponse