从golang-gin-realworld-example-app项目学写httpapi (三)

https://github.com/gothinkster/golang-gin-realworld-example-app/tree/master/users

路由定义

package users

import (
    "errors"
    "github.com/wangzitian0/golang-gin-starter-kit/common"
    "gopkg.in/gin-gonic/gin.v1"
    "net/http"
)

// 路由函数 用于用户注册、登录请求,注意 参数 *gin.RouterGroup, 用gin的groups router调用
func UsersRegister(router *gin.RouterGroup) {
    router.POST("/", UsersRegistration)
    router.POST("/login", UsersLogin)
}

// 路由函数 用于用户信息获取、更新请求
func UserRegister(router *gin.RouterGroup) {
    router.GET("/", UserRetrieve)
    router.PUT("/", UserUpdate)
}

// 路由函数 用于用户简介获取、增加关注、删除关注
// 请求参数变量,使用 :变量名
func ProfileRegister(router *gin.RouterGroup) {
    router.GET("/:username", ProfileRetrieve)
    router.POST("/:username/follow", ProfileFollow)
    router.DELETE("/:username/follow", ProfileUnfollow)
}

// 请求处理函数,用户简介获取,注意 参数 *gin.Context
func ProfileRetrieve(c *gin.Context) {
    // 获取请求参数 username
    username := c.Param("username")

    // 调用模型定义的FindOneUser函数
    userModel, err := FindOneUser(&UserModel{Username: username})
    
    if err != nil {
        c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username")))
        return
    }
    profileSerializer := ProfileSerializer{c, userModel}
    c.JSON(http.StatusOK, gin.H{"profile": profileSerializer.Response()})
}

func ProfileFollow(c *gin.Context) {
    username := c.Param("username")
    userModel, err := FindOneUser(&UserModel{Username: username})
    if err != nil {
        c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username")))
        return
    }
    myUserModel := c.MustGet("my_user_model").(UserModel)
    err = myUserModel.following(userModel)
    if err != nil {
        c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
        return
    }
    serializer := ProfileSerializer{c, userModel}
    c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()})
}

func ProfileUnfollow(c *gin.Context) {
    username := c.Param("username")
    userModel, err := FindOneUser(&UserModel{Username: username})
    if err != nil {
        c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username")))
        return
    }
    myUserModel := c.MustGet("my_user_model").(UserModel)

    err = myUserModel.unFollowing(userModel)
    if err != nil {
        c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
        return
    }
    serializer := ProfileSerializer{c, userModel}
    c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()})
}

func UsersRegistration(c *gin.Context) {
    userModelValidator := NewUserModelValidator()
    if err := userModelValidator.Bind(c); err != nil {
        c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
        return
    }

    if err := SaveOne(&userModelValidator.userModel); err != nil {
        c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
        return
    }
    c.Set("my_user_model", userModelValidator.userModel)
    serializer := UserSerializer{c}
    c.JSON(http.StatusCreated, gin.H{"user": serializer.Response()})
}

func UsersLogin(c *gin.Context) {
    loginValidator := NewLoginValidator()
    if err := loginValidator.Bind(c); err != nil {
        c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
        return
    }
    userModel, err := FindOneUser(&UserModel{Email: loginValidator.userModel.Email})

    if err != nil {
        c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password")))
        return
    }

    if userModel.checkPassword(loginValidator.User.Password) != nil {
        c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password")))
        return
    }
    UpdateContextUserModel(c, userModel.ID)
    serializer := UserSerializer{c}
    c.JSON(http.StatusOK, gin.H{"user": serializer.Response()})
}

func UserRetrieve(c *gin.Context) {
    serializer := UserSerializer{c}
    c.JSON(http.StatusOK, gin.H{"user": serializer.Response()})
}

func UserUpdate(c *gin.Context) {
    myUserModel := c.MustGet("my_user_model").(UserModel)
    userModelValidator := NewUserModelValidatorFillWith(myUserModel)
    if err := userModelValidator.Bind(c); err != nil {
        c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
        return
    }

    userModelValidator.userModel.ID = myUserModel.ID
    if err := myUserModel.Update(userModelValidator.userModel); err != nil {
        c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
        return
    }
    UpdateContextUserModel(c, myUserModel.ID)
    serializer := UserSerializer{c}
    c.JSON(http.StatusOK, gin.H{"user": serializer.Response()})
}

从golang-gin-realworld-example-app项目学写httpapi (三)

上一篇:[vue]模拟移动端三级路由


下一篇:Android学习——AlertDialog